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

InDesign tip : #26

data merge — ooh, that sounds scary as hell — I’m not even going to think about looking at that.

… well, you probably should — there’s a pretty good chance you’ll have a use for it sometime. like scripting, data merge is just another way of automating a tedious, repetitive task. we’re going to use business cards as our example. first set up a file so it looks the way you want — this will be your basic template :
screen grab of business card layout

this artwork has a few elements that will stay the same across all cards and a few that will change for each card — a perfect job for data merge.
data merge relies on a well-constructed text file. the one for this job started as a spreadsheet that looks like this :
screen grab of spreadsheet of business card details

notice the top line has a label for each type of data element — this is important. that file was exported to a csv file, so it now looks like this :
screen grab of data from spreadsheet in csv form

back to InDesign now … open the data merge panel (in CS6 it’s under window > utilities >) and choose select data source from the dropdown menu to create a reference to your text file :
screen grab of data merge panel with dropdown menu

the data merge panel will now have a bunch of tags corresponding to the labels you created in your text file. add these tags to the relevant positions in your artwork. the easiest way to do this is to select the placeholder text in the artwork then click on the appropriate label in the data merge panel.
screen grab showing how to add tag to artwork
screen grab showing after tag is added to artwork

the preview check box will show you what the first completed card will look like. now you just need to save your template file then click the create merged documents button at the bottom of the panel. you’ll get a window like this to experiment with at your leisure :
screen grab of data merge options window

this will generate a new InDesign file with all your data in place — automatically creating as many pages as required (this example had seven lines of data in the csv, so seven pages were generated in the finished file) :
screen grab of one of the finished cards
awesome.

but that’s not all…
this method is great when it comes time to run out the next batch of business cards too. the text file appears as a link in the links panel of the template file. you can open, edit and update that file the same way as you would any other link :
screen grab of text file in links panel

then click create merged documents again and you’ve got yourself a whole new bunch of cards.

macgrunt icon

InDesign tip : #25

InDesign’s print dialog offers more information than you may first think.

here’s a screen grab of what your print dialog might look like by default — this one shows a tiled job previewed in the bottom left corner :
screen grab of standard print dialog

if you click on that preview you’ll be given some handy specs. particularly useful is the number of pages your tiled job is going to print on — something which is often hard to easily see in the first preview :
screen grab of second print dialog window

click the preview again and you get the last screen which can be useful if you’re using custom paper sizes or other media :
screen grab of third print dialog window

that preview also has an icon at bottom left showing the currently selected colour output mode :
composite grey previewGrayscale
composite c m y k previewCMYK
composite r g b previewRGB
separations previewSeparations

macgrunt icon