InDesign scripting : lesson 18

ok — that’s enough lollygaggin’ around. let’s get back into it with a simple one. here’s how to shorten a filepath.

way back in lesson 12 you saw how to save a pdf into the same folder as your InDesign file (that script actually created a subfolder to contain the pdf — keeping everything nice and neat).

but sometimes you may want to go further UP the folder hierarchy. so, for example, if your InDesign file is here :

MacGrunt > Users > ThisUser > Documents > InDesign Files > TheDoc.indd

rather than accessing the folder containing the file — “InDesign Files” — you want to access the next one up — “Documents”. here’s how.

you get the file path of an InDesign document like this :

tell application "Adobe InDesign CS4"
  set theFilepath to file path of active document as string
end tell
return theFilepath

resulting in a string like this :

"MacGrunt:Users:ThisUser:Documents:InDesign Files:"

using text item delimiters, we can get the separate parts of that path :

set text item delimiters of AppleScript to {":"}
set theFilepathBits to text items of theFilepath
set text item delimiters of AppleScript to ""
return theFilepathBits

resulting in a list of elements :

{"MacGrunt", "Users", "ThisUser", "Documents", "InDesign Files", ""}

notice the end of the list is an empty item. no doubt some Applescript guru can explain why that happens.

we can then join the relevant bits back together :

set text item delimiters of AppleScript to ":"
set newFilepath to text items 1 thru 4 of theFilepath as string
set text item delimiters of AppleScript to ""
return newFilepath

giving us the string we’re looking for :

"MacGrunt:Users:ThisUser:Documents:"

now, that’s only going to work if your InDesign files are always six levels down in the hierarchy. but what we really want is a way to access the folder that’s one above the InDesign file no matter how deeply the file is buried.

luckily Applescript also lets you count backwards through a list. so, in the above example, “” is text item -1, “InDesign Files” is text item -2, etc.

so, what we need is this :

tell application "Adobe InDesign CS4"
  set theFilepath to file path of active document as string
end tell
set text item delimiters of AppleScript to ":"
set newFilepath to text items 1 thru -3 of theFilepath as string
set text item delimiters of AppleScript to ""
return newFilepath

that’s it — pretty simple, but pretty handy. with this little trick up your sleeve you’ll be throwing files all over the place with Applescript now.

keep grunting.

macgrunt icon

InDesign scripting : lesson 12

this is the fifth and final (for now) in a series of lessons on exporting pdfs using applescript. we started this script in lesson 11 which showed how to create an interface to capture some choices from the user. the finished script will export separate pdfs for different page ranges within the one document.

the next portion of the script does three things : it creates a folder in the same location as the InDesign file with the ‘mgSubFolder’ name specified in the first part of the script ; it creates a truncated version of the filename for us to append the page ranges to ; and it tells InDesign the pdf export preset we want to use :

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

set mgDocName to name of active document
set text item delimiters of AppleScript to {"."}
set mgDocName to text item 1 of mgDocName --strips extension from filename
set text item delimiters of AppleScript to {" "}
set mgDocName to (text item 1 of mgDocName & " " & text item 2 of mgDocName)
set text item delimiters of AppleScript to ""

set export reader spreads of PDF export preferences to false
set properties of PDF export preferences to properties of PDF export preset mgExport

that middle section is the place to play around with your own preference for how to name your pdfs. text item delimiters determine which character is used to separate bits of text — and those bits of text are called text items. these three examples should help to demonstrate :

set mgDocName to "54321 This_Is A-Weird File_Name.indd"
set text item delimiters of AppleScript to {"."}
return text items of mgDocName
-- > {"54321 This_Is A-Weird File_Name", "indd"}  - 2 text items
set mgDocName to "54321 This_Is A-Weird File_Name.indd"
set text item delimiters of AppleScript to {"_"}
return text items of mgDocName
-- > {"54321 This", "Is A-Weird File", "Name.indd"} - 3 text items
set mgDocName to "54321 This_Is A-Weird File_Name.indd"
set text item delimiters of AppleScript to {" "}
return text items of mgDocName
-- > {"54321", "This_Is", "A-Weird", "File_Name.indd"} - 4 text items

so, you need to change that middle section to suit your current filenaming protocol. ALWAYS set your text item delimiters back to the default “” before moving on.

ok, the last bit of the script repeats through the list of page ranges we captured in ‘mgPageList’ :

repeat with mgExportPage in mgPageList
  set page range of PDF export preferences to mgExportPage
  set mgFilePath to mgFolder & mgSubFolder & ":" & mgDocName & "_" & mgExportPage & ".pdf" as string
  if mgSpreads is "true" then
    if mgExportPage contains "-" then
      set export reader spreads of PDF export preferences to true
    end if
  end if
  tell active document
    export format PDF type to mgFilePath without showing options
  end tell
  set export reader spreads of PDF export preferences to false
end repeat

well, that’s it. it’s a good idea to add another dialog box at the end, just to let you know when all the exporting has completed. once you put all those bits together you should have a script which looks something like this (click to enlarge) :
screen grab of final Export As Separate PDFs script

or, if you’re feeling particularly lazy, you can get the completed script

macgrunt icon