InDesign scripting : lesson 08

this is the first of a series of lessons on exporting pdfs using applescript in script editor (found in applications > applescript). the end result of the first two lessons will be a script that will export all your open InDesign files while you go off to get a hot bevvie — rather than you having to sit there exporting each file individually. the script relies on pdf presets — you can use your own, or the defaults, or both.

first we’ll build a user interface (UI) which will ask you to select a pdf export preset. InDesign offers scripters an excellent range of UI elements — text fields, check boxes, dropdowns and, in this case, radio buttons. here is a very simple version of a UI and the script to produce it :

screen grab of simple export pdfs user interface

tell application "Adobe InDesign CS4"
  activate
  set mgDialog to make dialog with properties {name:"Export preset to use :  "}
  tell mgDialog
    make dialog column
    tell the result
      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
  show mgDialog
end tell

the basic structure is this : make dialog > put stuff in the dialog > show dialog. the observant will notice that in order to put stuff in a dialog you first need to make a dialog column. if you miss this step, it ain’t gonna work. you’ll also notice that you need to make a radiobutton group to contain the individual radiobutton controls. if you scroll right, you’ll see that you can set which of the radio buttons will be highlighted by default. you can add a bunch more radio buttons if you like, and you can label them however suits you — the labels do not have to exactly match the names of your pdf export presets — that bit comes later.

InDesign also let’s you play around with the layout a bit too — to give your UI windows a bit more presence. this comes in handy later when you start building more complex dialogs :

screen grab of export pdfs user interface

this version of the dialog has three dialog columns : the first one has a border around it and contains the other two ; the second contains the text “Export preset to use : ” ; and the third contains the radio buttons. here’s how to script it :

tell application "Adobe InDesign CS4"
  activate
  set mgDialog to make dialog with properties {name:"Export all open 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
    end tell
  end tell
  show mgDialog
end tell

this script uses the form “tell (make dialog column)” instead of “make dialog column > tell the result”. choose whichever suits you — one’s easier to understand, the other requires less lines of code.

next we need to capture the results of the dialog. first we need to determine which of the two buttons was clicked — ‘ok’ = true and ‘cancel’ = false. there are different ways to handle these two possibilities, here’s one — it goes immediately after show mgDialog, before that last end tell :

if result is true then
  set mgExport to selected button of mgExportButtons
  destroy mgDialog
else
  error number -128
end if

if the result is true then the ‘ok’ button was clicked — in which case we capture the radio button selected into a new variable (mgExport), ready for further processing. if the result is false then the cancel button was clicked — in which case we tell the script to quit immediately (error number -128). it’s good to get in the habit of destroying a dialog once you’ve captured the data you need. this prevents funky things happening in scripts that reference multiple dialogs.

ok, finally, we need to tell the script what to do, depending on the radio button selected. radio buttons are numbered from zero — so, for this script, mgExport could be 0, 1 or 2. again, this goes before the last end tell :

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

here is an example of how you can change the value of a variable — mgExport starts as a number (0, 1 or 2) but gets changed to the name of an export preset. this is where we have to use the exact name (case sensitive) of the Adobe PDF export preset. notice that default presets need to be in square brackets — that’s exactly as they are in the InDesign menu. the other thing we are doing here is setting the name for the folder the final PDFs will be saved in. this folder doesn’t have to already exist — the script will create it if it’s not there already.

ok, that’s it for the first part. we’re actually more than half way through. the next lesson will cover the guts of the export process.

go to lesson 09

macgrunt icon

Advertisements

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