Created
May 29, 2019 11:05
-
-
Save orlissenberg/600544f25dfe524c764adc62f21202ec to your computer and use it in GitHub Desktop.
Export InDesign pages to individual files with custom file name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Export InDesign pages to individual files with custom file name. | |
* | |
* References: | |
* | |
* https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#ExportFormat.html | |
* https://github.com/ExtendScript/wiki/wiki | |
* | |
* License: MIT | |
*/ | |
// Start with the source document open | |
var sourceDoc = app.activeDocument; | |
// Get the item names | |
var names = []; | |
var items = sourceDoc.pages[1].allPageItems; | |
for (n = 0; n < items.length; n++) { | |
if (items[n].name !== '') { | |
names.push(items[n].name); | |
} | |
} | |
// Reverse to get the same order as the source document | |
names.reverse(); | |
// Select the target folder | |
var targetFolder = Folder.selectDialog("Select a folder"); | |
if (targetFolder != null && targetFolder instanceof Folder) { | |
targetFolder = targetFolder.fsName + "/"; | |
} else { | |
alert('Invalid target folder.'); | |
targetFolder = null; | |
} | |
if (targetFolder != null) { | |
for (p = 0; p < sourceDoc.pages.length; p++) { | |
var page = sourceDoc.pages[p]; | |
var newDoc = app.open(targetFolder + 'clean.indd', false); | |
for (i = 0; i < names.length; i++) { | |
var item = page.pageItems.itemByName(names[i]); | |
if (item.isValid) { | |
item.duplicate(newDoc.activeLayer); | |
} | |
} | |
var frame = newDoc.activeLayer.textFrames.itemByName("output_filename"); | |
// Trigger output format by file name | |
var fileName = targetFolder + frame.contents; | |
if (fileName.indexOf('.png') !== -1) { | |
newDoc.exportFile(ExportFormat.PNG_FORMAT, fileName); | |
} else { | |
if (fileName.indexOf('.jpg') === -1) { | |
fileName = fileName + '.jpg'; | |
} | |
newDoc.exportFile(ExportFormat.JPG, fileName); | |
} | |
newDoc.close(SaveOptions.NO); | |
} | |
alert('Finished exporting pages.'); | |
} else { | |
alert('Invalid target destination.') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment