-
Notifications
You must be signed in to change notification settings - Fork 328
/
replaceItems.jsx
284 lines (232 loc) · 9.7 KB
/
replaceItems.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
/*
Author: Alexander Ladygin ([email protected])
Program version: Adobe Illustrator CC+
Name: replaceItems.jsx;
Copyright (c) 2018
www.ladyginpro.ru
*/
var scriptName = 'ReplaceItems',
settingFile = {
name: scriptName + '__setting.json',
folder: Folder.myDocuments + '/LA_AI_Scripts/'
};
var win = new Window('dialog', scriptName + ' \u00A9 www.ladyginpro.ru');
win.orientation = 'column';
win.alignChildren = ['fill', 'fill'];
with (win.add('group')) {
orientation = 'row';
var panel = add('panel', undefined, 'What to replace?');
panel.orientation = 'column';
panel.alignChildren = ['fill', 'fill'];
panel.margins = [20, 30, 20, 20];
var bufferRadio = panel.add('radiobutton', undefined, 'Object in buffer'),
currentRadio = panel.add('radiobutton', undefined, 'Top object'),
groupSuccessively = panel.add('radiobutton', undefined, 'All in group (successively)'),
randomRadio = panel.add('radiobutton', undefined, 'All in group (random)'),
groupValue = panel.add('group'),
randomValue = groupValue.add('edittext', undefined, '100'),
randomValueUnit = groupValue.add('statictext', undefined, '%'),
elementsInGroupCheckbox = panel.add('checkbox', undefined, 'Replace items in a group?');
groupValue.orientation = 'row';
groupValue.margins = 0;
groupValue.alignChildren = ['fill', 'fill'];
randomValue.minimumSize = [140, undefined];
var panelCheckboxes = add('panel');
panelCheckboxes.orientation = 'column';
panelCheckboxes.alignChildren = ['fill', 'fill'];
panelCheckboxes.margins = 20;
var fitInSizeCheckbox = panelCheckboxes.add('checkbox', undefined, 'Fit to element size');
copyWHCheckbox = panelCheckboxes.add('checkbox', undefined, 'Copy Width & Height');
saveOriginalCheckbox = panelCheckboxes.add('checkbox', undefined, 'Save original element'),
copyColorsCheckbox = panelCheckboxes.add('checkbox', undefined, 'Copy colors from element'),
randomRotateCheckbox = panelCheckboxes.add('checkbox', undefined, 'Random element rotation'),
symbolByRPCheckbox = panelCheckboxes.add('checkbox', [0, 0, 100, 40], 'Align symbols by\nregistration point');
bufferRadio.value = true;
fitInSizeCheckbox.value = false;
copyWHCheckbox.value = false;
saveOriginalCheckbox.value = false;
}
var winButtons = win.add('group');
winButtons.alignChildren = ['fill', 'fill'];
winButtons.margins = [0, 0, 0, 0];
var cancel = winButtons.add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () { win.close(); }
var ok = winButtons.add('button', [0, 0, 100, 30], 'OK');
ok.helpTip = 'Press Enter to Run';
ok.onClick = startAction;
ok.active = true;
var progressBar = win.add('progressbar', [0, 0, 110, 5]),
progressBarCounter = 100;
progressBar.value = 0;
progressBar.minvalue = 0;
progressBar.maxvalue = progressBarCounter;
copyWHCheckbox.onClick = function (e) {
groupValue.enabled = !copyWHCheckbox.value;
fitInSizeCheckbox.enabled = !this.value;
}
fitInSizeCheckbox.onClick = function (e) {
copyWHCheckbox.enabled = !this.value;
}
function randomRotation (item) {
item.rotate(Math.floor(Math.random() * 360), true, true, true, true, Transformation.CENTER);
}
function setFillColor (items, color) {
if (color) {
var i = items.length;
if (i) while (i--) {
if (items[i].typename === 'GroupItem') {
setFillColor(items[i].pageItems, color);
}
else if (items[i].typename === 'CompoundPathItem') {
if (items[i].pathItems.length) items[i].pathItems[0].fillColor = color;
}
else if (items[i].typename === 'PathItem') {
items[i].fillColor = color;
}
}
}
}
function getFillColor (items) {
var i = items.length, gc;
if (i) while (i--) {
if (items[i].typename === 'GroupItem' && (gc = getFillColor(items[i].pageItems))) return gc;
else if (items[i].typename === 'CompoundPathItem' && items[i].pathItems.length) return items[i].pathItems[0].fillColor;
else if (items[i].typename === 'PathItem') return items[i].fillColor;
}
}
function getSymbolPositionByRegistrationPoint (item) {
var bakupSymbol = item.symbol,
newSymbol = activeDocument.symbols.add(item, SymbolRegistrationPoint.SYMBOLTOPLEFTPOINT);
// replace symbol
item.symbol = newSymbol;
// set position
var position = [
item.left,
item.top
];
// restore symbol
item.symbol = bakupSymbol;
newSymbol.remove();
return position;
}
function startAction() {
if (selection.length) {
panel.enabled = groupValue.enabled = panelCheckboxes.enabled = ok.enabled = cancel.enabled = false;
var __ratio = !isNaN(parseFloat(randomValue.text)) ? parseFloat(randomValue.text) / 100 : 1,
items = (!elementsInGroupCheckbox.value ? selection : selection[selection.length - 1].pageItems),
nodes = (currentRadio.value ? selection[0] : (bufferRadio.value ? [] : selection[0].pageItems)),
length = nodes.length,
i = items.length,
j = 0;
progressBarCounter = progressBar.maxvalue / i;
if (bufferRadio.value) {
selection = null;
app.paste();
nodes = selection[0];
selection = null;
}
function getNode (__index) {
return ((currentRadio.value || bufferRadio.value) ? nodes : nodes[ typeof __index === 'number' ? __index : Math.floor(Math.random() * length) ]);
}
while (i--) {
if (!bufferRadio.value && !i) break;
if (j >= nodes.length) j = 0;
var item = items[i],
node = getNode(groupSuccessively.value ? j : undefined).duplicate(item, ElementPlacement.PLACEBEFORE),
__fn = 'height',
__fnReverse = 'width';
j++;
if (node.height <= node.width) {
__fn = 'width';
__fnReverse = 'height';
}
if (randomRotateCheckbox.value) randomRotation(node);
if (!copyWHCheckbox.value) {
var __size = (item.height >= item.width ? item.width : item.height) * __ratio,
precent = __size * 100 / node[__fn] / 100;
if (fitInSizeCheckbox.value) {
node[__fn] = __size;
node[__fnReverse] *= precent;
}
}
else {
node.width = item.width;
node.height = item.height;
}
node.left = item.left - (node.width - item.width) / 2;
node.top = item.top + (node.height - item.height) / 2;
if (symbolByRPCheckbox.value && node.typename === 'SymbolItem') {
var __pos = getSymbolPositionByRegistrationPoint(node);
node.left += (item.left + item.width / 2) - __pos[0];
node.top += (item.top - item.height / 2) - __pos[1];
}
if (copyColorsCheckbox.value) {
setFillColor([node], getFillColor([item]));
}
if (!saveOriginalCheckbox.value) item.remove();
progressBar.value += progressBarCounter;
win.update();
}
if (bufferRadio.value) nodes.remove();
}
win.close();
}
function saveSettings() {
var $file = new File(settingFile.folder + settingFile.name),
data = [
bufferRadio.value,
currentRadio.value,
randomRadio.value,
elementsInGroupCheckbox.value,
copyWHCheckbox.value,
saveOriginalCheckbox.value,
copyColorsCheckbox.value,
randomRotateCheckbox.value,
symbolByRPCheckbox.value,
randomValue.text,
groupSuccessively.value,
fitInSizeCheckbox.value
].toString();
$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(',');
bufferRadio.value = ($main[0] === 'true');
currentRadio.value = ($main[1] === 'true');
randomRadio.value = ($main[2] === 'true');
elementsInGroupCheckbox.value = ($main[3] === 'true');
copyWHCheckbox.value = ($main[4] === 'true');
saveOriginalCheckbox.value = ($main[5] === 'true');
copyColorsCheckbox.value = ($main[6] === 'true');
randomRotateCheckbox.value = ($main[7] === 'true');
symbolByRPCheckbox.value = ($main[8] === 'true');
randomValue.text = $main[9];
groupSuccessively.value = ($main[10] === 'true');
fitInSizeCheckbox.value = ($main[11] === 'true');
groupValue.enabled = !copyWHCheckbox.value;
fitInSizeCheckbox.enabled = !copyWHCheckbox.value;
copyWHCheckbox.enabled = !fitInSizeCheckbox.value;
} catch (e) {}
$file.close();
}
}
win.onClose = function() {
saveSettings();
return true;
}
function checkSettingFolder() {
var $folder = new Folder(settingFile.folder);
if (!$folder.exists) $folder.create();
}
checkSettingFolder();
loadSettings();
win.center();
win.show();