-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathDuplicateArtboardsLight.jsx
642 lines (568 loc) · 20.1 KB
/
DuplicateArtboardsLight.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
DuplicateArtboardsLight.jsx for Adobe Illustrator
Description: Script for copying the selected Artboard with his artwork
Requirements: Adobe Illustrator CS6 and later
Date: October, 2020
Modification date: February, 2024
Author: Sergey Osokin, email: [email protected]
Installation: https://github.com/creold/illustrator-scripts#how-to-run-scripts
Release notes:
0.4.4 Removed input activation on Windows OS below CC v26.4
0.4.3 Added new units API for CC 2023 v27.1.1
0.4.2 Added size correction in large canvas mode
0.4.1 Fixed input activation in Windows OS. Removed RU localization
0.4 Added more units (yards, meters, etc.) support if the document is saved
0.3 Fixed copying all objects into one layer
0.2.2 Performance optimization
0.2.1 Fixed script didn't run if the layers were locked
0.2 Fixed: bounds checking of the Illustrator canvas; position of the first copy
0.1 Initial version
Buy Pro version: https://buymeacoffee.com/aiscripts/extras
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
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
function main() {
var SCRIPT = {
name: 'Duplicate Atboards Light',
version: 'v0.4.4'
},
CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os),
units: getUnits(), // Active document units
copies: 0, // Default amount of copies
spacing: 20, // Default spacing between copies (doc units)
minSpacing: 0, // The spacing can't be < 0
tmpLyr: 'FOR_AB_COORD',
suffix: ' ', // Default suffix for copies names
limit: 2500, // The amount of objects, when the script can run slowly
abs: 10, // When the number of copies >, full-screen mode is enabled
cnvs: 16383, // Illustrator canvas max size, px
lKey: '%isLocked',
hKey: '%isHidden',
uiField: [0, 0, 60, 30],
uiTitle: [0, 0, 120, 30],
uiOpacity: .97 // UI window opacity. Range 0-1
},
SETTINGS = {
name: SCRIPT.name.replace(/\s/g, '_') + '_data.json',
folder: Folder.myDocuments + '/Adobe Scripts/'
};
if (CFG.aiVers < 16) {
alert('Error\nSorry, script only works in Illustrator CS6 and later');
return;
}
if (!documents.length) {
alert('Error\nOpen a document and try again');
return;
}
var doc = activeDocument,
maxCopies = ((CFG.aiVers >= 22) ? 1000 : 100) - doc.artboards.length, // Artboards limit
curAbIdx = doc.artboards.getActiveArtboardIndex(),
absArr = [],
copies = spacing = 0;
// Scale factor for Large Canvas mode
CFG.sf = doc.scaleFactor ? doc.scaleFactor : 1;
// Collect artboards names for dropdown menu
for (var i = 0, aLen = doc.artboards.length; i < aLen; i++) {
absArr.push((i + 1) + ': ' + doc.artboards[i].name);
}
// Main Window
var dialog = new Window('dialog', SCRIPT.name + ' ' + SCRIPT.version);
dialog.orientation = 'column';
dialog.alignChildren = ['fill', 'center'];
dialog.opacity = CFG.uiOpacity;
// Input fields
var abGroup = dialog.add('group');
abGroup.orientation = 'column';
abGroup.alignChildren = ['fill', 'top'];
abGroup.add('statictext', undefined, 'Select artboard');
var abIdx = abGroup.add('dropdownlist', CFG.uiTitle, absArr);
abIdx.selection = curAbIdx;
var fieldGroup = dialog.add('group');
fieldGroup.orientation = 'row';
fieldGroup.alignChildren = ['fill', 'center'];
var titlesGroup = fieldGroup.add('group');
titlesGroup.orientation = 'column';
var copiesTitle = titlesGroup.add('statictext', CFG.uiTitle, 'Copies (max ');
titlesGroup.add('statictext', CFG.uiTitle, 'Spacing, ' + CFG.units);
var inputsGroup = fieldGroup.add('group');
inputsGroup.orientation = 'column';
var copiesVal = inputsGroup.add('edittext', CFG.uiField, CFG.copies);
var spacingVal = inputsGroup.add('edittext', CFG.uiField, CFG.spacing);
if (CFG.isMac || CFG.aiVers >= 26.4 || CFG.aiVers <= 17) {
copiesVal.active = true;
}
if (doc.pageItems.length > CFG.limit) {
var warning = dialog.add('statictext', undefined, 'The document has over ' + CFG.limit + ' objects. The script can run slowly', { multiline: true });
}
// Buttons
var btnsGroup = dialog.add('group');
btnsGroup.orientation = 'row';
btnsGroup.alignChildren = ['fill', 'center'];
var cancel = btnsGroup.add('button', undefined, 'Cancel', { name: 'cancel' });
var ok = btnsGroup.add('button', undefined, 'Ok', { name: 'ok' });
var copyright = dialog.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
loadSettings();
spacing = convertUnits( strToAbsNum(spacingVal.text, CFG.minSpacing), CFG.units, 'px' );
curAbIdx = doc.artboards.getActiveArtboardIndex();
var abCoord = getArtboardCoordinates(curAbIdx, CFG.tmpLyr);
var overCnvsSize = isOverCnvsBounds(abCoord, maxCopies, spacing, CFG.cnvs);
copiesTitle.text = 'Copies (max ' + overCnvsSize.copies + ')';
if (strToAbsNum(copiesVal.text, CFG.copies) > overCnvsSize.copies) {
copiesVal.text = overCnvsSize.copies;
}
shiftInputNumValue(copiesVal, CFG.copies);
shiftInputNumValue(spacingVal, CFG.minSpacing);
// Change listeners
spacingVal.onChange = function () {
recalcCopies();
this.text = strToAbsNum(this.text, CFG.spacing);
}
copiesVal.onChange = function () {
recalcCopies();
this.text = strToAbsNum(this.text, CFG.copies);
}
copiesVal.onChanging = spacingVal.onChanging = recalcCopies;
abIdx.onChange = function() {
doc.artboards.setActiveArtboardIndex(abIdx.selection.index);
redraw();
recalcCopies();
}
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
cancel.onClick = dialog.close;
// Remove temp layer
dialog.onClose = function() {
try {
doc.layers.getByName(CFG.tmpLyr).remove();
} catch (e) {}
}
ok.onClick = okClick;
function okClick() {
copies = copiesVal.text = Math.round( strToAbsNum(copiesVal.text, CFG.copies) );
spacing = spacingVal.text = strToAbsNum(spacingVal.text, CFG.spacing);
spacing = convertUnits(spacing, CFG.units, 'px') / CFG.sf;
var userView = doc.views[0].screenMode;
if (copies == 0) {
dialog.close();
} else if (copies > CFG.abs) {
doc.views[0].screenMode = ScreenMode.FULLSCREEN;
}
selection = null;
redraw();
unlockLayers(doc.layers);
removeNote(doc.layers, CFG.lKey, CFG.hKey); // Сlear Note after previous run
saveItemsState(doc.layers, CFG.lKey, CFG.hKey);
// Copy Artwork
doc.selectObjectsOnActiveArtboard();
var abItems = selection;
try {
for (var i = 0; i < copies; i++) {
suffix = CFG.suffix + fillZero((i + 1), copies.toString().length);
duplicateArtboard(curAbIdx, abItems, spacing, suffix, i);
}
} catch (e) {}
// Restore locked & hidden pageItems
restoreItemsState(doc.layers, CFG.lKey, CFG.hKey);
selection = null;
doc.views[0].screenMode = userView;
saveSettings();
app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
dialog.close();
}
dialog.center();
dialog.show();
/**
* Recalculate the maximum amount of copies at a given spacing
*/
function recalcCopies() {
spacing = convertUnits( strToAbsNum(spacingVal.text, CFG.minSpacing), CFG.units, 'px' ) / CFG.sf;
curAbIdx = doc.artboards.getActiveArtboardIndex();
abCoord = getArtboardCoordinates(curAbIdx, CFG.tmpLyr);
overCnvsSize = isOverCnvsBounds(abCoord, maxCopies, spacing, CFG.cnvs);
copiesTitle.text = 'Copies (max ' + overCnvsSize.copies + ')';
if (strToAbsNum(copiesVal.text, CFG.copies) > overCnvsSize.copies) {
copiesVal.text = overCnvsSize.copies;
}
if (strToAbsNum(copiesVal.text, CFG.copies) < 0) {
copiesVal.text = 0;
}
}
/**
* Use Up / Down arrow keys (+ Shift) for change value
* @param {object} item - input text field
* @param {number} min - minimal input value
*/
function shiftInputNumValue(item, min) {
item.addEventListener('keydown', function (kd) {
var step;
ScriptUI.environment.keyboardState['shiftKey'] ? step = 10 : step = 1;
if (kd.keyName == 'Down') {
this.text = strToAbsNum(this.text, min) - step;
if (this.text * 1 < min) this.text = min;
recalcCopies();
kd.preventDefault();
}
if (kd.keyName == 'Up') {
this.text = strToAbsNum(this.text, min) + step;
recalcCopies();
kd.preventDefault();
}
});
}
/**
* Save input data 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.copies = copiesVal.text;
pref.spacing = spacingVal.text;
var data = pref.toSource();
$file.write(data);
$file.close();
}
/**
* Load input data 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') {
copiesVal.text = pref.copies;
spacingVal.text = pref.spacing;
}
} catch (e) {}
}
}
}
/**
* Unlock all Layers & Sublayers
* @param {Object} _layers - Layers collection
*/
function unlockLayers(_layers) {
for (var i = 0, len = _layers.length; i < len; i++) {
if (_layers[i].locked) _layers[i].locked = false;
if (_layers[i].layers.length) unlockLayers(_layers[i].layers);
}
}
/**
* Remove keyword from Note in Attributes panel
* @param {Object} _layers - Layers collection
* @param {string} lKey - Keyword for locked items
* @param {string} hKey - Keyword for hidden items
*/
function removeNote(_layers, lKey, hKey) {
var regexp = new RegExp(lKey + '|' + hKey, 'gi');
for (var i = 0, len = _layers.length; i < len; i++) {
var curLayer = _layers[i],
allItems = [];
if (curLayer.layers.length > 0) {
removeNote(curLayer.layers, lKey, hKey);
}
try {
getItems(curLayer.pageItems, allItems);
for (var j = 0, iLen = allItems.length; j < iLen; j++) {
var curItem = allItems[j];
curItem.note = curItem.note.replace(regexp, '');
}
} catch (e) {}
}
}
/**
* Save information about locked & hidden pageItems & layers
* @param {Object} _layers - Layers collection
* @param {string} lKey - Keyword for locked items
* @param {string} hKey - Keyword for hidden items
*/
function saveItemsState(_layers, lKey, hKey) {
var allItems = [];
for (var i = 0, len = _layers.length; i < len; i++) {
var curLayer = _layers[i];
if (curLayer.layers.length > 0) {
saveItemsState(curLayer.layers, lKey, hKey);
}
getItems(curLayer.pageItems, allItems);
for (var j = 0, iLen = allItems.length; j < iLen; j++) {
var curItem = allItems[j];
if (curItem.locked) {
curItem.locked = false;
curItem.note += lKey;
}
if (curItem.hidden) {
curItem.hidden = false;
curItem.note += hKey;
}
}
}
redraw();
}
/**
* Restoring locked & hidden pageItems & layers
* @param {Object} _layers - Layers collection
* @param {string} lKey - Keyword for locked items
* @param {string} hKey - Keyword for hidden items
*/
function restoreItemsState(_layers, lKey, hKey) {
var allItems = [],
regexp = new RegExp(lKey + '|' + hKey, 'gi');
for (var i = 0, len = _layers.length; i < len; i++) {
var curLayer = _layers[i];
if (curLayer.layers.length > 0) {
restoreItemsState(curLayer.layers, lKey, hKey);
}
getItems(curLayer.pageItems, allItems);
for (var j = 0, iLen = allItems.length; j < iLen; j++) {
var curItem = allItems[j];
if (curItem.note.match(lKey) != null) {
curItem.note = curItem.note.replace(regexp, '');
curItem.locked = true;
}
if (curItem.note.match(hKey) != null) {
curItem.note = curItem.note.replace(regexp, '');
curItem.hidden = true;
}
}
}
}
/**
* Collect items
* @param {(Object|Array)} obj - Items collection
* @param {Array} arr - Output array with childrens
*/
function getItems(obj, arr) {
for (var i = 0, len = obj.length; i < len; i++) {
var curItem = obj[i];
try {
switch (curItem.typename) {
case 'GroupItem':
arr.push(curItem);
getItems(curItem.pageItems, arr);
break;
default:
arr.push(curItem);
break;
}
} catch (e) {}
}
}
/**
* Add zero to the file name before the indexes are less then size
* @param {number} number - Copy number
* @param {number} size - Length of the amount of copies
* @return {string} Copy number with pre-filled zeros
*/
function fillZero(number, size) {
var str = '000000000' + number;
return str.slice(str.length - size);
}
/**
* Trick with temp pathItem to get the absolute coordinate of the artboard. Thanks to @moodyallen
* @param {number} abIdx - Current artboard index
* @return {Object} Absolute coordinates of the artboard
*/
function getArtboardCoordinates(abIdx, lyrName) {
var doc = activeDocument,
thisAbRect = doc.artboards[abIdx].artboardRect, // The selected artboard size
tmpLayer;
try {
tmpLayer = doc.layers.getByName(lyrName);
} catch (e) {
tmpLayer = doc.layers.add();
tmpLayer.name = lyrName;
}
var fakePath = tmpLayer.pathItems.add();
var cnvsDelta = 1 + ((fakePath.position[0] * 2 - 16384) - (fakePath.position[1] * 2 + 16384)) / 2;
var cnvsTempPath = tmpLayer.pathItems.rectangle(fakePath.position[0] - cnvsDelta, fakePath.position[1] + cnvsDelta, 300, 300);
cnvsTempPath.filled = false;
cnvsTempPath.stroked = false;
// Create a rectangle with the same size as the artboard
var top = thisAbRect[1],
left = thisAbRect[0],
width = thisAbRect[2] - thisAbRect[0],
height = thisAbRect[1] - thisAbRect[3];
var abTempPath = tmpLayer.pathItems.rectangle(top, left, width, height);
abTempPath.stroked = false;
abTempPath.filled = false;
// Use the X, Y coordinates of cnvsTempPath and abTempPath to get the absolute coordinate
var absLeft = Math.floor(abTempPath.position[0] - cnvsTempPath.position[0]),
absTop = Math.floor(cnvsTempPath.position[1] - abTempPath.position[1]),
absBottom = absTop + height,
absRight = absLeft + width;
fakePath.remove();
abTempPath.remove();
cnvsTempPath.remove();
redraw();
return { 'left': absLeft, 'right': absRight, 'top': absTop, 'bottom': absBottom };
}
/**
* Find out if the amount of copies over the canvas width
* @param {Object} coord - Coordinates of the selected artboard
* @param {number} copies - Amount of copies
* @param {number} spacing - Distance between copies
* @return {Object} Information about the extreme possible artboard
*/
function isOverCnvsBounds(coord, copies, spacing, max) {
var lastAbRight = coord.right + (spacing + coord.right - coord.left) * copies,
tempEdge = lastAbRight;
// Get a safe amount of copies
for (var i = copies; i >= 0; i--) {
if (tempEdge <= max) break;
tempEdge = tempEdge - (spacing + coord.right - coord.left);
}
return { 'answer': lastAbRight > max, 'copies': i };
}
/**
* Duplicate the selected artboard. Based on the idea of @Silly-V
* @param {number} thisAbIdx - Current artboard index
* @param {Object} items - Collection of items on the artboard
* @param {number} spacing - Distance between copies
* @param {string} suffix - Copy name suffix
* @param {number} counter - Current copy number
*/
function duplicateArtboard(thisAbIdx, items, spacing, suffix, counter) {
var doc = activeDocument,
thisAb = doc.artboards[thisAbIdx],
thisAbRect = thisAb.artboardRect,
idx = doc.artboards.length - 1,
lastAb = doc.artboards[idx],
lastAbRect = lastAb.artboardRect,
abWidth = thisAbRect[2] - thisAbRect[0];
var newAb = doc.artboards.add(thisAbRect);
if (counter === 0) {
newAb.artboardRect = [
thisAbRect[2] + spacing,
thisAbRect[1],
thisAbRect[2] + spacing + abWidth,
thisAbRect[3]
];
} else {
newAb.artboardRect = [
lastAbRect[2] + spacing,
lastAbRect[1],
lastAbRect[2] + spacing + abWidth,
lastAbRect[3]
];
}
newAb.name = thisAb.name + suffix;
var docCoordSystem = CoordinateSystem.DOCUMENTCOORDINATESYSTEM,
abCoordSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM,
isDocCoords = app.coordinateSystem == docCoordSystem,
dupArr = getDuplicates(items);
// Move copied items to the new artboard
for (var i = 0, dLen = dupArr.length; i < dLen; i++) {
var pos = isDocCoords ? dupArr[i].position : doc.convertCoordinate(dupArr[i].position, docCoordSystem, abCoordSystem);
dupArr[i].position = [pos[0] + (abWidth + spacing) * (counter + 1), pos[1]];
}
}
/**
* Duplicate all items
* @param {Object} collection - Selected items on active artboard
* @return {Array} arr - Duplicated items
*/
function getDuplicates(collection) {
var arr = [];
for (var i = 0, len = collection.length; i < len; i++) {
arr.push(collection[i].duplicate());
}
return arr;
}
/**
* Get active document ruler units
* @return {string} Shortened units
*/
function getUnits() {
if (!documents.length) return '';
var key = activeDocument.rulerUnits.toString().replace('RulerUnits.', '');
switch (key) {
case 'Pixels': return 'px';
case 'Points': return 'pt';
case 'Picas': return 'pc';
case 'Inches': return 'in';
case 'Millimeters': return 'mm';
case 'Centimeters': return 'cm';
// Added in CC 2023 v27.1.1
case 'Meters': return 'm';
case 'Feet': return 'ft';
case 'FeetInches': return 'ft';
case 'Yards': return 'yd';
// Parse new units in CC 2020-2023 if a document is saved
case 'Unknown':
var xmp = activeDocument.XMPString;
if (/stDim:unit/i.test(xmp)) {
var units = /<stDim:unit>(.*?)<\/stDim:unit>/g.exec(xmp)[1];
if (units == 'Meters') return 'm';
if (units == 'Feet') return 'ft';
if (units == 'FeetInches') return 'ft';
if (units == 'Yards') return 'yd';
return 'px';
}
break;
default: return 'px';
}
}
/**
* Convert units of measurement
* @param {string} value - Numeric data
* @param {string} curUnits - Document units
* @param {string} newUnits - Final units
* @return {number} Converted value
*/
function convertUnits(value, curUnits, newUnits) {
return UnitValue(value, curUnits).as(newUnits);
}
/**
* Convert string to absolute number
* @param {string} str - Input data
* @param {number} def - Default value if the string don't contain digits
* @return {number}
*/
function strToAbsNum(str, def) {
if (arguments.length == 1 || def == undefined) def = 1;
str = str.replace(/,/g, '.').replace(/[^\d.]/g, '');
str = str.split('.');
str = str[0] ? str[0] + '.' + str.slice(1).join('') : '';
if (isNaN(str) || !str.length) return parseFloat(def);
else return parseFloat(str);
}
/**
* Open link in browser
* @param {string} url - Website adress
*/
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 (e) {}