-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathPreflight.jsx
executable file
·429 lines (298 loc) · 10.8 KB
/
Preflight.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
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
// Turn debugging mesages on or off
var debugThisScript = false;
var xdebugThisScript = false;
// Which version of this script
// 0.1 First version
// 0.11 Minor changes here and there.
// 0.12 Wrap fill color checking in a try/catch block and flag type with crazy CMYK values.
var scriptVersion = "0.12";
// find active document
var theDoc = app.activeDocument;
// Arrays to hold details on the problem elements we find.
var badBlackType = new Array();
var badBlackIndex = 0;
var badOverprintType = new Array();
var badOverprintIndex = 0;
var badFillColorType = new Array();
var badFillColorIndex = 0;
// Keep track if we are accidentally in RGB color mode
var badColorMode = 0;
// Check document color space
if ( theDoc.documentColorSpace == DocumentColorSpace.CMYK ) {
//all good
debugln( "Document color space is CMYK." )
} else {
debugln( "Document color space is RGB." )
badColorMode = 1;
}
var dlg;
// Loop thru all text frames in document
for ( var i = 0; i < theDoc.textFrames.length; i ++ ) {
var item = theDoc.textFrames[i];
debugln( "Starting processing of text frame # " + i );
debugln( " Name: " + item.name );
debugln( " Type: " + item.typename );
debugln( " Parent name: " + item.parent.name );
debugln( " Parent type: " + item.parent.typename );
//debugln( " Contents: " + item.contents );
if ( item.typename == 'TextFrame' ) {
debugln( '' );
var badType = "";
//for ( var ci = 0; ci < item.characters.length; ci++ ) {
//}
var inBadBlackType = false;
var inBadOverprintType = false;
var inBadFillColorType = false;
for ( var j = 0; j < item.textRanges.length; j++ ) {
var tr = item.textRanges[j];
// xdebugln( 'text range is ' + tr.contents );
var fillColor;
try {
xdebugln( tr.contents + ' ' + inBadFillColorType + ' ' + badFillColorIndex);
fillColor = tr.characterAttributes.fillColor;
if ( inBadFillColorType ) {
xdebugln ( '--------- i am here --------- ' );
inBadFillColorType = false;
badFillColorIndex++;
}
} catch ( e ) {
if ( ! inBadFillColorType ) {
inBadFillColorType = true;
badFillColorType[ badFillColorIndex ] = new Object();
badFillColorType.contents = "";
}
if ( typeof( badFillColorType[badFillColorIndex].contents )=="undefined" ) {
badFillColorType[badFillColorIndex].contents = tr.contents;
} else {
badFillColorType[badFillColorIndex].contents += tr.contents;
}
continue;
}
//var fillColor = tr.characterAttributes.fillColor;
var c = undefined;
var m = undefined;
var y = undefined;
var k = undefined;
// we have CMYK type.
if ( fillColor.typename == "CMYKColor" ) {
c = fillColor.cyan;
m = fillColor.magenta;
y = fillColor.yellow;
k = fillColor.black;
} else if ( fillColor.typename == "SpotColor" ) {
var theSpot = fillColor.spot;
if ( theSpot.color.typename == "CMYKColor" ) {
c = theSpot.color.cyan;
m = theSpot.color.magenta;
y = theSpot.color.yellow;
k = theSpot.color.black;
}
} else if ( fillColor.typename == "GrayColor" ) {
c = 0; m = 0; y = 0;
k = fillColor.gray;
}
// Check for bad type
if ( k > 60 && c+m+y > 0 ) {
if ( ! inBadBlackType ) {
inBadBlackType = true;
badBlackType[ badBlackIndex ] = new Object();
badBlackType.contents = "";
}
if ( typeof( badBlackType[badBlackIndex].contents )=="undefined" ) {
badBlackType[badBlackIndex].contents = tr.contents;
} else {
badBlackType[badBlackIndex].contents += tr.contents;
}
badBlackType[badBlackIndex].cmyk = "" + c + " " + m + " " + y + " " + k;
inBadBlackType = true;
} else {
if ( inBadBlackType == true ) {
inBadBlackType = false;
badBlackIndex++;
}
}
// Check for overprint white type.
if ( ( c + m + y + k ) == 0 && tr.characterAttributes.overprintFill ) {
if ( ! inBadOverprintType ) {
inBadOverprintType = true;
badOverprintType[ badOverprintIndex ] = new Object();
badOverprintType.contents = "";
}
if ( typeof( badOverprintType[badOverprintIndex].contents )=="undefined" ) {
badOverprintType[badOverprintIndex].contents = tr.contents;
} else {
badOverprintType[badOverprintIndex].contents += tr.contents;
}
badOverprintType[badOverprintIndex].cmyk = "" + c + " " + m + " " + y + " " + k;
inbadOverprintType = true;
} else {
if ( inBadOverprintType == true ) {
inBadOverprintType = false;
badOverprintIndex++;
}
}
debugln( "--> " + tr.contents + " is " + fillColor.typename + " " + c + " " + m + " " + y + " " + k );
}
// out of textframe. reset inFlag. increment index by 1 if still in;
if ( inBadBlackType ) {
badBlackIndex++;
}
// out of textframe. reset inFlag. increment index by 1 if still in;
if ( inBadOverprintType ) {
badOverprintIndex++;
}
if ( inBadFillColorType ) {
badFillColorIndex++;
}
//item.contents = mapData[dataType][ key ].text;
//item.textRange.characterAttributes.fillColor = getColorfromString( mapData[dataType][ key ].fillCMYK );
}
}
var numProblems = badColorMode + badBlackType.length + badOverprintType.length + badFillColorType.length;
var resultsMsg = "";
if ( numProblems > 0 ) {
resultsMsg += "Summary of problems found in \"" + theDoc.name + "\"\n";
resultsMsg += '\n';
if ( badColorMode ) {
alertMsg += "\nYour document is in RGB color mode. Change it to CMYK.\n"
resultsMsg += 'BAD DOCUMENT COLOR MODE\n';
resultsMsg += '====================================================================\n';
resultsMsg += "Your document is RGB color mode. You must change it to CMYK.\n";
resultsMsg += '\n';
}
if ( badBlackType.length ) {
alertMsg += "\nYour document has " + badBlackType.length + " pieces of 4-color black type."
resultsMsg += 'POSSIBLE 4-COLOR BLACK TYPE\n';
resultsMsg += '================================================\n';
for ( var i=0; i<badBlackType.length; i++ ) {
debugln("");
debugln("-------------------------");
debugln("Bad 4-color black type")
debugln( badBlackType[i].contents );
debugln( badBlackType[i].cmyk );
resultsMsg += badBlackType[i].contents + "\n";
}
resultsMsg += '\n';
}
if ( badFillColorType.length ) {
alertMsg += "\nYour document has " + badFillColorType.length + " pieces of type with invalid CMYK specs."
resultsMsg += 'POSSIBLE TYPE WITH INVALID CMYK SPECS\n';
resultsMsg += '(It might be OK, but check each piece by hand, please)\n';
resultsMsg += '================================================\n';
for ( var i=0; i<badFillColorType.length; i++ ) {
debugln("");
debugln("-------------------------");
debugln(" type")
debugln( badFillColorType[i].contents );
debugln( badFillColorType[i].cmyk );
resultsMsg += badFillColorType[i].contents + "\n";
}
resultsMsg += '\n';
}
if ( badOverprintType.length ) {
alertMsg += "\nYour document has " + badOverprintType.length + " pieces of overprinting white type."
resultsMsg += 'POSSIBLE OVERPRINTING WHITE TYPE\n';
resultsMsg += '================================================\n';
for ( var i=0; i<badOverprintType.length; i++ ) {
debugln("");
debugln("-------------------------");
debugln("Overprinting white type")
debugln( badOverprintType[i].contents );
resultsMsg += badOverprintType[i].contents + "\n";
}
resultsMsg += '\n';
}
} else {
resultsMsg = 'Your document appears to be OK.';
}
displayAlert( resultsMsg );
function displayAlert( msg ) {
dlg = new Window('dialog', 'Preflight Report');
var msgPnl = dlg.add('panel', undefined, 'Preflight Report');
msgPnl.orientation = 'row';
msgPnl.report = msgPnl.add('edittext', undefined, msg,{multiline:true});
msgPnl.report.preferredSize = [600,400];
var btnPnl = dlg.add('group', undefined, '');
btnPnl.orientation = 'row'
btnPnl.cancelBtn = btnPnl.add('button', undefined, 'Cancel', {name:'cancel'});
btnPnl.cancelBtn.onClick = function() { dlg.close() };
dlg.show();
}
app.redraw();
function getCMYKfromString( s ) {
var cmyk = s.split( "," );
var color = new CMYKColor();
color.cyan = cmyk[0];
color.magenta = cmyk[1];
color.yellow = cmyk[2];
color.black = cmyk[3];
return color;
}
function getColorfromString( s ) {
var colorType = '';
var colorData = '';
if ( s.match( ":" ) ) {
var colorParts = s.split( ':' );
colorType = colorParts[0];
colorData = colorParts[1];
} else {
colorType = 'CMYK';
colorData = s;
}
var color;
debugln( ' --> Color type: ' + colorType ) ;
debugln( ' --> Color data: ' + colorData ) ;
if ( colorType == 'CMYK' ) {
var cmyk = s.split( "," );
color = new CMYKColor();
color.cyan = cmyk[0];
color.magenta = cmyk[1];
color.yellow = cmyk[2];
color.black = cmyk[3];
} else if ( colorType == 'Swatch' ) {
try {
if ( theDoc.swatches[colorData] ) {
var swatch = theDoc.swatches[colorData];
color = swatch.color;
}
} catch (e) {
alert( "The swatch " + colorData + " doesn’t exist" );
}
}
return color;
}
true;
//////////////////////////////////////////
// Functions start here
function getNewName(sourceDoc, destFolder) {
var docName = sourceDoc.name;
var ext = '.jpg'; // new extension for pdf file
var newName = "";
// if name has no dot (and hence no extension,
// just append the extension
if (docName.indexOf('.') < 0) {
newName = docName + ext;
} else {
var dot = docName.lastIndexOf('.');
newName += docName.substring(0, dot);
newName += ext;
}
newName = newName.replace( / /g, '-' );
// Create a file object to save the pdf
saveInFile = new File( destFolder + '/' + newName );
return saveInFile;
}
function debugln ( s ) {
if ( debugThisScript ) {
$.writeln( s );
}
}
function xdebugln ( s ) {
if ( xdebugThisScript ) {
$.writeln( s );
}
}