forked from jalbertbowden/ps-pdf-processor.jsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF Processor X.jsx
More file actions
737 lines (692 loc) · 30.8 KB
/
PDF Processor X.jsx
File metadata and controls
737 lines (692 loc) · 30.8 KB
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
/*
PDF Processor X 23.4.2
A modified version of 'PDF Processor for CS3 II' by Paul MR, Wed Aug 08, 2012
https://web.archive.org/web/20150511194417/https://www.ps-scripts.com/bb/viewtopic.php?f=10&t=1882&sid=a87e6a1cbc6fec5b38abbfc402e624ac
(c) 2007-2012 Paul MR
(c) 2020-2023 Paul Chiorean <jpeg@basement.ro>
*/
// @target photoshop
var settings, topLevelFolder, outputFolder;
var settingsFile = File(Folder.userData + '/PDF Processor X.prefs');
var defaults = {
mode: { colormode: 0, bitdepth: 0, resolution: '300' },
range: { mode: [ false, true, false ], from: '', to: '' },
io: { mode: [ false, true, false ], source: '', destination: '', origfolder: true },
processing: {
action: { active: false, set: '', name: '' },
resize: { active: false, width: '', height: '' },
when: 0
},
output: {
jpg: { active: true, quality: 8 },
jpgweb: { active: false, quality: 80 },
tiff: { active: false, compression: 0 },
png: { active: false, quality: 80 },
psd: { active: false },
pdf: { active: false }
},
options: { crop: 1, flatten: true }
};
app.bringToFront();
if (app.version.match(/\d+/) < 10) alert('Sorry, this script needs CS3 or better.');
else main();
function main() {
// User interface
var win = new Window('dialog', 'PDF Processor X');
win.orientation = 'column';
win.alignChildren = [ 'fill', 'top' ];
// Main group
win.main = win.add('group', undefined);
win.main.orientation = 'column';
win.main.alignChildren = [ 'fill', 'center' ];
// Panel 1: Mode & cropping
win.gMode = win.main.add('panel', undefined, 'Mode & cropping');
win.gMode.orientation = 'column';
win.gMode.alignChildren = 'left';
win.gMode.g1 = win.gMode.add('group', undefined);
win.gMode.g1.orientation = 'row';
win.gMode.g1.st1 = win.gMode.g1.add('statictext', undefined, 'Color mode:');
win.gMode.g1.st1.preferredSize.width = 90;
win.gMode.g1.dd1 = win.gMode.g1.add('dropdownlist', undefined, undefined,
{ items: [ 'CMYK Color', 'RGB Color', 'Grayscale', 'Lab Color' ] });
win.gMode.g1.dd1.preferredSize.width = 145;
win.gMode.g1.dd1.preferredSize.height = 24;
win.gMode.g1.st2 = win.gMode.g1.add('statictext', undefined, 'Bit depth:');
win.gMode.g1.dd2 = win.gMode.g1.add('dropdownlist', undefined, undefined, { items: [ 8, 16 ] });
win.gMode.g1.dd2.preferredSize.height = 24;
win.gMode.g1.st3 = win.gMode.g1.add('statictext', undefined, 'Resolution:');
win.gMode.g1.et1 = win.gMode.g1.add('edittext');
win.gMode.g1.et1.characters = 4;
win.gMode.g1.et1.preferredSize.height = 24;
win.gMode.g1.et1.justify = 'center';
win.gMode.g1.et1.onChanging = function () {
if (this.text.match(/[^\-\.\d]/)) this.text = this.text.replace(/[^\-\.\d]/g, '');
};
win.gMode.g2 = win.gMode.add('group', undefined);
win.gMode.g2.orientation = 'row';
win.gMode.g2.st1 = win.gMode.g2.add('statictext', undefined, 'Crop to:');
win.gMode.g2.st1.preferredSize.width = 90;
win.gMode.g2.dd1 = win.gMode.g2.add('dropdownlist', undefined, undefined,
{ items: [ 'Bounding Box', 'Media Box', 'Crop Box', 'Bleed Box', 'Trim Box', 'Art Box' ] });
win.gMode.g2.dd1.preferredSize.width = 145;
win.gMode.g2.dd1.preferredSize.height = 24;
win.gMode.g2.cb1 = win.gMode.g2.add('checkbox', undefined, 'Flatten document');
win.gMode.g2.cb1.preferredSize.height = 24;
// Panel 2: Range
win.gRange = win.main.add('panel', undefined, 'Pages to process');
win.gRange.orientation = 'row';
win.gRange.alignChildren = 'left';
win.gRange.rb1 = win.gRange.add('radiobutton', undefined, 'First page only');
win.gRange.rb2 = win.gRange.add('radiobutton', undefined, 'All pages');
win.gRange.rb3 = win.gRange.add('radiobutton', undefined, 'Range');
win.gRange.st1 = win.gRange.add('statictext', undefined, 'From:');
win.gRange.et1 = win.gRange.add('edittext');
win.gRange.et1.preferredSize.width = 40;
win.gRange.et1.preferredSize.height = 24;
win.gRange.et1.justify = 'center';
win.gRange.st2 = win.gRange.add('statictext', undefined, 'To:');
win.gRange.et2 = win.gRange.add('edittext');
win.gRange.et2.preferredSize.width = 40;
win.gRange.et2.preferredSize.height = 24;
win.gRange.et2.justify = 'center';
win.gRange.rb1.onClick = win.gRange.rb2.onClick = win.gRange.rb3.onClick = function () {
if (win.gRange.rb3.value === true) {
win.gRange.et1.enabled = win.gRange.et1.active = win.gRange.et2.enabled = true;
win.gRange.st1.visible = win.gRange.st2.visible = true;
win.gRange.et1.visible = win.gRange.et2.visible = true;
} else {
win.gRange.et1.enabled = win.gRange.et2.enabled = false;
win.gRange.st1.visible = win.gRange.st2.visible = false;
win.gRange.et1.visible = win.gRange.et2.visible = false;
}
};
win.gRange.et1.onChanging =
win.gRange.et2.onChanging = function () {
if (this.text.match(/[^\-\.\d]/)) this.text = this.text.replace(/[^\-\.\d]/g, '');
};
// Panel 3: Source & destination
win.gIO = win.main.add('panel', undefined, 'Source & destination');
win.gIO.orientation = 'column';
win.gIO.alignChildren = 'left';
win.gIO.g1 = win.gIO.add('group', undefined);
win.gIO.g1.orientation = 'row';
win.gIO.g1.rb1 = win.gIO.g1.add('radiobutton', undefined, 'Single file');
win.gIO.g1.rb2 = win.gIO.g1.add('radiobutton', undefined, 'Folder');
win.gIO.g1.rb3 = win.gIO.g1.add('radiobutton', undefined, 'Folder and subfolders');
win.gIO.g2 = win.gIO.add('group', undefined);
win.gIO.g2.orientation = 'row';
win.gIO.g2.st1 = win.gIO.g2.add('statictext', undefined, 'Source:');
win.gIO.g2.st1.preferredSize.width = 90;
win.gIO.g2.et1 = win.gIO.g2.add('edittext');
win.gIO.g2.et1.preferredSize.width = 320;
win.gIO.g2.et1.preferredSize.height = 24;
win.gIO.g2.bu1 = win.gIO.g2.add('button', undefined, 'Browse');
win.gIO.g2.bu1.onClick = function () {
if (win.gIO.g1.rb1.value === true)
topLevelFolder = File.openDialog('Select PDF file', 'PDF File:*.pdf');
else topLevelFolder = Folder.selectDialog('Select the source folder');
if (topLevelFolder != null) win.gIO.g2.et1.text = decodeURI(topLevelFolder.fsName);
};
win.gIO.g1.rb1.onClick =
win.gIO.g1.rb2.onClick =
win.gIO.g1.rb3.onClick = function () {
if (win.gIO.g1.rb1.value === true) {
if (topLevelFolder instanceof Folder) { topLevelFolder = ''; win.gIO.g2.et1.text = ''; }
} else if (topLevelFolder instanceof File) { topLevelFolder = ''; win.gIO.g2.et1.text = ''; }
};
win.gIO.g3 = win.gIO.add('group', undefined);
win.gIO.g3.orientation = 'row';
win.gIO.g3.st1 = win.gIO.g3.add('statictext', undefined, 'Destination:');
win.gIO.g3.st1.preferredSize.width = 90;
win.gIO.g3.et1 = win.gIO.g3.add('edittext');
win.gIO.g3.et1.preferredSize.width = 320;
win.gIO.g3.et1.preferredSize.height = 24;
win.gIO.g3.bu1 = win.gIO.g3.add('button', undefined, 'Browse');
win.gIO.g3.bu1.onClick = function () {
outputFolder = Folder.selectDialog('Select the output folder');
if (outputFolder != null) win.gIO.g3.et1.text = decodeURI(outputFolder.fsName);
};
win.gIO.g4 = win.gIO.add('group', undefined);
win.gIO.g4.orientation = 'row';
win.gIO.g4.cb1 = win.gIO.g4.add('checkbox', undefined, 'Save to original folder');
win.gIO.g4.cb1.onClick = function () {
if (win.gIO.g4.cb1.value) {
win.gIO.g3.et1.enabled = false;
win.gIO.g3.bu1.enabled = false;
} else {
win.gIO.g3.et1.enabled = true;
win.gIO.g3.bu1.enabled = true;
}
};
// Panel 4: Processing
win.gProc = win.main.add('panel', undefined, 'Processing');
win.gProc.orientation = 'column';
win.gProc.alignChildren = 'left';
win.gProc.g1 = win.gProc.add('group', undefined);
win.gProc.g1.orientation = 'row';
win.gProc.g1.cb1 = win.gProc.g1.add('checkbox', undefined, 'Run action:');
win.gProc.g1.cb1.preferredSize.width = 90;
win.gProc.g1.cb1.preferredSize.height = 24;
win.gProc.g1.dd1 = win.gProc.g1.add('dropdownlist', undefined, undefined);
win.gProc.g1.dd1.preferredSize.width = 145;
win.gProc.g1.dd1.preferredSize.height = 24;
win.gProc.g1.dd2 = win.gProc.g1.add('dropdownlist', undefined, undefined);
win.gProc.g1.dd2.preferredSize.width = 259;
win.gProc.g1.dd2.preferredSize.height = 24;
var actionSets = getActionSets();
for (var i in actionSets) win.gProc.g1.dd1.add('item', actionSets[i]);
var actions = getActions(actionSets[0]);
for (var i in actions) win.gProc.g1.dd2.add('item', actions[i]);
win.gProc.g1.dd1.onChange = function () {
win.gProc.g1.dd2.removeAll();
actions = getActions(actionSets[this.selection.index]);
for (var i in actions) win.gProc.g1.dd2.add('item', actions[i]);
};
win.gProc.g1.cb1.onClick = function () {
if (win.gProc.g1.cb1.value) win.gProc.g1.dd1.enabled = win.gProc.g1.dd2.enabled = true;
else win.gProc.g1.dd1.enabled = win.gProc.g1.dd2.enabled = false;
};
win.gProc.g2 = win.gProc.add('group', undefined);
win.gProc.g2.orientation = 'row';
win.gProc.g2.cb1 = win.gProc.g2.add('checkbox', undefined, 'Resize to fit');
win.gProc.g2.cb1.preferredSize.width = 90;
win.gProc.g2.st1 = win.gProc.g2.add('statictext', undefined, 'Width:');
win.gProc.g2.et1 = win.gProc.g2.add('edittext');
win.gProc.g2.et1.preferredSize.width = 50;
win.gProc.g2.st2 = win.gProc.g2.add('statictext', undefined, 'px');
win.gProc.g2.st3 = win.gProc.g2.add('statictext', undefined, 'Height:');
win.gProc.g2.et2 = win.gProc.g2.add('edittext');
win.gProc.g2.et2.preferredSize.width = 50;
win.gProc.g2.st4 = win.gProc.g2.add('statictext', undefined, 'px');
win.gProc.g2.dd1 = win.gProc.g2.add('dropdownlist', undefined, undefined,
{ items: [ 'Before action', 'After action' ] });
win.gProc.g2.dd1.preferredSize.height = 24;
win.gProc.g2.et1.onChanging =
win.gProc.g2.et2.onChanging = function () {
if (this.text.match(/[^\-\.\d]/)) this.text = this.text.replace(/[^\-\.\d]/g, '');
};
win.gProc.g2.cb1.onClick = function () {
if (win.gProc.g2.cb1.value) {
win.gProc.g2.et1.enabled = win.gProc.g2.et1.active = true;
win.gProc.g2.et2.enabled = true;
win.gProc.g2.dd1.enabled = true;
} else {
win.gProc.g2.et1.enabled = win.gProc.g2.et2.enabled = false;
win.gProc.g2.dd1.enabled = false;
}
};
// Panel 5: Output options
win.gOutput = win.main.add('panel', undefined, 'Output options');
win.gOutput.orientation = 'row';
win.gOutput.alignChildren = [ 'left', 'center' ];
win.gOutput.spacing = 20;
win.gOutput.c1 = win.gOutput.add('group', undefined);
win.gOutput.c1.orientation = 'column';
win.gOutput.c1.alignChildren = [ 'fill', 'center' ];
win.gOutput.c1.g1 = win.gOutput.c1.add('group', undefined);
win.gOutput.c1.g1.orientation = 'row';
win.gOutput.c1.g1.alignChildren = [ 'left', 'center' ];
win.gOutput.c1.g1.cb1 = win.gOutput.c1.g1.add('checkbox', undefined, 'JPG:');
win.gOutput.c1.g1.cb1.preferredSize.height = 24;
win.gOutput.c1.g1.dd1 = win.gOutput.c1.g1.add('dropdownlist', undefined, undefined,
{ items: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] });
win.gOutput.c1.g1.dd1.preferredSize.height = 24;
win.gOutput.c1.g2 = win.gOutput.c1.add('group', undefined);
win.gOutput.c1.g2.orientation = 'row';
win.gOutput.c1.g2.alignChildren = [ 'left', 'center' ];
win.gOutput.c1.g2.cb1 = win.gOutput.c1.g2.add('checkbox', undefined, 'Save For Web JPG:');
win.gOutput.c1.g2.cb1.preferredSize.height = 24;
win.gOutput.c1.g2.dd1 = win.gOutput.c1.g2.add('dropdownlist', undefined, undefined);
for (var a = 1; a < 101; a++) win.gOutput.c1.g2.dd1.add('item', a);
win.gOutput.c1.g2.dd1.preferredSize.height = 24;
win.gOutput.c2 = win.gOutput.add('group', undefined);
win.gOutput.c2.orientation = 'column';
win.gOutput.c2.alignChildren = [ 'fill', 'center' ];
win.gOutput.c2.g1 = win.gOutput.c2.add('group', undefined);
win.gOutput.c2.g1.orientation = 'row';
win.gOutput.c2.g1.alignChildren = [ 'left', 'center' ];
win.gOutput.c2.g1.cb1 = win.gOutput.c2.g1.add('checkbox', undefined, 'TIFF:');
win.gOutput.c2.g1.cb1.preferredSize.height = 24;
win.gOutput.c2.g1.dd1 = win.gOutput.c2.g1.add('dropdownlist', undefined, undefined,
{ items: [ 'LZW', 'ZIP', 'JPG', 'None' ] });
win.gOutput.c2.g1.dd1.preferredSize.width = 70;
win.gOutput.c2.g1.dd1.preferredSize.height = 24;
win.gOutput.c2.g2 = win.gOutput.c2.add('group', undefined);
win.gOutput.c2.g2.orientation = 'row';
win.gOutput.c2.g2.alignChildren = [ 'left', 'center' ];
win.gOutput.c2.g2.cb1 = win.gOutput.c2.g2.add('checkbox', undefined, 'PNG:');
win.gOutput.c2.g2.cb1.preferredSize.height = 24;
win.gOutput.c2.g2.dd1 = win.gOutput.c2.g2.add('dropdownlist', undefined, undefined);
for (var a = 0; a < 101; a++) win.gOutput.c2.g2.dd1.add('item', a);
win.gOutput.c2.g2.dd1.preferredSize.width = 70;
win.gOutput.c2.g2.dd1.preferredSize.height = 24;
win.gOutput.c3 = win.gOutput.add('group', undefined);
win.gOutput.c3.orientation = 'column';
win.gOutput.c3.alignChildren = [ 'fill', 'center' ];
win.gOutput.c3.spacing = 15;
win.gOutput.c3.g1 = win.gOutput.c3.add('group', undefined);
win.gOutput.c3.g1.orientation = 'column';
win.gOutput.c3.g1.alignChildren = [ 'left', 'center' ];
win.gOutput.c3.g1.cb1 = win.gOutput.c3.g1.add('checkbox', undefined, 'PSD');
win.gOutput.c3.g1.cb1.preferredSize.height = 20;
win.gOutput.c3.g2 = win.gOutput.c3.add('group', undefined);
win.gOutput.c3.g2.orientation = 'column';
win.gOutput.c3.g2.alignChildren = [ 'left', 'center' ];
win.gOutput.c3.g2.cb1 = win.gOutput.c3.g2.add('checkbox', undefined, 'Photoshop PDF');
win.gOutput.c3.g2.cb1.preferredSize.height = 20;
win.gOutput.c1.g1.cb1.onClick = function () {
if (win.gOutput.c1.g1.cb1.value) {
win.gOutput.c1.g1.dd1.enabled = true;
win.gOutput.c1.g2.cb1.value = false;
win.gOutput.c1.g2.dd1.enabled = false;
} else {
win.gOutput.c1.g1.dd1.enabled = false;
}
};
win.gOutput.c1.g2.cb1.onClick = function () {
if (win.gOutput.c1.g2.cb1.value) {
win.gOutput.c1.g1.cb1.value = false;
win.gOutput.c1.g1.dd1.enabled = false;
win.gOutput.c1.g2.dd1.enabled = true;
} else {
win.gOutput.c1.g2.dd1.enabled = false;
}
};
win.gOutput.c2.g1.cb1.onClick = function () {
if (win.gOutput.c2.g1.cb1.value) win.gOutput.c2.g1.dd1.enabled = true;
else win.gOutput.c2.g1.dd1.enabled = false;
};
win.gOutput.c2.g2.cb1.onClick = function () {
if (win.gOutput.c2.g2.cb1.value) win.gOutput.c2.g2.dd1.enabled = true;
else win.gOutput.c2.g2.dd1.enabled = false;
};
// Ok/Cancel group
win.gOkCancel = win.add('group', undefined);
win.gOkCancel.orientation = 'row';
win.gOkCancel.alignChildren = [ 'right', 'fill' ];
win.gOkCancel.bu1 = win.gOkCancel.add('button', undefined, 'Cancel');
win.gOkCancel.bu1.preferredSize.width = 100;
win.gOkCancel.bu1.preferredSize.height = 30;
win.gOkCancel.bu2 = win.gOkCancel.add('button', undefined, 'Ok');
win.gOkCancel.bu2.preferredSize.width = 100;
win.gOkCancel.bu2.preferredSize.height = 30;
// Helper functions
win.gOkCancel.bu2.onClick = function () {
if (win.gIO.g2.et1.text === '') { alert('No file or folder has been selected.'); return; }
if (win.gIO.g4.cb1.value === false && win.gIO.g3.et1.text === '')
{ alert('No output folder has been selected.'); return; }
if (win.gProc.g2.cb1.value === true && win.gProc.g2.et1.text === '')
{ alert('No resize width value has been entered.'); return; }
if (win.gProc.g2.cb1.value === true && win.gProc.g2.et2.text === '')
{ alert('No resize height value has been entered.'); return; }
if (win.gRange.rb3.value === true) {
if (win.gRange.et1.text === '') { alert("No number has been entered in the 'From' field."); return; }
if (win.gRange.et2.text === '') { alert("No number has been entered in the 'To' field."); return; }
if (Number(win.gRange.et1.text) > Number(win.gRange.et2.text))
{ alert("'To' field should be greater than the 'From' field."); return; }
}
var saveFiles = 0;
if (win.gOutput.c2.g1.cb1.value) saveFiles++;
if (win.gOutput.c3.g1.cb1.value) saveFiles++;
if (win.gOutput.c2.g2.cb1.value) saveFiles++;
if (win.gOutput.c1.g1.cb1.value) saveFiles++;
if (win.gOutput.c3.g2.cb1.value) saveFiles++;
if (win.gOutput.c1.g2.cb1.value) saveFiles++;
if (saveFiles === 0) { alert('No save format has been selected.'); return; }
win.close(0);
var folders = [];
app.displayDialogs = DialogModes.NO;
var strtRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// Single file
if (win.gIO.g1.rb1.value === true) processPDF(topLevelFolder);
// Folder of PDFs
if (win.gIO.g1.rb2.value === true) {
folders[0] = Folder(topLevelFolder);
var fileList = folders[0].getFiles('*.pdf');
for (var f in fileList) processPDF(fileList[f]);
}
// Folder and sub folders of PDFs
if (win.gIO.g1.rb3.value === true) {
folders = findAllFolders(topLevelFolder, folders);
folders.unshift(topLevelFolder);
for (var z in folders) {
var fileList = folders[z].getFiles('*.pdf');
for (var k in fileList) processPDF(fileList[k]);
}
}
app.preferences.rulerUnits = strtRulerUnits;
function processPDF(pdfFile) {
var noOfDocs = app.documents.length;
if (win.gRange.rb1.value === true) { pageStart = 1; pageEnd = 2; } // One page only
if (win.gRange.rb2.value === true) { pageStart = 1; pageEnd = 9999; } // All pages
if (win.gRange.rb3.value === true) { // Range of pages
pageStart = Number(win.gRange.et1.text);
pageEnd = (Number(win.gRange.et2.text) + 1);
}
var fileCount = 0;
if (win.gIO.g4.cb1.value) outputFolder = pdfFile.path;
var Name = decodeURI(pdfFile.name.replace(/\.[^\.]+$/, ''));
for (var a = pageStart; a < pageEnd; a++) {
var res = Number(win.gMode.g1.et1.text);
var modes = [ 'ECMY', 'RGBC', 'Grys', 'LbCl' ];
var mode = modes[win.gMode.g1.dd1.selection.index];
var bits = [ 8, 16 ];
var BitDepth = bits[win.gMode.g1.dd2.selection.index];
var cropTo = [ 'boundingBox', 'mediaBox', 'cropBox', 'bleedBox', 'trimBox', 'artBox' ];
var cropto = cropTo[win.gMode.g2.dd1.selection.index];
rasterizePDF(a, res, mode, BitDepth, cropto, pdfFile);
if (app.documents.length === noOfDocs) return; // No document opened
fileCount++;
if (win.gMode.g2.cb1.value) app.activeDocument.flatten();
var saveFile = outputFolder + '/' + Name;
if (fileCount !== 1 &&
(win.gRange.rb2.value === true || // All pages
win.gRange.rb3.value === true)) { // Range
saveFile = saveFile + '-' + zeroPad(fileCount, 1);
}
if (win.gProc.g2.cb1.value === true && win.gProc.g2.dd1.selection.index === 0)
fitImage(Number(win.gProc.g2.et1.text), Number(win.gProc.g2.et2.text));
if (win.gProc.g1.cb1.value)
doAction(win.gProc.g1.dd2.selection.text.toString(), win.gProc.g1.dd1.selection.text.toString());
if (win.gProc.g2.cb1.value === true && win.gProc.g2.dd1.selection.index === 1)
fitImage(Number(win.gProc.g2.et1.text), Number(win.gProc.g2.et2.text));
// Save files
if (win.gOutput.c2.g1.cb1.value) {
tifsaveFile = File(saveFile + '.tif');
saveTIFF(saveFile, win.gOutput.c2.g1.dd1.selection.index);
}
if (win.gOutput.c3.g1.cb1.value) {
psdsaveFile = File(saveFile + '.psd');
savePSD(psdsaveFile);
}
if (win.gOutput.c2.g2.cb1.value) {
pngsaveFile = File(saveFile + '.png');
savePNGweb(pngsaveFile, (win.gOutput.c2.g2.dd1.selection.index + 1));
}
if (win.gOutput.c1.g1.cb1.value) {
jpgsaveFile = File(saveFile + '.jpg');
saveJPEG(jpgsaveFile, (win.gOutput.c1.g1.dd1.selection.index + 1));
}
if (win.gOutput.c3.g2.cb1.value) {
pdfsaveFile = File(saveFile + '.pdf');
savePDF(pdfsaveFile);
}
if (win.gOutput.c1.g2.cb1.value) { // Safe For Web JPG
sfwsaveFile = File(saveFile + '.jpg');
saveJPEGweb(sfwsaveFile, (win.gOutput.c1.g2.dd1.selection.index + 1));
}
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
}
};
win.onShow = function () { readSettings(); };
win.onClose = function () { saveSettings(); };
win.center();
win.show();
function saveTIFF(saveFile, Comp) {
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.embedColorProfile = true;
tiffSaveOptions.transparency = true;
tiffSaveOptions.interleaveChannels = true;
tiffSaveOptions.alphaChannels = false;
switch (Number(Comp)) {
case 0: tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; break;
case 1: tiffSaveOptions.imageCompression = TIFFEncoding.TIFFZIP; break;
case 2: tiffSaveOptions.imageCompression = TIFFEncoding.JPEG; break;
case 3: tiffSaveOptions.imageCompression = TIFFEncoding.NONE; break;
default: break;
}
activeDocument.saveAs(File(saveFile + '.tif'), tiffSaveOptions, true, Extension.LOWERCASE);
}
function savePSD(saveFile) {
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
psdSaveOptions.layers = true;
activeDocument.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
}
function savePDF(saveFile) {
pdfSaveOptions = new PDFSaveOptions();
activeDocument.saveAs(saveFile, pdfSaveOptions, true, Extension.LOWERCASE);
}
function saveJPEG(saveFile, Quality) {
var doc = activeDocument;
if (doc.bitsPerChannel !== BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = Quality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
}
function saveJPEGweb(saveFile, Quality) {
var doc = activeDocument;
var tmpName = File(File(saveFile).path + '/SFW_TEMP.jpg');
if (doc.bitsPerChannel !== BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = Quality;
activeDocument.exportDocument(tmpName, ExportType.SAVEFORWEB, sfwOptions);
tmpName.rename(decodeURI(File(saveFile).name));
}
function savePNGweb(saveFile, Quality) {
var doc = activeDocument;
if (doc.bitsPerChannel !== BitsPerChannelType.EIGHT) doc.bitsPerChannel = BitsPerChannelType.EIGHT;
var pngOpts = new ExportOptionsSaveForWeb();
pngOpts.format = SaveDocumentType.PNG;
pngOpts.PNG8 = false;
pngOpts.transparency = true;
pngOpts.interlaced = false;
pngOpts.quality = Quality;
activeDocument.exportDocument(new File(saveFile), ExportType.SAVEFORWEB, pngOpts);
}
function getActionSets() {
var i = 1;
var sets = [];
while (true) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID('ASet'), i);
var desc;
var lvl = $.level;
$.level = 0;
try { desc = executeActionGet(ref); } catch (e) { break; } finally { $.level = lvl; }
if (desc.hasKey(charIDToTypeID('Nm '))) {
var set = {};
set.index = i;
set.name = desc.getString(charIDToTypeID('Nm '));
set.toString = function () { return this.name; };
set.count = desc.getInteger(charIDToTypeID('NmbC'));
set.actions = [];
for (var j = 1; j <= set.count; j++) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID('Actn'), j);
ref.putIndex(charIDToTypeID('ASet'), set.index);
var adesc = executeActionGet(ref);
var actName = adesc.getString(charIDToTypeID('Nm '));
set.actions.push(actName);
}
sets.push(set);
}
i++;
}
return sets;
}
function getActions(aset) {
var i = 1;
var names = [];
if (!aset) throw 'Action set must be specified';
while (true) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID('ASet'), i);
var desc;
try { desc = executeActionGet(ref); } catch (e) { break; }
if (desc.hasKey(charIDToTypeID('Nm '))) {
var name = desc.getString(charIDToTypeID('Nm '));
if (name == aset) {
var count = desc.getInteger(charIDToTypeID('NmbC'));
var names = [];
for (var j = 1; j <= count; j++) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID('Actn'), j);
ref.putIndex(charIDToTypeID('ASet'), i);
var adesc = executeActionGet(ref);
var actName = adesc.getString(charIDToTypeID('Nm '));
names.push(actName);
}
break;
}
}
i++;
}
return names;
}
function fitImage(inWidth, inHeight) { // https://gist.github.com/kuyseng/2987454
var desc = new ActionDescriptor();
var unitPixels = charIDToTypeID('#Pxl');
desc.putUnitDouble(charIDToTypeID('Wdth'), unitPixels, inWidth);
desc.putUnitDouble(charIDToTypeID('Hght'), unitPixels, inHeight);
var runtimeEventID = stringIDToTypeID('3caa3434-cb67-11d1-bc43-0060b0a13dc4');
executeAction(runtimeEventID, desc, DialogModes.NO);
}
function rasterizePDF(pageNumber, res, mode, BitDepth, cropto, pdfFile) {
var desc = new ActionDescriptor();
var optionsDesc = new ActionDescriptor();
optionsDesc.putString(charIDToTypeID('Nm '), 'rasterized page');
optionsDesc.putEnumerated(charIDToTypeID('Crop'), stringIDToTypeID('cropTo'), stringIDToTypeID(cropto));
optionsDesc.putUnitDouble(charIDToTypeID('Rslt'), charIDToTypeID('#Rsl'), res);
optionsDesc.putEnumerated(charIDToTypeID('Md '), charIDToTypeID('ClrS'), charIDToTypeID(mode));
optionsDesc.putInteger(charIDToTypeID('Dpth'), BitDepth);
optionsDesc.putBoolean(charIDToTypeID('AntA'), true);
optionsDesc.putBoolean(stringIDToTypeID('suppressWarnings'), false);
optionsDesc.putEnumerated(charIDToTypeID('fsel'), stringIDToTypeID('pdfSelection'), stringIDToTypeID('page'));
optionsDesc.putInteger(charIDToTypeID('PgNm'), pageNumber);
desc.putObject(charIDToTypeID('As '), charIDToTypeID('PDFG'), optionsDesc);
desc.putPath(charIDToTypeID('null'), File(pdfFile));
executeAction(charIDToTypeID('Opn '), desc, DialogModes.NO);
}
function findAllFolders(srcFolderStr, destArray) {
var fileFolderArray = Folder(srcFolderStr).getFiles();
for (var i = 0; i < fileFolderArray.length; i++) {
var fileFoldObj = fileFolderArray[i];
if (fileFoldObj instanceof File) {} else {
destArray.push(Folder(fileFoldObj));
findAllFolders(fileFoldObj.toString(), destArray);
}
}
return destArray;
}
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;
}
function readSettings() {
try { settings = $.evalFile(settingsFile); } catch (e) { setDefaults(); }
win.gMode.g1.dd1.selection = settings.mode.colormode;
win.gMode.g1.dd2.selection = settings.mode.bitdepth;
win.gMode.g1.et1.text = settings.mode.resolution;
win.gRange.rb1.value = settings.range.mode[0];
win.gRange.rb2.value = settings.range.mode[1];
win.gRange.rb3.value = settings.range.mode[2];
win.gRange.et1.text = settings.range.from;
win.gRange.et2.text = settings.range.to;
win.gRange.rb1.onClick();
win.gIO.g1.rb1.value = settings.io.mode[0];
win.gIO.g1.rb2.value = settings.io.mode[1];
win.gIO.g1.rb3.value = settings.io.mode[2];
win.gIO.g2.et1.text = (function () {
if (win.gIO.g1.rb1.value) topLevelFolder = File(settings.io.source).exists ? File(settings.io.source) : '';
else topLevelFolder = Folder(settings.io.source).exists ? Folder(settings.io.source) : '';
return (topLevelFolder && decodeURI(topLevelFolder.fsName)) || '';
}());
win.gIO.g3.et1.text = (function () {
outputFolder = Folder(settings.io.destination).exists ? Folder(settings.io.destination) : '';
return (outputFolder && decodeURI(outputFolder.fsName)) || '';
}());
win.gIO.g4.cb1.value = settings.io.origfolder; win.gIO.g4.cb1.onClick();
win.gProc.g1.cb1.value = settings.processing.action.active; win.gProc.g1.cb1.onClick();
win.gProc.g1.dd1.selection = findIndex(settings.processing.action.set, win.gProc.g1.dd1.items);
win.gProc.g1.dd2.selection = findIndex(settings.processing.action.name, win.gProc.g1.dd2.items);
win.gProc.g2.cb1.value = settings.processing.resize.active; win.gProc.g2.cb1.onClick();
win.gProc.g2.et1.text = settings.processing.resize.width;
win.gProc.g2.et2.text = settings.processing.resize.height;
win.gProc.g2.dd1.selection = settings.processing.when;
win.gOutput.c1.g1.dd1.selection = settings.output.jpg.quality;
win.gOutput.c1.g1.cb1.value = settings.output.jpg.active; win.gOutput.c1.g1.cb1.onClick();
win.gOutput.c1.g2.dd1.selection = settings.output.jpgweb.quality;
win.gOutput.c1.g2.cb1.value = settings.output.jpgweb.active; win.gOutput.c1.g2.cb1.onClick();
win.gOutput.c2.g1.dd1.selection = settings.output.tiff.compression;
win.gOutput.c2.g1.cb1.value = settings.output.tiff.active; win.gOutput.c2.g1.cb1.onClick();
win.gOutput.c2.g2.dd1.selection = settings.output.png.quality;
win.gOutput.c2.g2.cb1.value = settings.output.png.active; win.gOutput.c2.g2.cb1.onClick();
win.gOutput.c3.g1.cb1.value = settings.output.psd.active;
win.gOutput.c3.g2.cb1.value = settings.output.pdf.active;
win.gMode.g2.dd1.selection = settings.options.crop;
win.gMode.g2.cb1.value = settings.options.flatten;
function findIndex(name, array) {
for (var i = 0; i < array.length; i++) if (array[i].toString() === name) return array[i].index;
return 0;
}
function setDefaults() {
if(settingsFile.exists) alert('Preferences were reset.\nEither the file was an old version, or it was corrupt.');
try { settingsFile.remove(); } catch (e) {}
settings = defaults;
}
}
function saveSettings() {
settings = {
mode: {
colormode: win.gMode.g1.dd1.selection.index,
bitdepth: win.gMode.g1.dd2.selection.index,
resolution: win.gMode.g1.et1.text
},
range: {
mode: [ win.gRange.rb1.value, win.gRange.rb2.value, win.gRange.rb3.value ],
from: win.gRange.et1.text,
to: win.gRange.et2.text
},
io: {
mode: [ win.gIO.g1.rb1.value, win.gIO.g1.rb2.value, win.gIO.g1.rb3.value ],
source: win.gIO.g2.et1.text,
destination: win.gIO.g3.et1.text,
origfolder: win.gIO.g4.cb1.value
},
processing: {
action: {
active: win.gProc.g1.cb1.value,
set: win.gProc.g1.dd1.selection.text,
name: win.gProc.g1.dd2.selection.text
},
resize: {
active: win.gProc.g2.cb1.value,
width: win.gProc.g2.et1.text,
height: win.gProc.g2.et2.text
},
when: win.gProc.g2.dd1.selection.index
},
output: {
jpg: { active: win.gOutput.c1.g1.cb1.value, quality: win.gOutput.c1.g1.dd1.selection.index },
jpgweb: { active: win.gOutput.c1.g2.cb1.value, quality: win.gOutput.c1.g2.dd1.selection.index },
tiff: { active: win.gOutput.c2.g1.cb1.value, compression: win.gOutput.c2.g1.dd1.selection.index },
png: { active: win.gOutput.c2.g2.cb1.value, quality: win.gOutput.c2.g2.dd1.selection.index },
psd: { active: win.gOutput.c3.g1.cb1.value },
pdf: { active: win.gOutput.c3.g2.cb1.value }
},
options: {
crop: win.gMode.g2.dd1.selection.index,
flatten: win.gMode.g2.cb1.value
}
};
try {
oldSettings = $.evalFile(settingsFile);
if (settings.toSource() === oldSettings.toSource()) return;
} catch (e) {}
settingsFile.open('w');
settingsFile.write(settings.toSource());
settingsFile.close();
}
}