Skip to main content
March 2, 2020
Answered

Export separate layers as PDFs Script - Crashing sometimes

  • March 2, 2020
  • 1 reply
  • 383 views

Hi All,

I have a script (pasted below) that I use to export PDFs, each layer becomes a separate PDF.

The problem is that the larger the file, the more it crashes when outputting the larger (file size) layer, other times it will export the same PDF with no problem. I have tried changing different parts of the code but it still does it, it seems to happen on the "// Export File" line and leaves a "Zero K PDF" as it doesn't complete the export, does anyone have any suggestions on what could be the problem?

Many Thanks, Bren

 

pdfExport = "[High Quality Print]"
// Check InDesign is ready to run the script
try { saveLocPath = String(app.activeDocument.fullName).replace(/\..+$/, "") } catch(er){alert("Need a open document with a valid path to continue"), exit()} // Open Document?
if ( !app.pdfExportPresets.itemByName(pdfExport).isValid ) {alert("Requires \""+pdfExport+"\" PDF preset to run"), exit()} // Correct PDF Preset present

// Prepare variables to run
actDoc = app.activeDocument
layerLength = actDoc.layers.length
origHistory = actDoc.undoHistory.length // Start number of undos
app.pdfExportPreferences.pageRange = PageRange.ALL_PAGES; // Works with all pages
// Export separate Layers
app.activeDocument.layers.everyItem().visible = false; // Hides all Layers (also "visible" to "locked")
for (count = 0; count<layerLength; count++)
{actDoc.layers.item(count).visible ^= 1; // Toggle layer visibility
actDoc.exportFile(ExportFormat.PDF_TYPE, new File(saveLocPath+"_"+actDoc.layers.item(count).name+"_PTP"+".pdf"), false, app.pdfExportPresets.itemByName(pdfExport)) // Export File
app.activeDocument.layers.everyItem().visible = false; // Hides all Layers (also "visible" to "locked")
}
// Undo
undoFunc() // Run Function
function undoFunc() { newHistory = actDoc.undoHistory.length - origHistory // How many undo's to do
for (count = 0; count<newHistory; count++) {actDoc.undo()} // Undo to original state
}

This topic has been closed for replies.
Correct answer

Ok, so i've found out how to stop the crashing that seems to work, I added

$.sleep(3000); // Delay or short pause

just before the "// Export File" line and it seemed to sort it out, no idea why but it seems to prefer having a short pause to think about what it's done before it carries on.

1 reply

Correct answer
March 6, 2020

Ok, so i've found out how to stop the crashing that seems to work, I added

$.sleep(3000); // Delay or short pause

just before the "// Export File" line and it seemed to sort it out, no idea why but it seems to prefer having a short pause to think about what it's done before it carries on.