-
Notifications
You must be signed in to change notification settings - Fork 328
/
optimizero.jsx
342 lines (305 loc) · 14.3 KB
/
optimizero.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
/*
Author: Alexander Ladygin (i@ladygin.pro)
Program version: Adobe Illustrator CS6+
Name: optimizero.jsx;
Copyright (c) 2018
www.ladyginpro.ru
*/
var scriptName = 'Optimizero',
copyright = ' \u00A9 www.ladyginpro.ru',
settingFile = {
name: scriptName + '__setting.json',
folder: Folder.myDocuments + '/LA_AI_Scripts/'
};
$.errorMessage = function (err) {alert(err + '\n' + err.line);};
$.command = function (command) {try {app.executeMenuCommand(command);return $;}catch (e) {$.errorMessage('Command is " ' + command + ' " error: ' + e);}};
Object.prototype.extend = function (userObject, deep) {try {for (var key in userObject) {if (this.hasOwnProperty(key)) {if (deep&& this[key] instanceof Object&& !(this[key] instanceof Array)&& userObject[key] instanceof Object&& !(userObject[key] instanceof Array)) {this[key].extend(userObject[key], deep);}else this[key] = userObject[key];}}return this;}catch (e) {$.errorMessage('Object.extend() - error: ' + e);}};
Object.prototype.getAllParents = function () {function get(obj) {var arr = [];if (obj.parent.typename !== 'Application') {for (var i = 0; ; i++) {arr.push(obj.parent);if (obj.parent.parent.typename === 'Application') {return arr;}else {obj = obj.parent;}if (i === 1000) {break;}}}else {return obj;}}return get(this);};
Object.prototype.removeItemsWithArray = function (items) {var obj = [];for (var i = 0; i < this.length; i++) {if (!check(this[i])) {obj.push(this[i]);}}function check(e) {for (var j = 0; j < items.length; j++) {if (e === items[j]) {return true;}}return false;}return obj;};
Object.prototype.emptyLayers = function () {var arr = [];function check(layers) {var obj = [];for (var i = 0; i < layers.length; i++) {var subLayers = layers[i].subLayers(),emptySubLayers = layers[i].emptySubLayers(),count = subLayers.removeItemsWithArray(emptySubLayers).length;if (!count && !layers[i].pageItems.length) {obj = obj.concat(layers[i]);}}return obj;}var doc = this.typename === 'Document' ? [this] : this;for (var i = 0; i < doc.length; i++) {arr = arr.concat(check(doc[i].layers));}return arr;};
Object.prototype.subLayers = function (level) {var arr = [], count = 0;if (level !== undefined) {level = level - 1;}function __subLayers(layer) {var obj = [], sub = layer.layers;for (var j = 0; j < sub.length; j++) {obj = obj.concat(sub[j]);if ((level === undefined) || (sub[j].layers.length > 0 && count < level)) {obj = obj.concat(__subLayers(sub[j], count++));count--;}}return obj;}var obj = this.typename === 'Document' ? [this] : this;for (var j = 0; j < obj.length; j++) {arr = arr.concat(__subLayers(obj[j]));}return arr;};
Object.prototype.emptySubLayers = function (level) {var arr = [], obj = this.typename === 'Document' ? [this] : this;function process(sub) {var sub_arr = [];sub = sub.subLayers(level).reverse();for (var i = 0; i < sub.length; i++) {if (sub[i].pageItems.length > 0) {var parents = sub[i].pageItems[0].getAllParents();parents.pop();sub_arr = sub_arr.concat(parents);}}return sub.removeItemsWithArray(sub_arr).reverse();}for (var i = 0; i < obj.length; i++) {arr = arr.concat(process(obj[i]));}return arr;};
Array.prototype.__remove = function(){var i = this.length; if (i > 0) while (i--) this[i].remove();}
function optimizero (userOptions) {
var options = {
hidden: false,
ghost: false,
rasterEmbed: false,
symbolBreakLink: false,
expandStyle: false,
removeEmptyLayers: false,
compoundFix: false,
locked: 'skip',
/*
skip
unlock
remove
*/
guides: 'skip',
/*
skip
newlayer
remove
*/
openPaths: 'skip',
/*
skip
join
remove
*/
}.extend(userOptions || {});
options.guides = (typeof options.guides === 'string' ? options.guides.toLowerCase() : 'newlayer');
if (options.guides !== 'skip') {
var layerGuides = activeDocument.layers.add(),
guidesCollection = [];
layerGuides.name = scriptName + '_guides';
}
function compoundFixAction ($items) {
function __ungroup (__items) {
var l = __items.length;
for (var i = 0; i < l; i++) {
if (__items[i].typename === 'GroupItem') {
var j = __items[i].pageItems.length;
while (j--) { __items[i].pageItems[0].moveBefore(__items[i]); }
__items[i].remove();
}
}
}
function compoundFix (item) {
selection = [item];
app.executeMenuCommand('noCompoundPath');
__ungroup(selection);
app.executeMenuCommand('compoundPath');
selection = null;
}
function compoundPathItemsNormalize (items) {
var i = items.length;
while (i--) {
if (items[i].typename === 'GroupItem') {
compoundPathItemsNormalize(items[i].pageItems);
}
else if (items[i].typename === 'CompoundPathItem') {
compoundFix(items[i]);
}
}
}
compoundPathItemsNormalize($items);
}
function layersOptimize (layers) {
var n = layers.length;
if (n > 0) while (n--) {
if (options.hidden && !layers[n].visible) {
layers[n].visible = true;
layers[n].remove();
continue;
}
else if (options.locked !== 'skip' && layers[n].locked) {
layers[n].locked = false;
if (!(options.locked !== 'remove')) {
layers[n].remove();
continue;
}
}
layersOptimize(layers[n].layers);
}
}
function itemsOptimize (items, $progress) {
try {
var i = items.length;
if (!$progress) progressBarCounter = progressBar.maxvalue * 0.8 / i;
while (i--) {
if (options.guides !== 'skip' && items[i].guides) {
guidesCollection.push(items[i]);
}
else if (options.hidden && items[i].hidden) {
items[i].hidden = true;
items[i].remove();
}
else if (options.locked !== 'skip' && items[i].locked) {
if (options.locked !== 'remove') items[i].locked = false;
else items[i].remove();
}
else if (options.ghost && items[i].typename.indexOf('PathItem') > -1) {
try {
var __items = (items[i].typename === 'CompoundPathItem' ? items[i].pathItems[0] : items[i]);
if (!__items.stroked && !__items.filled && !__items.guides &&
!(__items.clipping && __items.parent.clipped)
) {
items[i].remove();
}
else if (!items[i].closed && items[i].pathPoints && items[i].pathPoints.length > 2) {
switch (options.openPaths.toLowerCase().replace(/ /g, '')) {
case 'join' : items[i].closed = true; break;
case 'remove' : items[i].remove(); break;
}
}
}catch (e) {}
}
else if (items[i].typename === 'GroupItems') {
itemsOptimize(items[i].pageItems, true);
}
if (!$progress) { progressBar.value += progressBarCounter; win.update(); }
}
}
catch(err) {
$.errorMessage(err);
}
}
function optimizeRasters() {
if (options.rasterEmbed) {
var i = activeDocument.placedItems.length;
if (i > 0) while (i--) {
activeDocument.placedItems[i].embed();
}
}
}
function optimizeSymbols() {
if (options.symbolBreakLink) {
var i = activeDocument.symbolItems.length;
if (i > 0) while (i--) {
activeDocument.symbolItems[i].breakLink();
}
}
}
selection = null;
if (options.expandStyle) $.command('selectall').command('expandStyle');
if (options.compoundFix) compoundFixAction(activeDocument.compoundPathItems);
selection = null;
if (options.locked !== 'skip' || options.hidden) {
layersOptimize(activeDocument.layers);
}
if (options.guides !== 'skip' || options.locked !== 'skip' || options.ghost || options.hidden) {
itemsOptimize(activeDocument.pageItems);
}
optimizeRasters();
optimizeSymbols();
if (options.guides !== 'skip') {
var i = guidesCollection.length;
if (i > 0) while (i--) { options.guides !== 'remove' ? guidesCollection[i].moveToBeginning(layerGuides) : guidesCollection[i].remove(); }
}
if (options.emptyLayers) {
activeDocument.emptyLayers().__remove();
progressBar.value += progressBar.maxvalue * 0.2;
win.update();
}
selection = null;
}
var win = new Window('dialog', scriptName + copyright);
win.alignChildren = 'fill';
with (globalGroup = win.add('group')) {
orientation = 'column';
alignChildren = 'fill';
with (panel = add('panel')) {
alignChildren = 'fill';
var checkHidden = add('checkbox', undefined, 'Remove Hidden items');
checkHidden.value = true;
var checkGhost = add('checkbox', undefined, 'Remove Ghost items');
checkGhost.value = true;
var checkCompoundFix = add('checkbox', undefined, 'Fix Compound Path Items');
checkCompoundFix.value = true;
var checkRemoveEmptyLayers = add('checkbox', undefined, 'Remove empty layers');
checkRemoveEmptyLayers.value = true;
var checkRasterEmbed = add('checkbox', undefined, 'Embed rester items');
checkRasterEmbed.value = true;
var checkSymbolBreakLink = add('checkbox', undefined, 'Symbols break link');
checkSymbolBreakLink.value = true;
var checkExpandStyle = add('checkbox', undefined, 'Expand items (styles)');
checkExpandStyle.helpTip = 'Expand brushes, symbols, effects ...';
checkExpandStyle.value = true;
var dlistLocked = add('dropdownlist', undefined, 'Locked items: Skip,Locked items: Unlock,Locked items: Remove'.split(','));
dlistLocked.selection = 0;
var dlistOpenPaths = add('dropdownlist', undefined, 'Open paths: Join,Open paths: Remove,Open paths: Skip'.split(','));
dlistOpenPaths.selection = 0;
var dlistGuides = add('dropdownlist', undefined, 'Guides: Skip,Guides: Remove,Guides: Move to new layer'.split(','));
dlistGuides.selection = 1;
}
with (add('group')) {
orientation = 'row';
alignChildren = ['fill', 'fill'];
var cancel = add('button', undefined, 'Cancel');
cancel.helpTip = 'Press Esc to Close';
cancel.onClick = function () { win.close(); }
var ok = add('button', [0, 0, 100, 30], 'Optimize');
ok.helpTip = 'Press Enter to Run';
ok.onClick = startAction;
ok.active = true;
}
}
var progressBar = win.add('progressbar'),
progressBarCounter = 100;
progressBar.value = 0;
progressBar.minvalue = 0;
progressBar.maxvalue = progressBarCounter;
progressBar.maximumSize = [1000, 5];
function startAction() {
try {
globalGroup.enabled = false;
optimizero({
hidden: checkHidden.value,
locked: dlistLocked.selection.text.toLowerCase().replace(/ /g, '').replace('lockeditems:', ''),
ghost: checkGhost.value,
compoundFix: checkCompoundFix.value,
rasterEmbed: checkRasterEmbed.value,
symbolBreakLink: checkSymbolBreakLink.value,
expandStyle: checkExpandStyle.value,
guides: dlistGuides.selection.text.toLowerCase().replace(/ /g, '').replace('guides:', ''),
removeEmptyLayers: checkRemoveEmptyLayers.value,
openPaths: dlistOpenPaths.selection.text.toLowerCase().replace(/ /g, '').replace('openpaths:', ''),
});
}
catch(err)
{$.errorMessage(err);
}
win.close();
}
function saveSettings() {
var $file = new File(settingFile.folder + settingFile.name),
data = [
checkHidden.value,
dlistLocked.selection.index,
checkGhost.value,
dlistGuides.selection.index,
checkRemoveEmptyLayers.value,
checkRasterEmbed.value,
checkExpandStyle.value,
dlistOpenPaths.selection.index,
checkCompoundFix.value,
checkSymbolBreakLink.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(',');
checkHidden.value = ($main[0] === 'true');
dlistLocked.selection = parseInt($main[1]);
checkGhost.value = ($main[2] === 'true');
dlistGuides.selection = parseInt($main[3]);
checkRemoveEmptyLayers.value = ($main[4] === 'true');
checkRasterEmbed.value = ($main[5] === 'true');
checkExpandStyle.value = ($main[6] === 'true');
dlistOpenPaths.selection = parseInt($main[7]);
checkCompoundFix.value = ($main[8] === 'true');
checkSymbolBreakLink.value = ($main[9] === 'true');
} 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();