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

11 thoughts on “InDesign scripting : lesson 25

  1. Hi sir, greetings from Canada!
    Not sure if this is still active since the last posts are easily 5 years old, but here goes anyhoo.
    i followed your script and am only missing the part that captures de bleed info in order to put it in my slug. I haven’t been able to figure out from your lesson 27. Any help would be greatly appreciated. Cheers!

    Btw, here’s the code i have at the moment which works fine other than the bleed info:
    set mgDate to do shell script “date ‘+%Hh%M • %d/%m/%y'”

    tell application id “com.adobe.InDesign”
    tell active document
    set mgPath to file path as string
    set mgName to name
    set text item delimiters of AppleScript to “:”
    set mgDossier to text item 3 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’)

    repeat with mgPage from 1 to count pages
    set mgWidth to item 4 of bounds of page mgPage
    set mgHeight to item 3 of bounds of page mgPage
    set mgDimensions to ((mgWidth as string) & ” x ” & mgHeight as string) & ” in”
    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

    tell table 1 of mgFrame
    set contents of cell 2 of row 2 to mgDossier
    set contents of cell 4 of row 1 to mgDimensions
    if contents of cell 5 of row 2 is “#” then
    set contents of cell 5 of row 2 to “1”
    else
    set contents of cell 5 of row 2 to (contents of cell 5 of row 2) + 1 as string
    end if
    set contents of cell 7 of row 6 to mgDate
    end tell
    end tell
    end repeat

    end tell
    end tell

    • G’day from Australia, Stephan

      Yes, I am no longer posting to this site, as other things are taking my time.
      But I am still regularly helping others with their scripts wherever I can.

      To capture bleed you can just use:
      set mgBleed to document bleed top offset of document preferences

      This would go before the repeat loop because it is common to all pages.

      If you need to capture the four separate bleeds (because bleed is not the same on all edges) you could do it like this :
      set mgBleedT to document bleed top offset of document preferences
      set mgBleedB to document bleed bottom offset of document preferences
      set mgBleedL to document bleed inside or left offset of document preferences
      set mgBleedR to document bleed outside or right offset of document preferences

      Hope this gets you closer

      m.

  2. macgrunt

    I’ve been working with manually entering data into my slugs for a long time and this is awesome.

    One thing I’ve been searching for and can’t figure out is if it’s possible to script the bleed dimension and live area (defined by margins) into the slug?

    Thanks

    • G’day Mike
      Apart from being tedious, entering slug data by hand is prone to errors. I’d prefer a machine to do it any day — I can’t be trusted.

      You can see how we capture bleed data in the other slug post : https://macgrunt.com/2012/11/29/indesign-scripting-lesson-27/
      Margins are captured through the Margin Preferences of the document
      So, yes — this is pretty easy to achieve. I don’t have time to put it together right now. But I can have a look at it tonight for you (about 14 hours from now).

      m.

  3. Thank you! this is so helpful for making my workflow more streamlined.

    I keep getting an error message
    “The variable mgFrame is not defined.” number -2753 from “mgFrame”

    I have put a script label on a text frame in the slug area of my document as instructed – what am I doing wrong? I don’t have a lot of experience with scripting, and I think InDesign knows!

    Thanks again

    • G’day Abby
      InDesign definitely knows — you need to show no fear.

      Make sure the script label in the InDesign file is “SLUG” (all caps, no spaces, without the quote marks)
      OR — you could replace “SLUG” in the script with whatever script label you have chosen for your InDesign file.

      Hope that helps.
      m.

  4. Hi macgrunt,

    I am getting the following error message:

    AppleScript Error!

    Error Number” -1708
    Error String: every page of doesn’t understand the count message.

    Engine: Default
    File:/Applications/Adobe Indesign CS6/Scripts/Scripts Panel/Samples/AppleScript/Slug.applescript

    Do you know of any possible fixes as scripting is beyond my capabilities?

    Thanks

    • g’day

      my initial suggestion would be to ensure you have copied the script across accurately.
      the script is failing on this line (or the line that should say) :
      repeat with mgPage from 1 to count pages

      i’ll flick you the complete script — see if you still have the same issue.

      m.

  5. I put it together and it seems correct –

    But I can’t get text item 2 of ” ” – number -1728
    Renamed document & moved to desktop etc.. keep getting same error.

    _____________
    tell current application
    do shell script “date ‘+%H:%M %d/%m/%y'”
    –> “07:47 02/10/12”
    end tell
    tell application “Adobe InDesign CS6”
    get file path of active document
    –> file “jobvol: TOM:208573 Polaris BC: Layouts:”
    get name of active document
    –> “208573 Polaris Proofs.indd”
    Result:
    error “Can’t get text item 2 of \”208573 Polaris Proofs\”.” number -1728 from text item 2 of “208573 Polaris Proofs”
    _____________

    Thank You for sharing all of this useful information with us!

    • G’day Tom

      You need to select the text item delimiter that is appropriate to your workflow.

      The code to separate the bits of filename in the post is :

      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 default

      which separates the filename “mg34567_letterhead.indd” into :
      mgShortName – mg34567_letterhead
      mgCode – mg34567
      mgJob – letterhead

      But your filename does not have any underscores. So using the above code you’ll get :
      mgShortName – 208573 Polaris Proofs
      mgCode – 208573 Polaris Proofs
      mgJob – {error}

      Since your filenames only have spaces, this is the code you need :

      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 items 2 thru -1 of mgShortName
      set mgJob to mgJob as string
      set text item delimiters of AppleScript to "" -- this is actually better than 'default'

      The first line starting “set mgJob…” will give you this — {“Polaris”, “Proofs”} — a list of items
      The second line starting “set mgJob…” will give you this — “Polaris Proofs” — the list items in a string separated by whatever the current text item delimiter is.

      See the difference to mgJob with these two variations :

      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 items 2 thru -1 of mgShortName
      set text item delimiters of AppleScript to "_"
      set mgJob to mgJob as string
      set text item delimiters of AppleScript to ""

      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 items 2 thru -1 of mgShortName
      set text item delimiters of AppleScript to ""
      set mgJob to mgJob as string

      Hope that makes things a bit clearer

      Good luck with it

      m.

Leave a reply to Stephan Cancel reply