InDesign scripting : lesson 25

a slug form is a brilliant way to attach proofing data to an InDesign file — especially when you’re trying to keep track of which version of a job a client is signing off on.

there are many variations on the slug form, but all of them need to be filled out at some stage and many of them need to be updated with each new proof. here’s one that can be updated entirely automatically each time a pdf is exported — using applescript (click to enlarge) :
screen grab of a typical slug form

code and job are details pulled from the filename ;
client will be pulled from the document’s filepath ;
size and page are pulled from data for each page ;
proof is incremented by 1 each time the script is run ; and
proof date is gathered using a shell script.

for this example we’ll be using this document in this folder structure :
screen grab showing indesign file in folder hierarchy

the first part of the script starts capturing the data we need :

set mgDate to do shell script "date '+%d/%m/%y'"

tell application id "com.adobe.InDesign"
  tell active document
    set mgPath to file path as string
    set mgName to name
    
  end tell
end tell

the shell script grabs the current date and pulls the elements we want into a text string (mgDate) like this “31/09/12”. that’s the australian date format, if you wanted that weird month-day-year format you’d change it to :

set mgDate to do shell script "date '+%m/%d/%y'"

if you wanted to add the time as well, you could do it like this :

set mgDate to do shell script "date '+%H:%M %d/%m/%y'"

which would make mgDate something like “13:45 31/09/12”

we’ve also captured mgPath —
“Macintosh HD:macgrunt:clients:macgrizzle:mg34567_stationery:”
and mgName —
“mg34567_letterhead.indd”
which now have to be broken up to get at their bits. this is done with text item delimiters. a text item delimiter specifies which character is used to separate elements in a text string. in a sentence, the space character is used to separate words. in the file path above, the colon character is used to separate the folder names. so, here’s how we get the bits we need :

    set mgPath to file path as string
    set mgName to name
    set text item delimiters of AppleScript to ":"
    set mgClient to text item 4 of mgPath
    set text item delimiters of AppleScript to "."
    set mgShortName to text item 1 of mgName
    set text item delimiters of AppleScript to "_"
    set mgCode to text item 1 of mgShortName
    set mgJob to text item 2 of mgShortName
    set text item delimiters of AppleScript to "" --(updated from 'default')

this gives us three elements : mgClient = “macgrizzle”, mgCode = “mg34567”, and mgJob = “letterhead” (we’ve also captured mgShortName “mg34567_letterhead” which we could use when we export the PDF).

now, this workflow was created for InDesign files that could have different page sizes throughout and needed a separate slug for each page. so the next part gets placed within a repeat loop to address each page in turn :

    repeat with mgPage from 1 to count pages
      set mgWidth to item 4 of bounds of page mgPage
      set mgWidth to mgWidth as integer
      set mgHeight to item 3 of bounds of page mgPage
      set mgHeight to mgHeight as integer
      set mgDimensions to ((mgWidth as string) & "x" & mgHeight as string) & "mm"

      set active page of layout window 1 to page mgPage
      tell active page of layout window 1
        try -- check to see that correct SLUG is on the page
          set mgFrame to ((text frame 1 of group 1) whose label is "SLUG")
        on error
          try
            set mgFrame to (text frame 1 whose label is "SLUG")
          on error
            display dialog "Your SLUG should be in place on every page." & return & return & "(Note : the correct SLUG has been given a script label to help with automation)"
          end try
        end try
      --bits to enter data into slug table here
      end tell
    end repeat

the first part captures the page number (mgPage) and the page size (mgDimensions). notice that a page does not have a ‘size’, it has bounds and we have to get the dimensions from that. bounds are expressed as a list of four numbers which define, in order, the top, left, bottom and right edges of the page. for an A4 page with mm as the measurement unit, the bounds will be approximately {0.0, 0.0, 297.0, 210.0}. if you’re in the habit of moving your origin, you’ll need to change the way you capture the dimensions which may then be something like {-100.0, -50.0, 197, 160} for example.

the next part checks if there’s a slug on the page (mgFrame). the easiest way to do this is to give the text frame in InDesign a script label. you select the text frame and then choose window > utilities > script label (in CS6 — it might be in a different place in other versions of InDesign) and then type in whatever label you are planning to use. DO NOT hit return/enter, as this will become part of your script label — simply click outside the window instead. script labels are case sensitive :
screen grab of script label window

we check for mgFrame twice — first to see if it’s part of a group (the way the slug was originally set up) and then again in case someone inadvertently ungrouped the slug. this is called error handling — we don’t want the whole script to fail just because the slug is ungrouped.

o … k … now we’ve got all the bits of data we need and we just need to plug them all into the slug. to understand the next bit it helps to know how the slug frame has been created — it’s actually a table with three rows — six cells in the first row, eight cells in the second row and one cell in the third row (again, click to enlarge) :
screen grab of slug showing cell breakdown

we captured the text frame (mgFrame) now we need to address the table within that frame :

        tell table 1 of mgFrame
          set contents of cell 2 of row 1 to mgCode
          set contents of cell 4 of row 1 to mgJob
          set contents of cell 6 of row 1 to mgClient
          set contents of cell 2 of row 2 to mgDimensions
          set contents of cell 4 of row 2 to mgPage as string
          if contents of cell 6 of row 2 is "#" then
            set contents of cell 6 of row 2 to "1"
          else
            set contents of cell 6 of row 2 to (contents of cell 6 of row 2) + 1 as string
          end if
          set contents of cell 8 of row 2 to mgDate
        end tell

hopefully that all makes sense. if not, flick a question through. when it’s done its thing the slug will look something like this :
screen grab of completed slug
bonza.

once that process has been repeated for all the pages it’s time to export the pdf proof. we’re not going to go into all that here — you can see how to export pdfs in lesson 08, lesson 11 and lesson 18

when you’re done cobbling all those bits together you’ll have a script that looks something like this :
screen grab of script in script editor

rippa rita

macgrunt icon

Advertisements