InDesign scripting : lesson 24

waaay back in lesson 04 we looked at a script to automate printing (and lessons 05 and 06 explained how some of the script elements worked).

well, finally, it’s time to take printing automation to the next level. the original script prompts the user to select a print preset from a list. but that’s too much like hard work. what if the script could automatically decide which preset to use based on the document specs?

apologies to you lovely people in the States — we’re going to be using metric for this example because, well, it is the twenty first century after all and anything referred to as ‘imperial’ should be long dead.

the first part of the script captures a few bits of information from the document preferences. facing pages is boolean, so it returns true or false. page orientation returns landscape or portrait (square pages return portrait too). page height and width return real numbers in whatever measurement unit you’re using :

tell application "Adobe InDesign CS6"
  tell active document
    set mgPgFace to facing pages of document preferences as string
    set mgPgOrnt to page orientation of document preferences as string
    set mgPgHght to page height of document preferences
    set mgPgWdth to page width of document preferences
  end tell
end tell

display dialog (mgPgHght as string) & " : " & (mgPgWdth as string) & " : " & mgPgOrnt & " : " & mgPgFace

so, for a single A4 page the resulting dialog will look something like this :
screen grab of initial dialog

now, if you’re a neat freak (and aren’t we all?) you may prefer your real numbers converted to integers. notice that you can’t coerce a real to an integer as it is captured — the conversion has to be done separately :

tell application "Adobe InDesign CS6"
  tell active document
    set mgPgFace to facing pages of document preferences as string
    set mgPgOrnt to page orientation of document preferences as string
    set mgPgHght to page height of document preferences
    set mgPgWdth to page width of document preferences
    set mgPgHght to mgPgHght as integer
    set mgPgWdth to mgPgWdth as integer
  end tell
end tell

display dialog (mgPgHght as string) & " : " & (mgPgWdth as string) & " : " & mgPgOrnt & " : " & mgPgFace

then you’ll have neat (not necessary, but neat) :
screen grab of revised dialog
…and you can get rid of that display dialog line now — dialogs are a handy way to check your progress when developing a script.

ok, now, the complexity of the next bit is dependant on the complexity of your workflow. the workflow this script was developed for has only eight possible scenarios, roughly like so :
table showing print preset options for various document types

so, all we need to do is find out which of the scenarios our document falls into and select the corresponding print preset (which we’ll capture into the variable mgPrintPreset). let’s start with the two facing pages options (printed as spreads) :

if mgPgFace is "true" then --print document as spreads
  if mgPgHght is greater than 277 or mgPgWdth is greater than 200 then --use A3 fit preset
    set mgPrintPreset to "A3S fit"
  else --use A3 100% preset
    set mgPrintPreset to "A3S 100%"
  end if
else --print document as single pages
  --(scripting bits for single page options go here)
end if

this shows the basic approach to these kinds of problems — if/then/else. if something is true, then do this thing, else (otherwise) do this other thing. we just need to break the problem down into pairs of options.

and we can still use this method for eight different scenarios because if statements can also be nested — ifs within ifs. the full code for dealing with all eight possibilities looks like this :

if mgPgFace is "true" then --print document as spreads
  if mgPgHght is greater than 277 or mgPgWdth is greater than 200 then --use A3 fit preset
    set mgPrintPreset to "A3S fit"
  else --use A3 100% preset
    set mgPrintPreset to "A3S 100%"
  end if
else --print document as single pages
  if mgPgOrnt is "portrait" then --use one of the vertical presets
    if mgPgHght is greater than 277 or mgPgWdth is greater than 200 then --use one of the A3 presets
      if mgPgHght is greater than 400 or mgPgWdth is greater than 277 then --use A3 fit preset
        set mgPrintPreset to "A3V fit"
      else --use A3 100% preset
        set mgPrintPreset to "A3V 100%"
      end if
    else --use A4 vertical preset
      set mgPrintPreset to "A4V"
    end if
  else --use one of the horizontal presets
    if mgPgHght is greater than 200 or mgPgWdth is greater than 277 then --use one of the A3 presets
      if mgPgHght is greater than 277 or mgPgWdth is greater than 400 then --use A3 fit preset
        set mgPrintPreset to "A3H fit"
      else --use A3 100% preset
        set mgPrintPreset to "A3H 100%"
      end if
    else --use A4 horizontal preset
      set mgPrintPreset to "A4H"
    end if
  end if
end if

but it’s easier to understand in a screen grab of the code in script editor :
screen grab of this section of code in script editor

the bits in quotation marks are the names of the InDesign print presets, the green elements are the variables (remember you can name variables anything you like, just as long as it’s not a term reserved by applescript), and the other coloured bits are comments to help us keep track of what we’re doing. comments are preceded by two hyphens and are not actually part of the code — they’re little reminders that are especially helpful when revisiting the script sometime in the future.

the last line of code simply tells the document to print using the selected print preset :

print using (mgPrintPreset as string) without print dialog

cobble all those bits together and the complete script for this workflow looks something like this :
screen grab of complete script in script editor

now, obviously you’re not going to be able to use this one straight out of the box — you’ll have to do a little work to update it to your page sizes and print presets and whatnot to suit your workflow.

but once you’ve set it up you can do all your printing with a double click — rather than cmnd-p, click, choose, click, click …

OR, even better, reassign the cmnd-p shortcut to run your script.

bonza.

macgrunt icon

Advertisements

2 thoughts on “InDesign scripting : lesson 24

  1. What do you do if you have multiple layouts within a document (CS6)? (example 10 pages of portrait and 10 pages of landscape) I want to be able to print (not export) to 1 PDF document and my laser printers in 1 click….

    • G’day Kirtsy

      For printing you’d have to get the measurements page-by-page.
      If you wanted the printout to stay collated, you’d have to print page-by-page too, otherwise you could compile lists of page numbers based on their dimensions and then print batches to the various print presets (if you’re happy to hand-sort the pages later).

      Pages don’t have dimensions, they have bounds and you have to work out the dimensions from there.
      Maybe something like this (untested) :

      repeat with mgPage from 1 to count pages
      set mgPgWdth to item 4 of bounds of page mgPage
      set mgPgWdth to mgPgWdth as integer
      set mgPgHght to item 3 of bounds of page mgPage
      set mgPgHght to mgPgHght as integer
      --add in the if/then statements ...
      set page range of print preferences to mgPage
      print using (mgPrintPreset as string) without print dialog
      end repeat

      I’m not sure why (or how) you’d print a PDF instead of exporting.

      Hope that helps a little.
      Let me know how you go.

      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