-
Notifications
You must be signed in to change notification settings - Fork 114
/
GroupArtboardObjects.jsx
311 lines (252 loc) · 9.14 KB
/
GroupArtboardObjects.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
/*
GroupArtboardObjects.jsx for Adobe Illustrator
Description: Group objects on the artboards. It will skip locked or hidden objects
Date: June, 2024
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.2 Added artboards range, sort groups in layers
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: 'Group Artboard Objects',
version: 'v0.2'
};
var CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os)
};
var SETTINGS = {
name: SCRIPT.name.replace(/\s/g, '_') + '_data.json',
folder: Folder.myDocuments + '/Adobe Scripts/'
};
if (!isCorrectEnv('version:16')) return;
var doc = app.activeDocument;
var absLength = doc.artboards.length;
var currAb = doc.artboards.getActiveArtboardIndex();
// DIALOG
var win = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
win.orientation = 'column';
win.alignChildren = ['fill', 'center'];
// RANGE
var rangePnl = win.add('panel', undefined, 'Artboards Range');
rangePnl.orientation = 'column';
rangePnl.alignChildren = ['fill', 'center'];
rangePnl.margins = [10, 15, 10, 7];
var radio = rangePnl.add('group');
radio.alignChildren = ['left', 'center'];
var isCurr = radio.add('radiobutton', undefined, 'Current #' + (currAb + 1));
var isCstm = radio.add('radiobutton', undefined, 'Custom');
isCstm.value = true;
var idx = rangePnl.add('group');
idx.alignChildren = ['fill', 'center'];
var startGrp = idx.add('group');
startGrp.alignChildren = ['left', 'center'];
var startLbl = startGrp.add('statictext', undefined, 'Start:');
startLbl.justify = 'left';
var startInp = startGrp.add('edittext', undefined, '1');
startInp.characters = 6;
startInp.enabled = isCstm.value;
var endGrp = idx.add('group');
endGrp.alignChildren = ['left', 'center'];
var endLbl = endGrp.add('statictext', undefined, 'End:');
endLbl.justify = 'left';
var endInp = endGrp.add('edittext', undefined, absLength);
endInp.characters = 6;
endInp.enabled = isCstm.value;
// SORT
var sortPnl = win.add('panel', undefined, 'Move Groups in Layers');
sortPnl.orientation = 'row';
sortPnl.alignChildren = ['left', 'center'];
sortPnl.margins = [10, 15, 10, 7];
var isKeep = sortPnl.add('radiobutton', undefined, 'No');
isKeep.value = true;
var isToBtm = sortPnl.add('radiobutton', undefined, 'To Bottom');
var isToTop = sortPnl.add('radiobutton', undefined, 'To Top');
// OPTIONS
var optPnl = win.add('panel', undefined, 'Options');
optPnl.orientation = 'column';
optPnl.alignChildren = ['left', 'center'];
optPnl.margins = [10, 15, 10, 7];
var isAddName = optPnl.add('checkbox', undefined, 'Rename Groups as Artboards');
isAddName.value = true;
var isGrpSingle = optPnl.add('checkbox', undefined, 'Group Single Objects');
isGrpSingle.value = true;
// BUTTONS
var btns = win.add('group');
btns.alignChildren = ['fill', 'fill'];
var cancel, ok;
if (CFG.isMac) {
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
ok = btns.add('button', undefined, 'Ok', { name: 'ok' });
} else {
ok = btns.add('button', undefined, 'Ok', { name: 'ok' });
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
}
var copyright = win.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
var prgGroup = win.add('group');
var progBar = prgGroup.add('progressbar', [20, 5, 230, 10], 0, 100);
loadSettings(SETTINGS);
// EVENTS
isCurr.onClick = function () {
startInp.enabled = endInp.enabled = false;
}
isCstm.onClick = function () {
startInp.enabled = endInp.enabled = true;
}
cancel.onClick = win.close;
ok.onClick = function () {
var zOrder = isToTop.value ? 'top' : (isToBtm.value ? 'bottom' : '');
if (isCurr.value) { // Current artboard
groupArtboard(doc, currAb, isGrpSingle.value, zOrder, isAddName.value);
progBar.value = 100;
} else { // Custom range
var startIdx = parseInt(startInp.text) - 1 || 0;
var endIdx = parseInt(endInp.text) || absLength;
if (isNaN(startIdx) || startIdx < 0 || startIdx >= absLength) {
alert('Start index is invalid', 'Input error');
return;
}
if (isNaN(endIdx) || endIdx < startIdx || endIdx > absLength) {
alert('End index is invalid', 'Input error');
return;
}
var rangeLength = endIdx - startIdx;
for (var i = startIdx; i < endIdx; i++) {
groupArtboard(doc, i, isGrpSingle.value, zOrder, isAddName.value);
progBar.value = parseInt(100 * (i - startIdx + 1) / rangeLength);
win.update();
}
}
app.executeMenuCommand('deselectall');
saveSettings(SETTINGS);
win.close();
}
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
// Save UI options to 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.artboard = isCurr.value;
pref.move = isKeep.value ? 0 : (isToBtm.value ? 1 : 2);
pref.group = isGrpSingle.value;
pref.rename = isAddName.value;
var data = pref.toSource();
f.write(data);
f.close();
}
// Load options from file
function loadSettings(prefs) {
var f = File(prefs.folder + prefs.name);
if (!f.exists) return;
try {
f.encoding = 'UTF-8';
f.open('r');
var json = f.readln();
var pref = new Function('return ' + json)();
f.close();
if (typeof pref != 'undefined') {
isCurr.value = pref.artboard;
isCstm.value = !isCurr.value;
startInp.enabled = endInp.enabled = isCstm.value;
sortPnl.children[pref.move].value = true;
isAddName.value = pref.rename;
isGrpSingle.value = pref.group;
}
} catch (err) {}
}
win.center();
win.show();
}
// Check the script environment
function isCorrectEnv() {
var args = ['app', 'document'];
args.push.apply(args, arguments);
for (var i = 0; i < args.length; i++) {
var arg = args[i].toString().toLowerCase();
switch (true) {
case /app/g.test(arg):
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Wrong app version\nSorry, script only works in Illustrator v.' + rqdVers + ' and later', 'Script error');
return false;
}
break;
case /document/g.test(arg):
if (!app.documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return false;
}
break;
case /selection/g.test(arg):
var rqdLen = parseFloat(arg.split(':')[1]);
if (app.selection.length < rqdLen || selection.typename === 'TextRange') {
alert('Few objects are selected\nPlease select ' + rqdLen + ' path(s) and try again', 'Script error');
return false;
}
break;
}
}
return true;
}
// Group all items on artboard
function groupArtboard(doc, i, isGroup, zOrder, isAddName) {
app.executeMenuCommand('deselectall');
doc.artboards.setActiveArtboardIndex(i);
doc.selectObjectsOnActiveArtboard();
var sel = app.selection;
if (sel.length === 0) return;
if (sel.length > 1 || (sel.length == 1 && sel[0].typename !== 'GroupItem' && isGroup)) {
app.executeMenuCommand('group');
}
sel = app.selection; // Update selection
if (zOrder === 'top') {
sel[0].zOrder(ZOrderMethod.BRINGTOFRONT);
} else if (zOrder === 'bottom') {
sel[0].zOrder(ZOrderMethod.SENDTOBACK);
}
if (isAddName) {
sel[0].name = doc.artboards[i].name;
}
}
// Open a URL in the default web browser
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();
}
// Run script
try {
main();
} catch (err) {}