-
Notifications
You must be signed in to change notification settings - Fork 328
/
createRectangleOnArtboard.jsx
161 lines (129 loc) · 4.98 KB
/
createRectangleOnArtboard.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
/*
Program version: Adobe Illustrator CS5+
Name: createRectangleOnArtboard.jsx;
Author: Alexander Ladygin, email: [email protected]
Copyright (c) 2019
www.ladyginpro.ru
*/
var scriptName = 'CROA',
copyright = ' \u00A9 www.ladyginpro.ru',
settingFile = {
name: scriptName + '__setting.json',
folder: Folder.myDocuments + '/LA_AI_Scripts/'
};
if (app.documents.length) {
function getCustomNumbers ($str, items, returnItems) {
var __num = $str.replace(/ /g, '').replace(/[^-0-9^,]/gim,'').split(','),
$maxItems = items.length,
l = __num.length,
arr = [];
function getNumbersBetweenMinMax (min, max) {
var numbers = [];
for (var n = min; n <= max; n++) {
if (n < $maxItems) {
numbers.push(returnItems ? items[n] : n);
}
}
return numbers;
}
for (var j = 0; j < l; j++) {
if (__num[j].indexOf('-') > -1) {
var values = __num[j].split('-'),
min = parseInt(values[0]),
max = parseInt(values[1]);
if (!isNaN(min) && !isNaN(max)) arr = arr.concat(getNumbersBetweenMinMax(min - 1, max - 1));
}
else {
var __val = parseInt(__num[j]);
if (!isNaN(__val) && __val <= $maxItems) {
arr.push(returnItems ? items[__val - 1] : __val - 1);
}
}
}
return arr;
}
function createRectangleOnArtboard (__artNumbers) {
if (__artNumbers && __artNumbers.length) {
var l = __artNumbers.length;
for (var i = 0; i < l; i++) {
var rect = activeDocument.artboards[__artNumbers[i]].artboardRect,
shape = activeDocument.activeLayer.pathItems.rectangle(rect[1], rect[0], rect[2] - rect[0], rect[1] - rect[3]);
shape.fillColor = activeDocument.defaultFillColor;
}
}
}
var win = new Window('dialog', 'Rectangle On Art \u00A9 www.ladyginpro.ru', undefined);
win.orientation = 'column';
win.alignChildren = ['fill', 'fill'];
var panel = win.add('panel', undefined, 'Artboards:');
panel.orientation = 'column';
panel.alignChildren = ['fill', 'fill'];
panel.margins = 20;
var groupRadio = panel.add('group');
groupRadio.orientation = 'row';
var allArtboardsRadio = groupRadio.add('radiobutton', undefined, 'All'),
customArtboardsRadio = groupRadio.add('radiobutton', undefined, 'Custom');
allArtboardsRadio.value = true;
allArtboardsRadio.onClick = function () { customArts.enabled = false; };
customArtboardsRadio.onClick = function () { customArts.enabled = true; };
var customArts = panel.add('edittext', undefined, activeDocument.artboards.getActiveArtboardIndex() + 1);
customArts.enabled = false;
var winButtons = win.add('group');
winButtons.orientation = 'row';
winButtons.alignChildren = ['fill', 'fill'];
var cancel = winButtons.add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () { win.close(); }
var ok = winButtons.add('button', undefined, 'OK');
ok.helpTip = 'Press Enter to Run';
ok.onClick = startAction;
ok.active = true;
function startAction() {
var __arts = [];
__arts = getCustomNumbers((customArtboardsRadio.value ? customArts.text : ('1-' + activeDocument.artboards.length)), activeDocument.artboards);
createRectangleOnArtboard(__arts);
win.close();
}
selection = null;
function saveSettings() {
var $file = new File(settingFile.folder + settingFile.name),
data = [
allArtboardsRadio.value,
customArtboardsRadio.value
].toString() + '\n' + customArts.text;
$file.open('w');
$file.write(data);
$file.close();
}
function loadSettings() {
var $file = File(settingFile.folder + settingFile.name);
if ($file.exists) {
try {
$file.open('r');
var data = $file.read().split('\n'),
$main = data[0].split(','),
$arts = data[1];
allArtboardsRadio.value = ($main[0] === 'true');
customArtboardsRadio.value = ($main[1] === 'true');
customArts.text = $arts;
customArts.enabled = customArtboardsRadio.value;
} catch (e) {}
$file.close();
}
}
function checkSettingFolder() {
var $folder = new Folder(settingFile.folder);
if (!$folder.exists) $folder.create();
}
win.onClose = function () {
saveSettings();
return true;
}
checkSettingFolder();
loadSettings();
win.center();
win.show();
}
else {
throw new Error('Create document!');
}