InDesign scripting : lesson 34

ok, here’s the last lesson (for now) on exporting each page of an InDesign file as a separate pdf using applescript. lesson 32 showed a basic solution and lesson 33 expanded it to allow for more naming options. this lesson will look at how to generate the pdf filenames from information saved in the InDesign file itself. this solution was prompted by comment 39 in this discussion :

I’m using InDesign to manage 200+ pages of tech sheets for a company … The obvious benefit of using InDesign and having all the tech sheets in a single file is because then I can edit and update Master Pages and have the changes reflected in all of the tech sheets. The major downside though is that when exporting the PDFs … the file name must be specified manually for each tech sheet.

as always, there are many ways to skin a cat. we’re going to look at two approaches — naming based on the heading text of each page, and naming based on script labels. both solutions rely on setting up the InDesign file in a particular way and both solutions begin with this basic setup (refer to the previous lessons for an explanation of these parts of the script) :

tell application id "com.adobe.InDesign"
  set raster resolution of interactive PDF export preferences to one hundred forty four ppi
  set (export reader spreads of interactive PDF export preferences) to false
  set mgFolder to file path of active document
  set mgDocName to name of active document
end tell

tell application "Finder"
  if (exists folder "the pdfs" of folder mgFolder) is false then
    make folder at mgFolder with properties {name:"the pdfs"}
  end if
end tell

tell application id "com.adobe.InDesign"
  repeat with x in pages of active document
    set mgPageName to name of x
    set page range of interactive PDF export preferences to mgPageName
    tell active document


      set mgFilePath to mgFolder & "the pdfs:" & mgText & ".pdf" as string
      export format interactive PDF to mgFilePath without showing options
    end tell
  end repeat
  display dialog "ALL DONE!" buttons {"OK"} default button 1
end tell

here’s a snippet of our InDesign file :
screen grab of tech sheets indesign file

there are a few different approaches you could use to pull out the heading on each page as the filename for each pdf. but the easiest way is to set up the InDesign file so that those heading text frames are on their own layer (for this example we’ve called the layer ‘header layer’). so, each page has only one text frame on the header layer — the heading. here’s the remainder of the code to be plugged into the basic script above :

set mgFrames to (text frames of x whose name of item layer is "header layer")
set mgFrame to item 1 of mgFrames
set mgText to contents of item 1 of mgFrame

the first line makes a list of all the text frames on the page on the header layer (this should be a list with only one item). the second line references the first (and only) item in the list — the heading text frame. and the third line grabs the ‘contents’ of that frame (the heading text) to use as the pdf filename. running that completed script will give you this result :
screen grab of generated pdfs
simple, eh?

the alternative method allows you to specify a filename for each page without that filename actually appearing anywhere on the page — the invisible script label. script labels are awesome and were the key to the functioning of the calendars script. a script label can be assigned to any page item, but for this example we’re going to label the heading text frames.

you open the script label panel under windows > utilities. select the frame you want to label — then just type the new label into the panel. once you’re done, deselect the frame — don’t just go to a different page, the script label won’t stick — and don’t hit ‘return’, as this will become part of the label :
screen grab of script label panel

providing you have only one labelled item on each page, you only need to add one extra line to that original basic script above :

set mgText to get (label of every page item of x whose label is not "")

giving you these pdfs :
screen grab of second set of pdfs

another variation will also add the export date to the pdf filenames — this is a good way to keep track of which version you’re up to. we just need to add two more lines :

set mgDate to do shell script "date '+%d-%m-%y'"


set mgText to mgText & "_" & mgDate

so the finished script would look like this :
screen grab of final script to export by script labels

and would create pdfs like this :
screen grab of final dated pdfs

ok, so there we have it — a few variations on the exporting-each-page-as-a-separate-pdf theme. hopefully, from this brief introduction, you can see that there are any number of ways to get the job done with applescript. there’s a solution out there for just about any workflow.

keep grunting.

macgrunt icon