-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathPlaceSymbols.jsx
544 lines (458 loc) · 15.3 KB
/
PlaceSymbols.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
PlaceSymbols.jsx for Adobe Illustrator
Description: Search and place symbol objects from the Symbols panel on the canvas
Date: August, 2024
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1 Initial version
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
NOTICE:
Tested with Adobe Illustrator CC 2019-2025 (Mac/Win).
This script is provided "as is" without warranty of any kind.
Free to use, not for sale
Released under the MIT license
http://opensource.org/licenses/mit-license.php
Check my other scripts: https://github.com/creold
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
var SCRIPT = {
name: 'Place Symbols',
version: 'v0.1'
};
var CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os),
uiOpacity: .97 // UI window opacity. Range 0-1
};
var SETTINGS = {
name: SCRIPT.name.replace(/\s/g, '_') + '_data.json',
folder: Folder.myDocuments + '/Adobe Scripts/'
};
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return;
}
if (!app.documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return;
}
var docSymbols = get(app.activeDocument.symbols);
// Sort default list alphabetically
docSymbols.sort(function (a, b) {
var aName = a.name;
var bName = b.name;
if (!isNaN(aName) && !isNaN(bName)) {
return 1 * aName - 1 * bName;
} else {
return hasMixedCase(aName, bName) ?
handleMixedCase(aName, bName, false) :
aName.toLowerCase().localeCompare(bName.toLowerCase());
}
})
var targetLayer = getEditableLayer();
// DIALOG
var win = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version, undefined, {resizeable: true});
win.preferredSize = [240, 200];
win.alignChildren = ['fill', 'fill'];
win.opacity = CFG.uiOpacity;
var queryGrp = win.add('group');
queryGrp.alignChildren = ['fill', 'center'];
queryGrp.alignment = ['fill', 'top'];
var query = queryGrp.add('edittext', undefined, '');
query.alignment = ['fill', 'center'];
if (CFG.isMac || CFG.aiVers >= 26.4 || CFG.aiVers <= 17) {
query.active = true;
}
var clear = queryGrp.add('button', undefined, 'Clear');
clear.preferredSize.width = 50;
clear.alignment = ['right', 'center'];
// SEARCH RESULTS
var list = win.add('listbox', undefined, '',
{
numberOfColumns: 2,
showHeaders: true,
columnTitles: ['Parent Symbol', 'Instances'],
multiselect: true
});
// FOOTER
var footer = win.add('group');
footer.alignChildren = ['fill', 'center'];
footer.alignment = ['fill', 'bottom'];
// Instances counter include hidden symbols
var isHiddenCount = footer.add('checkbox', undefined, 'Count Hidden Symbol Instances');
isHiddenCount.alignment = ['left', 'center'];
isHiddenCount.value = true;
var btns = win.add('group');
btns.alignChildren = ['fill', 'center'];
btns.alignment = ['right', 'center'];
var close = btns.add('button', undefined, 'Close', {name: 'cancel'});
var select = btns.add('button', undefined, 'Select Instances', {name: 'select'});
select.enabled = list.selection !== null;
var place = btns.add('button', undefined, 'Place', {name: 'place'});
place.enabled = list.selection !== null;
var copyright = win.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
// EVENTS
addList('', docSymbols);
list.preferredSize = [240, 200];
query.onChanging = function () {
addList(this.text, docSymbols);
}
clear.onClick = function () {
query.text = '';
this.active = true;
query.active = true;
addList('', docSymbols);
}
list.onChange = function () {
place.enabled = list.selection !== null;
select.enabled = list.selection !== null;
}
isHiddenCount.onClick = function () {
addList(query.text, docSymbols);
}
place.onClick = function () {
var items = getSelectedList(list);
if (!items.length) {
this.active = true;
this.active = false;
return;
}
placeSymbols(targetLayer, items);
// Update instances counter
for (var i = 0, len = items.length; i < len; i++) {
var instCol = items[i].subItems[0];
instCol.text = instCol.text.length ? parseInt(instCol.text) + 1 : 1;
}
list.hide();
list.show();
this.active = true;
this.active = false;
}
select.onClick = function () {
var items = getSelectedList(list);
if (!items.length) {
this.active = true;
this.active = false;
return;
}
selectSymbols(items);
this.active = true;
this.active = false;
}
close.onClick = win.close;
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold/');
});
win.onShow = function () {
loadSettings(SETTINGS);
addList(query.text, docSymbols);
this.layout.resize();
}
win.onResizing = function () {
this.layout.resize();
}
win.onClose = function () {
saveSettings(SETTINGS);
}
/**
* Display a list of symbols to place based on the search string
* @param {string} query - The search string to filter symbols
* @param {(Object|Array)} symbols - An array of symbol objects to filter
*/
function addList(query, symbols) {
if (query == undefined) query = '';
var filtered = filterSymbols(query, symbols);
var instances = countInstances(isHiddenCount.value);
list.removeAll(); // Clear current list before search
// Create listbox rows from search results
for (var i = 0, len = filtered.length; i < len; i++) {
var name = filtered[i].name;
var row = list.add('item', name);
row.subItems[0].text = instances[name] !== undefined ? instances[name] : '';
}
}
/**
* Get the selected items from a list object
* @param {Object} obj - The list object containing items
* @returns {Array} results - An array of selected items
*/
function getSelectedList(obj) {
var results = [];
for (var i = 0, len = obj.children.length; i < len; i++) {
if (obj.children[i].selected) results.push(obj.children[i]);
}
return results;
}
/**
* Save options to a file
* @param {Object} prefs - The preferences file
*/
function saveSettings(prefs) {
if(!Folder(prefs.folder).exists) Folder(prefs.folder).create();
var f = new File(prefs.folder + prefs.name);
f.encoding = 'UTF-8';
f.open('w');
var pref = {};
pref.win_x = win.location.x;
pref.win_y = win.location.y;
pref.win_w = win.size.width;
pref.win_h = win.size.height;
pref.query = query.text;
pref.hidden = isHiddenCount.value;
var data = pref.toSource();
f.write(data);
f.close();
}
/**
* Loads options from a file
* @param {Object} prefs - The preferences file
*/
function loadSettings(prefs) {
var f = File(prefs.folder + prefs.name);
if (f.exists) {
try {
f.encoding = 'UTF-8';
f.open('r');
var json = f.readln();
var pref = new Function('return ' + json)();
f.close();
if (typeof pref != 'undefined') {
win.location = [pref.win_x ? pref.win_x : 0, pref.win_y ? pref.win_y : 0];
if (pref.win_w && pref.win_h) {
win.size = [pref.win_w, pref.win_h];
}
query.text = pref.query;
isHiddenCount.value = pref.hidden;
win.update();
}
} catch (err) {}
}
}
win.center();
win.show();
}
/**
* Convert a collection into a standard Array
* @param {Object} coll - The collection to be converted
* @returns {Array} A new array containing the elements
*/
function get(coll) {
var results = [];
for (var i = 0, len = coll.length; i < len; i++) {
results.push(coll[i]);
}
return results;
}
/**
* Check if two strings have mixed case (one is uppercase and the other is lowercase)
* @param {string} a - The first string
* @param {string} b - The second string
* @returns {boolean} True if the strings have mixed case, false otherwise
*/
function hasMixedCase(a, b) {
if (a.charAt(0).toLowerCase() !== b.charAt(0).toLowerCase()) return false;
return isUpperCase(a) && isLowerCase(b) ? true : isLowerCase(a) && isUpperCase(b);
}
/**
* Handle the sorting of mixed case strings
* @param {string} a - The first string
* @param {string} b - The second string
* @param {boolean} isLowerFirst - If true, lowercase strings come first
* @returns {number} The sort order value
*/
function handleMixedCase(a, b, isLowerFirst) {
var result = a === b ? 0 : isUpperCase(a) ? 1 : -1;
return isLowerFirst ? result : result * -1;
}
/**
* Check if the first character of a string is uppercase
* @param {string} str - The string to be checked
* @returns {boolean} True if the first character is uppercase, false otherwise
*/
function isUpperCase(str) {
return /[A-Z]/.test(str.charAt(0));
}
/**
* Check if the first character of a string is lowercase
* @param {string} str - The string to be checked
* @returns {boolean} True if the first character is lowercase, false otherwise
*/
function isLowerCase(str) {
return /[a-z]/.test(str.charAt(0));
}
/**
* Get an editable layer from the active document
* @returns {Object} The editable layer
*/
function getEditableLayer() {
var doc = app.activeDocument;
var activeLay = doc.activeLayer;
if (activeLay.visible && !activeLay.locked) return activeLay;
for (var i = 0, len = doc.layers.length; i < len; i++) {
var currLay = doc.layers[i];
if (currLay.visible && !currLay.locked) {
doc.activeLayer = currLay;
return currLay;
}
}
doc.layers[0].visible = true
doc.layers[0].locked = false;
doc.activeLayer = doc.layers[0];
return doc.layers[0];
}
/**
* Filter symbols in the active document based on the search string
* @param {string} query - The search string used to filter symbols
* @param {(Object|Array)} symbols - An array of symbol objects to filter
* @returns {Array} sortedResults - An array of symbols that match the search criteria
*/
function filterSymbols(query, symbols) {
if (query == undefined || query.length === 0) {
return symbols;
}
var results = [];
query = normalizeString(query);
for (var j = 0, len = symbols.length; j < len; j++) {
var symbol = symbols[j];
var index = query.length > 0 ? normalizeString(symbol.name).indexOf(query) : 0;
if (index !== -1) {
var score = index === 0 ? 1 : index > 0 ? 0.5 : 0;
results.push({ symbol: symbol, name: symbol.name, score: score, index: index });
}
}
// Sort results based on score and index
for (var j = 0; j < results.length; j++) {
for (var k = j + 1; k < results.length; k++) {
var a = results[j];
var b = results[k];
if (b.score > a.score || (b.score === a.score && (a.index > b.index || (a.index === b.index && a.name.localeCompare(b.name) > 0)))) {
var temp = results[j];
results[j] = results[k];
results[k] = temp;
}
}
}
var sortedResults = [];
for (var s = 0; s < results.length; s++) {
sortedResults.push(results[s].symbol);
}
return sortedResults;
}
/**
* Normalize a string (lowercase, trim)
* @param {string} str - The string to normalize.
* @returns {string} The normalized string
*/
function normalizeString(str) {
return str.toLowerCase().replace(/^\s+|\s+$/g, '');
}
/**
* Count the instances of symbols in the active document
* @param {boolean} isHiddenCount - Flag to include hidden symbols in the count
* @returns {Object} result - An object with symbol names as keys and their counts as values
*/
function countInstances(isHiddenCount) {
var doc = app.activeDocument;
var symbols = doc.symbolItems;
var i = symbols.length;
var symbol, result = {};
while (i--) {
if (!isHiddenCount && isHidden(symbols[i])) continue;
symbol = symbols[i].symbol;
result[symbol.name] = result[symbol.name] || 0;
result[symbol.name]++;
}
return result;
}
/**
* Check if an item is hidden
* @param {Object} item - The item to be checked
* @returns {boolean} result - True if the item is hidden, false otherwise
*/
function isHidden(item) {
if (item.hasOwnProperty('hidden') && item.hidden == true) {
return true;
}
if (item.hasOwnProperty('visible') && item.visible == false) {
return true;
}
var result = false;
var prnt = item.parent;
switch (prnt.typename) {
case 'GroupItem':
result = isHidden(prnt);
break;
case 'Layer':
result = isHidden(prnt);
break;
default:
break;
}
return result;
}
/**
* Place symbols into the target container in the active document
* @param {Object} target - The target container where symbols will be placed
* @param {Array} arr - An array of objects containing the names of symbols
*/
function placeSymbols(target, arr) {
var doc = app.activeDocument;
for (var i = 0, len = arr.length; i < len; i++) {
var name = arr[i].text;
try {
var symbol = doc.symbols.getByName(name);
symbol = target.symbolItems.add(symbol);
symbol.selected = true;
} catch (err) {}
}
if (parseFloat(app.version) >= 16) {
app.executeMenuCommand('artboard');
app.executeMenuCommand('artboard');
}
}
/**
* Select symbols in the active document based on the provided array of names
* @param {(Object|Array)} arr - An array of objects containing the names of symbols
*/
function selectSymbols(arr) {
var doc = app.activeDocument;
app.selection = null;
for (var i = 0, len = arr.length; i < len; i++) {
var name = arr[i].text;
for (var j = 0; j < doc.symbolItems.length; j++) {
var item = doc.symbolItems[j];
if (item.symbol.name === name && item.editable) {
item.selected = true;
}
}
}
if (app.selection.length && parseFloat(app.version) >= 16) {
app.executeMenuCommand('artboard');
app.executeMenuCommand('artboard');
}
}
/**
* Open a URL in the default web browser
* @param {string} url - The URL to open in the web browser
* @returns {void}
*/
function openURL(url) {
var html = new File(Folder.temp.absoluteURI + '/aisLink.html');
html.open('w');
var htmlBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
html.write(htmlBody);
html.close();
html.execute();
}
try {
main();
} catch (err) {}