-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportPDFInteractive.jsx
321 lines (277 loc) · 8.89 KB
/
ExportPDFInteractive.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
(function () {
var title = "Export PDF in Interactive";
// Script variables
var doneMessage;
var pdfPreset;
var pdfPresetNames;
var progress;
// Reusable UI variables
var g; // group
var p; // panel
var w; // window
// Permanent UI variables
var btnCancel;
var btnFolderInput;
var btnFolderOutput;
var btnOk;
var grpPreset;
var listPdfPresets;
var rbPdfInteractive;
var rbPdfPrint;
var txtFolderInput;
var txtFolderOutput;
var rbSameLocation;
var rbSelectFolder;
var outputPath;
var rbOptionReaderSpreads;
var rbOptionReaderPages;
// SETUP
// Load application PDF presets.
pdfPresetNames = app.pdfExportPresets.everyItem().name;
pdfPresetNames.sort();
// CREATE PROGRESS WINDOW
progress = new Window("palette", "Progress", undefined, {
closeButton: false,
});
progress.t = progress.add("statictext");
progress.t.preferredSize = [450, -1];
progress.b = progress.add("progressbar");
progress.b.preferredSize = [450, -1];
progress.display = function (message) {
message && (this.t.text = message);
this.show();
this.update();
};
progress.increment = function () {
this.b.value++;
};
progress.set = function (steps) {
this.b.value = 0;
this.b.minvalue = 0;
this.b.maxvalue = steps;
};
// CREATE USER INTERFACE
w = new Window("dialog", title);
w.alignChildren = "fill";
p = w.add("panel", undefined, "Input");
p.margins = [18, 18, 18, 18];
g = p.add("group");
g.alignment = "left";
btnFolderInput = g.add("button", undefined, "Folder...");
txtFolderInput = g.add("statictext", undefined, "", {
truncate: "middle",
});
txtFolderInput.preferredSize = [300, -1];
p = w.add("panel", undefined, "Export as");
p.margins = [18, 18, 18, 18];
g = p.add("group");
g.alignment = "left";
rbOptionReaderSpreads = g.add("radiobutton", undefined, "Spreads");
rbOptionReaderPages = g.add("radiobutton", undefined, "Pages");
p = w.add("panel", undefined, "Output");
p.margins = [18, 18, 18, 18];
g = p.add("group");
g.alignment = "left";
rbSameLocation = g.add("radiobutton", undefined, "Same .innd file location");
rbSelectFolder = g.add("radiobutton", undefined, "Select folder");
btnFolderOutput = g.add("button", undefined, "Folder...");
txtFolderOutput = g.add("statictext", undefined, "", {
truncate: "middle",
});
txtFolderOutput.preferredSize = [300, -1];
g = p.add("group");
g.alignment = "left";
rbPdfInteractive = g.add("radiobutton", undefined, "PDF Interactive");
rbPdfPrint = g.add("radiobutton", undefined, "PDF Print");
grpPreset = p.add("group");
grpPreset.alignment = "left";
grpPreset.add("statictext", undefined, "PDF preset:");
listPdfPresets = grpPreset.add("dropdownlist", undefined, pdfPresetNames);
rbOptionReaderSpreads.value = true;
rbSameLocation.value = true;
rbPdfInteractive.value = true;
btnFolderOutput.enabled = false;
// rbPdfPrint.value = true;
grpPreset.enabled = false;
g = w.add("group");
g.alignment = "center";
btnOk = g.add("button", undefined, "OK");
btnCancel = g.add("button", undefined, "Cancel");
// UI EVENT HANDLERS
btnFolderInput.onClick = function () {
var f = Folder.selectDialog();
if (f) {
txtFolderInput.text = f.fullName;
}
};
btnFolderOutput.onClick = function () {
var f = Folder.selectDialog();
if (f) {
txtFolderOutput.text = f.fullName;
}
};
rbPdfInteractive.onClick = function () {
if (this.value) {
grpPreset.enabled = false;
}
};
rbPdfPrint.onClick = function () {
if (this.value) {
grpPreset.enabled = true;
}
};
// Custom
rbSameLocation.onClick = function () {
if (this.value) {
btnFolderOutput.enabled = false;
}
};
rbSelectFolder.onClick = function () {
if (this.value) {
btnFolderOutput.enabled = true;
}
};
btnOk.onClick = function () {
if (!txtFolderInput.text) {
alert("Select input folder", " ", false);
return;
}
if (!txtFolderOutput.text && !rbSameLocation.value) {
alert("Select output folder", " ", false);
return;
}
if (grpPreset.enabled && !listPdfPresets.selection) {
alert("Select a PDF preset", " ", false);
return;
}
w.close(1);
};
btnCancel.onClick = function () {
w.close(0);
};
// SHOW THE WINDOW
if (w.show() == 1) {
try {
process();
alert(doneMessage || "Successful", title, false);
} catch (e) {
alert(
"An error has occurred.\nLine " + e.line + ": " + e.message,
title,
true
);
}
}
// function process() {
// var files;
// var i;
// // Ignore messages when opening documents.
// app.scriptPreferences.userInteractionLevel =
// UserInteractionLevels.NEVER_INTERACT;
// progress.display("Reading folder...");
// // Get InDesign files in folder.
// files = new Folder(txtFolderInput.text).getFiles("*.indd");
// if (!files.length) {
// doneMessage = "No files found in selected folder";
// return;
// }
// progress.set(files.length);
// try {
// // Loop through files array.
// for (i = 0; i < files.length; i++) {
// progress.display(File.decode(files[i].name));
// processFile(files[i]);
// }
// } finally {
// progress.close();
// }
// }
function process() {
var inputFolder = new Folder(txtFolderInput.text);
var files = getAllInDesignFiles(inputFolder);
var i;
app.scriptPreferences.userInteractionLevel =
UserInteractionLevels.NEVER_INTERACT;
progress.display("Processing files...");
progress.set(files.length);
try {
for (i = 0; i < files.length; i++) {
progress.display(File.decode(files[i].name));
processFile(files[i]);
}
} finally {
progress.close();
}
}
function getAllInDesignFiles(folder) {
var files = [];
var subFolders = folder.getFiles(function (file) {
return file instanceof Folder;
});
for (var i = 0; i < subFolders.length; i++) {
files = files.concat(getAllInDesignFiles(subFolders[i]));
}
var inddFiles = folder.getFiles("*.indd");
// inddFiles = inddFiles.filter(function (file) {
// return file.name !== "._"; // Exclude files named exactly "._"
// });
files = files.concat(inddFiles);
return files;
}
function processFile(file) {
var doc;
var filePdf;
doc = app.open(file);
try {
if (rbSameLocation.value) {
// Generate the output PDF file path in the same location as the InDesign file
outputPath =
file.parent.fsName + "/" + file.name.replace(/\.indd$/i, "") + ".pdf";
} else {
// Generate the output PDF file path in Select
outputPath =
txtFolderOutput.text +
"/" +
file.name.replace(/\.indd$/i, "") +
".pdf";
}
filePdf = new File(outputPath);
if (rbPdfInteractive.value) {
app.interactivePDFExportPreferences.properties = {
exportAsSinglePages: false,
exportLayers: true,
// exportReaderSpreads: true, // Set this to true to export as spreads
exportReaderSpreads: rbOptionReaderSpreads.value ? true : false, // Set this to true to export as spreads
flipPages: false,
flipPages: false,
flipPagesSpeed: 5,
generateThumbnails: true,
includeStructure: false,
interactivePDFInteractiveElementsOption:
InteractivePDFInteractiveElementsOptions.INCLUDE_ALL_MEDIA,
openInFullScreen: false,
pageRange: PageRange.ALL_PAGES,
pageTransitionOverride: PageTransitionOverrideOptions.FROM_DOCUMENT,
pdfDisplayTitle: PdfDisplayTitleOptions.DISPLAY_FILE_NAME,
pdfJPEGQuality: PDFJPEGQualityOptions.HIGH,
pdfMagnification: PdfMagnificationOptions.DEFAULT_VALUE,
pdfPageLayout: PageLayoutOptions.DEFAULT_VALUE,
pdfRasterCompression: PDFRasterCompressionOptions.JPEG_COMPRESSION,
rasterResolution: 300,
usePDFStructureForTabOrder: false,
viewPDF: false,
};
doc.exportFile(ExportFormat.INTERACTIVE_PDF, filePdf, false);
} else if (rbPdfPrint.value) {
// Get PDF preset to use.
pdfPreset = app.pdfExportPresets.item(listPdfPresets.selection.text);
// Set export preferences to all pages.
app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES;
doc.exportFile(ExportFormat.PDF_TYPE, filePdf, false, pdfPreset);
}
progress.increment();
} finally {
doc.close(SaveOptions.NO);
}
}
})();