forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_fluff.js
319 lines (288 loc) · 7.5 KB
/
delete_fluff.js
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
/////////////////////////////////////////////////////////////////
//Delete Unused Assets (fluff) v.2 -- CS, CS2
//>=--------------------------------------
//Code is loosely based on a VB script from
// <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0321112512/qid=1112121623/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/002-3373531-7064044?v=glance&s=books&n=507846">Adobe Illustrator Scripting</a> by Ethan Wilde
// -------------------------------------------------------
// This script does the following:
// 1) removes unused swatches.
// 2) removes unused symbols.
// 3) removes ALL styles:
// There is no way to check if a style is applied,
// but this is not critical to me because the styled object
// will not loose it's appearance, and the style can be re-added
// if necessary by just dragging a 'styled' object back to the styles pallate.
//
// This script does <b>NOT</b> do the following:
// 1) remove brushes:
// The Javascript support just isn't there.
//
// <b>As of version 2:</b>
// You can set two variables at the top of the script to
// delete the "NoColor" and/or the "Registration" swatches.
// They are ignored by default.
//
// <b>THIS SCRIPT HAS BEEN RENDERED 99% OBSOLETE
// BY THE "DELETE FLUFF" ACTION </b>
// which can be found in the "default.aia" file (included with illustrator),
// or you can downloaded the action separately <a href='http://www.wundes.com/js4ai/deleteFluff.zip'>HERE</a>
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
var docRef= app.activeDocument;
if ( app.documents.length > 0 )
{
//RE: tweaking this script:
//
//-----Set 'keepRegistration' to 'false' to remove registration swatch.---
var keepRegistration = true;
//-----Set 'keepNoColor' to 'false' to remove NoColor swatch.---
var keepNoColor = true;
//-----Set 'brag' to '0' to disable the "Removed:" statistics info.---
var brag = 1;
var skipSwatches = 0;
var swtsRem = 0;
var smbsRem = 0;
var stysRem = 0;
// Now call the "remove" functions (just comment out the ones you don't want to use...)
deleteUnusedSwatches();
deleteUnusedSymbols();
deleteStyles();
//deleteBrushes();
//
//____deleteBrushes() doesn't work.
//____ You can see them, but you cant remove them..._____
// now assemble the stats regarding what was removed.
if(brag == 1){
var msg = "";
var msg1 = "";
var msg2 = "";
var msg3 = "";
if(swtsRem>0){msg1 = swtsRem+" unused swatches.\n"}
if(smbsRem>0){msg2 = smbsRem+" unused Symbols.\n"}
if(stysRem>0){msg3 = "All "+stysRem+" Styles.\n"}
if(swtsRem>0 || smbsRem>0 || stysRem>0){msg = msg1+msg2+msg3;
} else {msg = "Nothing.\n(Nothing to Remove.)";}
alert("Removed:\n"+msg);
}
}
// ============================
// ====functions start here====
// ============================
function deleteUnusedSwatches(){
killed = "";
saved = "";
var usedSwatches = findUsedSwatches();
//alert("UsedSwatchesLength = "+usedSwatches.length);
var x = lastIndex = app.activeDocument.swatches.length;
total = x;
var isSpotReg = 0;
/*
*/
try
{
while(x > skipSwatches){
var lastIndex = app.activeDocument.swatches.length - 1;
var swatchToDelete = app.activeDocument.swatches[x-1];
//initialize vars to 0
save = ulen = noSwt = regSwt = 0;
try
{
isSpotReg = swatchToDelete.color.spot.colorType == ColorModel.REGISTRATION;
}
catch (e)
{
// do nothing, we don't care if it fails, only if it succeeds.
}
for (var u in usedSwatches)
{
ulen ++;
if (compareColors(usedSwatches[u],swatchToDelete.color) )
{
saved+= swatchToDelete+"\n";
save = 1;
x--
}
}
if (isSpotReg && keepRegistration)
{ // For Registration swatch..
saved+= swatchToDelete+"\n";
save = 1;
x--;
//resetting variable to 0 because every subsequent "try" will fail
isSpotReg = 0;
regSwt=1;
} else if (swatchToDelete.color.typename == "NoColor" && keepNoColor)
{// for "NoColor" swatch
saved+= swatchToDelete+"\n";
save = 1;
x--;
noSwt=1;
}
if (save == 0)
{
killed += swatchToDelete+"\n";
swatchToDelete.remove();
x--;
}
}
// for tracking...
swtsRem =total-(ulen+noSwt+regSwt);
}
catch (e)
{
alert( e+"\nThe specified swatch doesn't exist. x = " +x);
}
//}
}
function deleteStyles(){
try
{
stylesLen = app.activeDocument.graphicStyles.length;
if( stylesLen > 1){
// there is always the one default style...
stysRem = stylesLen;
app.activeDocument.graphicStyles.removeAll();
}
}
catch (e)
{
alert( "Cannot Delete Styles." + e);
}
}
function deleteUnusedSymbols(){
try
{
usedSymbols = testSymbols();
var x = lastIndex = app.activeDocument.symbols.length;
while(x >= 1){
// here I use x to iterate backwards through the symbols table
// instead of reloading the length each time.
var symbolToDelete = app.activeDocument.symbols[x-1];
if(!contains(usedSymbols,symbolToDelete)){
symbolToDelete.remove();
smbsRem++;
}
x--;
}
}
catch (e)
{
alert( "The specified Symbol doesn't exist" +e);
}
}
function contains(array,item){
for (var each in array)
{
if (item == array[each])
{
return(true);
}
}
return false;
}
function findUsedSwatches(){
allitems = activeDocument.pageItems.length;
var found = [];
while (allitems > 0)
{
if(activeDocument.pageItems[allitems-1].stroked == true){
stk = activeDocument.pageItems[allitems-1].strokeColor;
if (!inList(stk,found))
{
found.push(stk);
}
}
if(activeDocument.pageItems[allitems-1].filled == true){
fil = activeDocument.pageItems[allitems-1].fillColor;
if (!inList(fil,found))
{
found.push(fil);
}
} else if(activeDocument.pageItems[allitems-1].typename == "TextFrame"){
fil = activeDocument.pageItems[allitems-1].textRange.fillColor;
if (!inList(fil,found))
{
found.push(fil);
}
}
//
allitems--;
}
return(found);
}
function inList(a,b){
if (b.length == 0)
{
return false;
}
for (var all in b)
{
if(compareColors(a,b[all])){
return true;
}
}
return false;
}
function compareColors(a,b){
// No need to check for "none" because the calling function only passes hits.
if (a.pattern == b.pattern && a.pattern != undefined)
{
//Compare patterns
return true;
}
else if (a.gradient == b.gradient && a.gradient != undefined)
{
//Compare gradients
return true;
}
else
{
//innocent until proven guilty..
answer = true;
//Compare contents...
for (var each in a)
{
if (a[each] != b[each] && each!= "tint")
{
//if anything doesn't match:
answer = false;
}
}
return answer;
}
}
function testSymbols(){
all = activeDocument.pageItems.length;
safe = 0;
var found = [];
while (all > 0)
{
var current = activeDocument.pageItems[all-1].symbol
if(current != undefined){
found.push(current);
}
all--;
}
return (found);
}
//The following function doesn't work.
//there is no JS support for removing brushes in Illustrator CS... Maybe in CS2... (sigh)
function deleteBrushes(){
if(Number(app.version.substr(0,2)) >= 12){
try
{
var x = lastIndex = app.activeDocument.brushes.length;
while(x >= 1){
var lastIndex = app.activeDocument.brushes.length - 1;
var brushToDelete = app.activeDocument.brushes[lastIndex];
brushToDelete.remove();
x--;
}
}
catch (e)
{
alert( "Whoops...\n" + e);
} }
}