This very useful JavaScript code was developed by Peter Kahrel as he found my problem interesting. Bless you Peter! The full thread is over on the Adobe’s InDesign Scripting Forum here abouts. Because there’s only a limited life for any post over there – I’m dumping here too for posterity.

The purpose of the script is to export single page PDFs from a multi-page InDesign CS2 document where the page numbering has been set via page Numbering and Section Options. PDFs are exported to a predetermined PDF setting: “PDF_X-1a with Bleed & co” in my case. The resulting file PDFs are named with a three-digit prefix corresponding to the page number in the InDesign document and a user defined base name – defined when the script is run.

A basic version of this script is supplied by Adobe in with the extras for an InDesign install but unfortunately that falls over when there is a Named Section present. Getting a stray Named Section in your document is very easy. Easier for some users mind – but still very easy. Peter – I still owe you a beer or three…

Here is the magic script:

#target indesign 

if(app.documents.length != 0) 
   { 
   var myFolder = Folder.selectDialog ("Choose a Folder");   
   if(myFolder != null) 
      { 
      myExportPages(myFolder); 
      } 
   } 
else 
   { 
   alert("Please open a document and try again."); 
   } 

function myExportPages(myFolder) 
{ 
var myPageName, myFilePath, myFile; 
var myDocument = app.activeDocument; 
var myDocumentName = myDocument.name; 
//This next line sets the PDF export Setting 
var myPDFExportPreset = app.pdfExportPresets.item("PDF_X-1a with Bleed & co") 
var myDialog = app.dialogs.add(); 
with(myDialog.dialogColumns.add().dialogRows.add()) 
   { 
   staticTexts.add({staticLabel:"Base name:"}); 
   var myBaseNameField = textEditboxes.add({editContents:myDocumentName, minWidth:160}); 
   } 
var myResult = myDialog.show({name:"ExportPages"}); 
if(myResult == true) 
   { 
   var myBaseName = myBaseNameField.editContents; 
   myDialog.destroy(); 
   // Remember the state of each section prefix 
   var section_states = myDocument.sections.everyItem().includeSectionPrefix; 
   // Get all section-prefix names 
   var section_names = app.activeDocument.sections.everyItem().name; 
   // Enable all section prefixes 
   myDocument.sections.everyItem().includeSectionPrefix = true; 
   // Create two parallel arrays with page numbers: 
   // one with numbers preceded by section prefixes, 
   // the other with bare page numbers 
   var page_ranges = []; 
   var filename_pagenames = []; 
   for(var i = 0; i < myDocument.pages.length; i++) 
      { 
      page_ranges.push (myDocument.pages.item(i).name); 
      var temp = strip_section (myDocument.pages[i].name, section_names); 
      filename_pagenames.push (("00" + temp).slice(-3)) 
      } 
   // Restore the section-prefix settings 
   restore_section_states (myDocument, section_states); 
   // Export each page 
   for(var i = 0; i < myDocument.pages.length; i++) 
      { 
      app.pdfExportPreferences.pageRange = page_ranges[i]; 
      myFilePath = myFolder + "/" + filename_pagenames[i] + "-" + myBaseName + ".pdf"; 
      myFile = new File(myFilePath); 
      myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset); 
      } 
   } 
else 
   { 
   myDialog.destroy(); 
   } 
} 

function restore_section_states (doc, states) 
{ 
for (var i = 0; i < doc.sections.length; i++) 
   doc.sections[i].includeSectionPrefix = states[i]; 
} 

function strip_section (pg, array) 
{ 
for (var i = 0; i < array.length; i++) 
   if (array[i].length > 0) 
      if (pg.match (RegExp ('^'+array[i])) != null) 
         { 
         pg = pg.replace (RegExp ('^'+array[i]), ''); 
         break 
         } 
return pg 
}

Last Updated on