InDesign scripting : lesson 09

this is the second of a series of lessons on exporting pdfs using applescript. we started this script in lesson 08 which showed how to create a UI (user interface) so the user can select a PDF export preset to use, and how to capture the resulting data into variables for use in the next part of the script. the finished script will export all open InDesign documents in one go — rather than you having to export each one separately. but, this script may still be useful to you if you only ever work on one document at a time.

ok, here we go. we’ve captured the name of the export preset we want to use in the variable mgExport. now we just need to tell InDesign :

set properties of PDF export preferences to properties of PDF export preset mgExport
set page range of PDF export preferences to all pages

hopefully, the first line is pretty self explanatory. you need the second line because the page range is not saved as part of the export preset. InDesign will default to whatever was used at the last export. so you have to reset it to export all pages.

the rest of the script is held within a repeat loop — to address each of the open documents in turn. this version of the script closes each document after export, so we can use this form of repeat statement :

repeat with mgDoc in every document

each pdf will be saved to a folder in the same location as the InDesign document. so, first we need to determine where the document is saved and then create the appropriate folder if it does not already exist. this is where we use the mgSubFolder variable we created in the first part of the script :

set mgFolder to file path of active document
tell application "Finder"
  if (exists folder mgSubFolder of folder mgFolder) is false then
    make new folder at mgFolder with properties {name:mgSubFolder}
  end if
end tell

the first line returns a colon delimited path something like this “MacGrunt:Users:ThisUser:Documents:”. a colon at the end of the path indicates that it is referring to a folder (we’ll use this information in a moment). the rest uses the finder to first check if the appropriate folder is there (exists) and create one if not (exists is false).

the next thing to do is determine the name for the pdf. this script uses the full name of the InDesign file minus the extension (.indd) — because you don’t want your pdf to be called ThisDocument.indd.pdf :

set mgDocName to name of active document
set text item delimiters of AppleScript to "."
set mgShortName to text item 1 of mgDocName
set text item delimiters of AppleScript to ""
set mgFilePath to mgFolder & mgSubFolder & ":" & mgShortName & ".pdf" as string

text item delimiters determine which character is used to separate bits of text. applescript’s default delimiter is “” (or nothing). by setting the delimiter to “.” we can extract everything before the “.” (your filename). text item 2 of mgDocName is everything after the “.” (ie. the extension indd). this, of course, assumes that you never use “.” as part of your filename which, of course, you never would.

that last line sets the final filepath of the pdf. this is where we use our knowledge that a colon is required to specify a folder. we need to add one to the filepath because the mgSubFolder variable (eg. “Prepress Files”) doesn’t contain a colon. that long last line results in the new variable, mgFilePath, looking something like this : “MacGrunt:Users:ThisUser:Documents:Prepress Files:ThisDocument.pdf”.

ok, now all we need to do is export the pdf. once that is done, we save and close the current document and close the repeat loop. this is the most straightforward part, because we’ve already done the hard work of setting the export preferences and the filepath :

  tell active document
    export format PDF type to mgFilePath without showing options
  end tell
  save active document
  close active document
end repeat

well, that’s it. once you put all those bits together you should have a script which looks like this (click to enlarge) :

screen grab of complete export PDFs script

save that into your scripts panel folder and you’ll be rocking.

the next lesson will show why the type of repeat loop we used here will only work if we close each file after export. if you want to keep all the files open, you’ll need a different form of the repeat loop statement. after that we’ll move on to a different type of pdf export script.

go to lesson 10

macgrunt icon


thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s