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

Advertisements

8 thoughts on “InDesign scripting : lesson 12

  1. Macgrunt, thanks again for another great lesson. When I try and run the script on a file I get the following error.

    error “Can’t make text item 2 of \”638257_125_TIKGALA.indd\” into type Unicode text.” number -1700 from text item 2 of “638257_125_TIKGALA.indd” to Unicode text

    What’s this error about?
    Thanks, Richard.

    • G’day Richard
      Glad you’re finding something useful here.

      You need to adapt the script to your own filenaming convention.
      if the script says
      set text item delimiters of AppleScript to {" "}
      text item 1 will be “638257_125_TIKGALA.indd” and there will be NO text item 2 (because there are no spaces separating text elements in your filename)
      try
      set text item delimiters of AppleScript to {"_"}

      you should also change
      set text item delimiters of AppleScript to default
      to
      set text item delimiters of AppleScript to ""
      because the way I originally wrote it doesn’t seem to work any more
      (I’ll change it in the post too)

      hope this gets you closer to where you’re going
      m.

      • Hi macgrunt, my workflow requires the sequence number to appear at a certain spot in the filename, other than the beginning or end of the filename. Is there away to change the position of that sequence number or replace a common string in the filename with the sequence number?

        Thanks,
        Richard.

      • Absolutely
        Play around with the text item delimiters and how they break apart a text string, etc.
        Have another look at the middle of this lesson which shows three examples of how a filename can be broken apart.

        Start with this basic script, adjust it until you’re getting what you want, then plug the relevant bits back into your main script :

        set mgDocName to "638257_125_TIKGALA.indd"
        --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 "_NEWBIT_"
        set mgNewDocName to mgDocName as string
        set text item delimiters of AppleScript to ""
        return mgNewDocName

        Let me know how you go
        m.

  2. Is it possible to recognize the path of the indesign file and create subsequent PDFs that particular folder, as in, do not create in a subfolder?

    • G’day K
      There’s just a couple of things you need to change in the script.

      At the top of this post you can see a section which tells the finder to create the subfolder. You can simply delete that section or, alternatively, disable it by enclosing it in “(*” and “*)”, like this :

      (*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*)

      Then you just need to make sure that mgFilePath does not include a reference to the subfolder. So, change this :

      set mgFilePath to mgFolder & mgSubFolder & “:” & mgDocName & “_” & mgExportPage & “.pdf” as string

      to this :

      set mgFilePath to mgFolder & mgDocName & “_” & mgExportPage & “.pdf” as string

      Then you should be rockin’
      m.

  3. Macgrunt you’re’ awesome as always thanks for posting this.
    I’ll have to do some catching up with the last few posts.

    I tried this script and it worked beautifully for the low res setting.

    I need to take some time to understand it all lol I currently keep getting “Adobe InDesign CS5 got an error: No data available of the requested type.” when I try the other two buttons. I probably missed something important in the other posts.

    Thanks again for sharing your hard work with us noobs!

    • G’day Jocely — my number one fan. Glad you’re finding this stuff useful. It looks like you missed something important in Lesson 08 (or I may simply have explained it badly) right after the last code example. You need to change the values for ‘mgExport’ to the exact name of your own exports. The reason “Screen Resolution” works is that the equivalent mgExport in the example is the default that comes with Indesign – [Smallest File Size]. The other two mgExports are my own settings, using my own naming. Hope that makes sense. Good luck with it. m.

thoughtful and respectful comments welcome

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

WordPress.com Logo

You are commenting using your WordPress.com 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