-
Notifications
You must be signed in to change notification settings - Fork 114
/
DuplicateToArtboards.jsx
393 lines (334 loc) · 11.3 KB
/
DuplicateToArtboards.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
/*
DuplicateToArtboards.jsx for Adobe Illustrator
Description: Copy and paste selected artboard objects to the same position on specific artboards
Date: September, 2022
Modification date: February, 2024
Author: Sergey Osokin, email: hi@sergosokin.ru
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.1.3 Removed input activation on Windows OS below CC v26.4
0.1.2 Fixed display of indexes when launched by action
0.1.1 Fixed input activation in Windows OS
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
// Main function
function main() {
var SCRIPT = {
name: 'Duplicate To Artboards',
version: 'v0.1.3'
},
CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os),
isPreserve: app.preferences.getBooleanPreference('layers/pastePreserve'), // Default Paste Remembers Layers
color: [255, 0, 0], // RGB artboard index color
tmpLyr: 'ARTBOARD_INDEX',
uiOpacity: .98 // UI window opacity. Range 0-1
},
SETTINGS = {
name: SCRIPT.name.replace(/\s/g, '_') + '_data.json',
folder: Folder.myDocuments + '/Adobe Scripts/'
};
if (!isCorrectEnv('version:16', 'selection')) return;
polyfills();
// Dialog
var win = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
win.orientation = 'column';
win.alignChildren = 'fill';
win.opacity = CFG.uiOpacity;
var wrapper = win.add('group');
wrapper.alignChildren = ['fill', 'fill'];
// Artboards
var absPnl = wrapper.add('panel', undefined, 'Artboards range');
absPnl.orientation = 'column';
absPnl.alignChildren = 'fill';
absPnl.margins = [10, 15, 10, 10];
var absInp = absPnl.add('edittext', undefined, 1);
if (CFG.isMac || CFG.aiVers >= 26.4 || CFG.aiVers <= 17) {
absInp.active = true;
}
var absDescr = absPnl.add('statictext', undefined, '(e.g. "1, 3-5" to export 1, 3, 4, 5)\nor enter nothing to use all', { multiline: true });
absDescr.justify = 'left';
// Separator
wrapper.separator = wrapper.add('panel');
wrapper.separator.minimumSize.width = wrapper.separator.maximumSize.width = 2;
// Options
var optsGrp = wrapper.add('group');
optsGrp.orientation = 'column';
optsGrp.alignChildren = 'fill';
var pasteGrp = optsGrp.add('group');
pasteGrp.orientation = 'column';
pasteGrp.alignChildren = 'fill';
pasteGrp.spacing = 3;
var isFront = pasteGrp.add('radiobutton', undefined, 'Paste in front');
isFront.helpTip = 'Similar to the Edit menu function';
isFront.value = true;
var isBack = pasteGrp.add('radiobutton', undefined, 'Paste in back');
isBack.helpTip = 'Similar to the Edit menu function';
var isPreserve = optsGrp.add('checkbox', undefined, 'Preserve layers');
isPreserve.helpTip = 'Similar to the Layer option\nPaste Remembers Layers';
isPreserve.value = CFG.isPreserve;
var isSelAfter = optsGrp.add('checkbox', undefined, 'Select pasted');
isSelAfter.value = true;
// Buttons
var btns = win.add('group');
btns.orientation = 'row';
btns.alignChildren = ['fill', 'fill'];
var copyright = btns.add('statictext', undefined, 'Visit Github');
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' });
}
loadSettings();
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold/');
});
win.onShow = function () {
showAbIndex(CFG.tmpLyr, CFG.color);
}
win.onClose = function () {
removeAbIndex(CFG.tmpLyr);
}
cancel.onClick = win.close;
ok.onClick = okClick;
function okClick() {
var doc = app.activeDocument,
actIdx = doc.artboards.getActiveArtboardIndex(),
docSel = selection,
cmdName = isFront.value ? 'pasteFront' : 'pasteBack',
absRange = getArtboardsRange(absInp.text, actIdx),
dupItems = [];
// Activate Paste Remembers Layers
app.preferences.setBooleanPreference('layers/pastePreserve', isPreserve.value);
app.executeMenuCommand('copy');
app.executeMenuCommand('deselectall');
for (var i = 0; i < absRange.length; i++) {
pasteToArtboard(cmdName, absRange[i]);
dupItems.push.apply(dupItems, selection);
}
selection = isSelAfter.value ? dupItems : docSel;
doc.artboards.setActiveArtboardIndex(actIdx);
app.preferences.setBooleanPreference('layers/pastePreserve', CFG.isPreserve);
saveSettings();
win.close();
}
// Save UI options to file
function saveSettings() {
if(!Folder(SETTINGS.folder).exists) Folder(SETTINGS.folder).create();
var $file = new File(SETTINGS.folder + SETTINGS.name);
$file.encoding = 'UTF-8';
$file.open('w');
var pref = {};
pref.range = absInp.text;
pref.isFront = isFront.value;
pref.isSelAfter = isSelAfter.value;
var data = pref.toSource();
$file.write(data);
$file.close();
}
// Load options from file
function loadSettings() {
var $file = File(SETTINGS.folder + SETTINGS.name);
if ($file.exists) {
try {
$file.encoding = 'UTF-8';
$file.open('r');
var json = $file.readln();
var pref = new Function('return ' + json)();
$file.close();
if (typeof pref != 'undefined') {
pref.isFront ? isFront.value = true : isBack.value = true;
absInp.text = pref.range;
isSelAfter.value = pref.isSelAfter;
}
} catch (e) {}
}
}
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('Error\nRun script from Adobe Illustrator');
return false;
}
break;
case /version/g.test(arg):
var rqdVers = parseFloat(arg.split(':')[1]);
if (parseFloat(app.version) < rqdVers) {
alert('Error\nSorry, script only works in Illustrator v.' + rqdVers + ' and later');
return false;
}
break;
case /document/g.test(arg):
if (!documents.length) {
alert('Error\nOpen a document and try again');
return false;
}
break;
case /selection/g.test(arg):
if (!selection.length || selection.typename === 'TextRange') {
alert('Error\nPlease, select at least one object on the artboard');
return false;
}
break;
}
}
return true;
}
// Setup JavaScript Polyfills
function polyfills() {
Array.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
Array.prototype.includes = function (search) {
return this.indexOf(search) !== -1;
};
Array.prototype.indexOf = function (obj, start) {
for (var i = start || 0, j = this.length; i < j; i++) {
if (this[i] === obj) return i;
}
return -1;
};
Array.prototype.filter = function (callback, context) {
arr = [];
for (var i = 0; i < this.length; i++) {
if (callback.call(context, this[i], i, this))
arr.push(this[i]);
}
return arr;
};
}
// Output artboard indexes as text
function showAbIndex(layer, color) {
if (arguments.length == 1 || color == undefined) color = [0, 0, 0];
var doc = activeDocument,
actLayer = doc.activeLayer,
actIdx = doc.artboards.getActiveArtboardIndex(),
tmpLayer;
var idxColor = new RGBColor();
idxColor.red = color[0];
idxColor.green = color[1];
idxColor.blue = color[2];
try {
tmpLayer = doc.layers.getByName(layer);
} catch (e) {
tmpLayer = doc.layers.add();
tmpLayer.name = layer;
}
for (var i = 0, len = doc.artboards.length; i < len; i++) {
doc.artboards.setActiveArtboardIndex(i);
var currAb = doc.artboards[i],
abWidth = currAb.artboardRect[2] - currAb.artboardRect[0],
abHeight = currAb.artboardRect[1] - currAb.artboardRect[3],
label = tmpLayer.textFrames.add(),
labelSize = (abWidth >= abHeight) ? abHeight / 3 : abWidth / 3;
label.contents = i + 1;
// 1296 pt limit for font size in Illustrator
label.textRange.characterAttributes.size = (labelSize > 1296) ? 1296 : labelSize;
label.textRange.characterAttributes.fillColor = idxColor;
label.position = [currAb.artboardRect[0], currAb.artboardRect[1]];
}
doc.artboards.setActiveArtboardIndex(actIdx);
doc.activeLayer = actLayer;
if (parseInt(app.version) >= 16) {
app.executeMenuCommand('preview');
app.executeMenuCommand('preview');
} else {
redraw();
}
}
// Remove temp layer with artboard indexes
function removeAbIndex(layer) {
try {
var layerToRm = activeDocument.layers.getByName(layer);
layerToRm.remove();
} catch (e) {}
}
// Get artboard indexes from string
function getArtboardsRange(str, actIdx) {
var userAbs = [],
docAbs = [];
for (var i = 0; i < activeDocument.artboards.length; i++) {
docAbs.push(i);
}
if (!str.replace(/\s/g, '').length) {
var rmvIdx = docAbs.indexOf(actIdx);
if (rmvIdx !== -1) docAbs.splice(rmvIdx, 1);
return docAbs;
}
str = str.replace(/\s/g, '').replace(/\./g, ',');
var tmp = str.split(',');
tmp.forEach(function (e) {
if (e.match('-') == null) {
userAbs.push(e - 1);
return;
};
var extreme = e.split('-'); // Min & max value in range
for (var j = (extreme[0] - 1); j <= extreme[1] - 1; j++) {
userAbs.push(j);
}
});
return docAbs.filter(function (e) {
return userAbs.includes(e);
});
}
// Paste from clipboard
function pasteToArtboard(cmdName, idx) {
if (arguments.length <= 1) {
cmdName = 'pasteInAllArtboard';
} else {
activeDocument.artboards.setActiveArtboardIndex(idx);
}
switch(cmdName) {
case 'pasteFront':
case 'pasteBack':
case 'pasteInPlace':
case 'pasteInAllArtboard':
app.executeMenuCommand(cmdName);
break;
case 'paste':
default:
app.paste();
break ;
}
}
// Open link in 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 (e) {}