InDesign scripting : lesson 11

this is the fourth in a series of lessons on exporting pdfs using applescript. lessons 08 and 09 showed how to export all open InDesign files at once. this next script will create separate pdfs for different page ranges within the one document. this is really handy in a publishing environment where you often export just portions of a file, rather than the whole document.

as with the first script, this one begins by creating an interface to get user input. InDesign offers scripters an excellent range of UI elements — this dialog has two sets of radio buttons and a text edit field. copy this into your script editor (found in applications > applescript) :

tell application "Adobe InDesign CS4"
  activate
  set mgDialog to make dialog with properties {name:"This script exports specified pages as separate PDFs"}
  tell mgDialog
    tell (make dialog column)
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Export preset to use :    "}
        end tell
        tell (make dialog column)
          set mgExportButtons to make radiobutton group
          tell mgExportButtons
            make radiobutton control with properties {static label:"Press Quality", checked state:true}
            make radiobutton control with properties {static label:"Inhouse Printing"}
            make radiobutton control with properties {static label:"Screen Resolution"}
          end tell
        end tell
      end tell
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Page range to export :    "}
        end tell
        tell (make dialog column)
          set mgPageRange to make text editbox with properties {edit contents:""}
        end tell
      end tell
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Export spreads as :    "}
        end tell
        tell (make dialog column)
          set mgSpreadsButtons to make radiobutton group
          tell mgSpreadsButtons
            make radiobutton control with properties {static label:"Separate pages", checked state:true}
            make radiobutton control with properties {static label:"Spreads"}
          end tell
        end tell
      end tell
    end tell
  end tell
  set mgResult to show mgDialog
end tell

that script gives you a dialog something like this :
screen grab of user interface created by script

well, that’s OK, but it’s a bit fugly. to get things lining up, you need to specify some minimum widths. the static text elements should all be the same width, and the interactive elements should all be the same width :

tell application "Adobe InDesign CS4"
  activate
  set mgDialog to make dialog with properties {name:"This script exports specified pages as separate PDFs"}
  tell mgDialog
    tell (make dialog column)
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Export preset to use :    ", min width:170}
        end tell
        tell (make dialog column)
          set mgExportButtons to make radiobutton group
          tell mgExportButtons
            make radiobutton control with properties {static label:"Press Quality", checked state:true}
            make radiobutton control with properties {static label:"Inhouse Printing", min width:150}
            make radiobutton control with properties {static label:"Screen Resolution"}
          end tell
        end tell
      end tell
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Page range to export :    ", min width:170}
        end tell
        tell (make dialog column)
          set mgPageRange to make text editbox with properties {edit contents:"", min width:150}
        end tell
      end tell
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Export spreads as :    ", min width:170}
        end tell
        tell (make dialog column)
          set mgSpreadsButtons to make radiobutton group
          tell mgSpreadsButtons
            make radiobutton control with properties {static label:"Separate pages", checked state:true}
            make radiobutton control with properties {static label:"Spreads", min width:150}
          end tell
        end tell
      end tell
      make static text with properties {static label:" "}
      make static text with properties {static label:"Commas = separate PDFs (eg. 1, 2, 3, 4 gives four PDFs)"}
      make static text with properties {static label:"Hyphens = the same PDF (eg. 1, 2-3, 4 gives three PDFs)"}
    end tell
  end tell
  set mgResult to show mgDialog
end tell

notice how the static text elements align right by default — you just have to get used to it because there’s no way around it. this version of the dialog also has a couple of hints for the user at the bottom :
screen grab of revised user interface

aawww — nicer. ok, now to capture the results of the dialog. you’ll remember from lesson 08 that you have to check that the ‘OK’ button is clicked, that radio buttons are numbered from 0 and that this is where you must use the exact name of your pdf export presets (case sensitive). this portion of the script comes immediately before the last end tell :

  if mgResult is true then
    set mgExport to selected button of mgExportButtons
    set mgPageList to edit contents of mgPageRange as string
    set mgSpreads to selected button of mgSpreadsButtons
    destroy mgDialog
  else
    destroy mgDialog
    error number -128
  end if
  
  if mgExport is 0 then
    set mgExport to "Offset Printing"
    set mgSubFolder to "Prepress Files"
  else if mgExport is 1 then
    set mgExport to "Xerox Printing"
    set mgSubFolder to "Xerox PDFs"
  else if mgExport is 2 then
    set mgExport to "[Smallest File Size]"
    set mgSubFolder to "Low Res PDFs"
  end if
  if mgSpreads is 1 then
    set mgSpreads to "true"
  end if

  set text item delimiters of AppleScript to " "
  set mgPageList to text items of mgPageList
  set text item delimiters of AppleScript to {""}
  set mgPageList to mgPageList as string
  set text item delimiters of AppleScript to ","
  set mgPageList to text items of mgPageList
  set text item delimiters of AppleScript to ""

now, you’ll understand most of that because you studied lesson 08 so well. here’s an explanation of that last bit where we process mgPageList through three coercions… imagine your user is a keyboard hack — a really shocking typist who enters this in the text editbox :

"1, 2-3, 14 -15, 26 - 27,   38,   49  "

you could just let them suffer and have the script fail, or you could take pity on them and add some processing to make everything right. the first coercion above gives you this monstrosity :

{"1,", "2-3,", "14", "-15,", "26", "-", "27,", "", "", "38,", "", "", "49", "", ""}

but don’t panic, because the second coercion renders this :

"1,2-3,14-15,26-27,38,49"

and the third coercion makes it all ok. a nice neat list, each item of which will be its own pdf. this example will export six pdfs :

{"1", "2-3", "14-15", "26-27", "38", "49"}

well, that’s it for now. we’ll complete this script in the next lesson. the finished script will add the page ranges to a truncated version of the filename — so exporting from “54321 ThisFile Name.indd” gives a pdf named, for example, “54321 ThisFile_2-3.pdf”. you’ll learn how flexible that process is — so you can tailor the renaming to your specific needs.

until then, keep grunting.

go to lesson 12

macgrunt icon

Advertisements

2 thoughts on “InDesign scripting : lesson 11

  1. Great tutorials.

    Any idea how to space a radio button group in 2 columns? i.e.

    • Top left • Top Right
    • Bottom left • Bottom Right

    Thanks

    • Hi Nik
      There’s no way to do this with pure Applescript.
      I expect it would be achievable with something like AppleScriptObjC, but I have no experience at all with the more advanced scripting possibilities.
      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