InDesign scripting : lesson 27

here is a variation on the slug form script from lesson 25. this version does not use a pre-existing form on the page — it creates a simple text frame holding information similar to the ‘page information’ you can add to your printouts, with three important improvements : it’s more readable ; it includes the page dimensions and ; it includes the user name.

the full version of the script prints the document and exports a pdf to an email attachment — functionality that you can find lessons for elsewhere on this site. today we’ll just focus on the core problem of creating the slug info which will look something like this :
screen grab of slug information

the first part of the script demonstrates a handy little scripting trick — the script property :

property mgUser : ""

if mgUser is "" then
  --bit to collect new user name
  tell application id "com.adobe.InDesign"
    activate
    set mgDialog to make dialog
    tell mgDialog
      tell (make dialog column)
        tell (make dialog row)
          make static text with properties {static label:"Enter the name you'd like to be known by."}
        end tell
        tell (make dialog row)
          set mgTextField to make text editbox with properties {edit contents:"Who you then?", min width:250}
        end tell
        tell (make dialog row)
          make static text with properties {static label:"(you won't be hassled with this again)"}
        end tell
      end tell
    end tell
    
    set mgResult to show mgDialog
    if mgResult is true then
      set mgUser to edit contents of mgTextField as string
      destroy mgDialog
    else
      error number -128
      destroy mgDialog
    end if
  end tell
  mgRunThisSucker()
else
  mgRunThisSucker()
end if

you’ll notice that we set the property mgUser to “” right at the start, and then go on to ask if mgUser is “”… — now that might seem nonsensical, but it’s not — because script properties have persistent data. the first time we run the script mgUser starts off as “” but is given a different value by the user when they enter their name in the dialog that’s generated (eg. ‘macgrunt’). that new value gets saved as an integral part of the script so that the next time it is run mgUser is “macgrunt”, not “”. by wrapping that whole script in an if/then statement, we ensure that the user only has to enter their name the first time the script is run. every subsequent run skips everything down to the second last line — mgRunThisSucker().

the user will be presented with a dialog something like this :
screen grab of user dialog
of course, you can change the various text elements in that dialog to read however you like.

the next part of the script captures a bunch of data we need :

on mgRunThisSucker()
  with timeout of 36000 seconds
    set mgHour to do shell script "date '+%H'"
    set mgMinute to do shell script "date '+%M'"
    if mgHour is less than 12 then
      set mgAmPm to "am"
    else
      set mgAmPm to "pm"
    end if
    if mgHour is more than 12 then
      set mgHour to mgHour - 12
    end if
    set mgDate to do shell script "date '+%d/%m/%y'"
    set mgDate to mgDate & "  :  " & mgHour & ":" & mgMinute & mgAmPm
    
    tell application id "com.adobe.InDesign"
      tell active document
        set mgName to name
        set mgFolder to file path
        set text item delimiters of AppleScript to "."
        set mgShortName to text item 1 of mgName
        set text item delimiters of AppleScript to ":"
        set mgClient to text item -4 of (mgFolder as string)
        set text item delimiters of AppleScript to ""

we’re capturing both the date and time for this version of the slug form. notice that …”date ‘+%H'” captures the hours in 24-hour time — but we want our time to appear as “2:00pm” rather than “14:00” — hence the bit of extra mucking around. if you’re happy with 24-hour time you could capture mgDate like this :

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

note also that the way you capture mgClient depends on where the InDesign file sits in your folder hierarchy — text item -2 of (mgFolder as string) is the folder containing the InDesign file — text item -3 of (mgFolder as string) is the next folder up, etc.

next we create a layer and paragraph style for the slug information (if they don’t already exist) and the slug offset at the bottom of the page :

        try
          set mgLayer to layer "SLUG"
          set locked of layer "SLUG" to false
          delete every text frame of layer "SLUG"
        on error
          set mgLayer to make layer with properties {name:"SLUG"}
        end try
        try
          set mgPara to paragraph style "SLUG"
        on error
          set mgPara to make paragraph style with properties {name:"SLUG", justification:left align, point size:7, applied font:"Minion Pro  Regular", fill color:"Black"}
        end try
        
        set mgBleed to document bleed bottom offset of document preferences
        set mgSlug to mgBleed + 5
        
        if slug bottom offset of document preferences is less than mgSlug then
          set properties of document preferences to {document slug uniform size:false}
          set properties of document preferences to {slug bottom offset:mgSlug}
        end if

the first part has a couple of try blocks — the first time the script is run on a particular file it will create a new layer and para style — but on subsequent runs the layer and style will already exist and trying to create them again will cause an error. so, instead, we cover off all eventualities with the try blocks. notice that the first try block deletes all the text frames on the slug layer — new frames are created with fresh content on each run. the rest of that part of the script is dedicated to setting the slug offset to 5mm outside the bleed (unless it’s already bigger than that).

the next part captures the rest of the data we need and puts all the elements together into one long text string (mgContents) :

        repeat with mgspread from 1 to count spreads
          set active spread of layout window 1 to spread mgspread
          tell active spread of layout window 1
            set mgPage to name of page 1
            set mgWidth to item 4 of bounds of page 1
            set mgWidth to mgWidth as integer
            set mgHeight to item 3 of bounds of page 1
            set mgHeight to mgHeight as integer
            set mgDimensions to ((mgHeight as string) & "x" & mgWidth as string) & "mm"
          end tell
          
          set mgContents to mgShortName & "  :  " & mgClient & "  :  page " & mgPage & "  :  " & mgDimensions & "  :  " & mgUser & "  :  " & mgDate

notice that we’ve opened a repeat loop here — because we want to add a separate slug to each spread (this script was created for a workflow where it is possible that a single file could have pages of varying sizes).

and finally the last bit — create the text frame, fill it with text, style the text, move the frame to the right layer and, when all frames are done, lock the layer so it can’t be inadvertently used for other content :

          set mgBounds1 to mgHeight + mgBleed
          set mgBounds3 to mgBounds1 + 4
          set mgFrameBounds to {mgBounds1, "0", mgBounds3, mgWidth}
          set mgSlug to make text frame in page mgPage with properties {geometric bounds:mgFrameBounds, fill color:swatch 1}
          set properties of text frame preferences of mgSlug to {inset spacing:{0, 2, 0, 2}}
          set vertical justification of text frame preferences of mgSlug to bottom align
          set contents of mgSlug to mgContents
          set applied paragraph style of parent story of mgSlug to "SLUG"
          move mgSlug to layer "SLUG"
        end repeat
        set locked of layer "SLUG" to true
      end tell
    end tell
  end timeout
end mgRunThisSucker

cobble all that together and you’ll have a script that looks something like this :
screen grab of entire script in script editor

save that as a script (.scpt) into your scripts panel folder. as discussed in the previous post, lesson 26, if you save it in text format (.applescript) the property mgUser will not be saved as persistent data and will have to be entered every time the script is run — and that would be tedious.

if you can’t be bothered compiling this yourself, you can go here to get the complete slug script

keep grunting

macgrunt icon

Advertisements

InDesign scripting : lesson 26

you learn something new every day — well, at least you should, if you’re paying sufficient attention.

way back in lesson 04 you’ll find this statement :

the first scripting lesson – lesson 01 – suggested you save your InDesign applescripts in script file format (.scpt). but there’s a very good reason why you should ignore such nonsensical advice and save them in text file format (.applescript) instead…

well, it turns out that there’s also a very good reason to ignore THAT and go on and save your scripts in .scpt format — the marvellous script property.

a script property is an immensely handy little scripting trick. it allows you to save data into a variable, within the script, so that it is still available the next time the script is run, and the next time. The data is persistent.

one example of the use of a script property is in the finder cleanup app. on the first run, this script captures the way finder windows are set so that, the next time the script is run, and every time after, those same finder windows can be opened in exactly the same place and pointing to exactly the same folders. so, the data is captured, then used over and over again.

but, if your InDesign script is saved in text format (.applescript) the data captured in the script property is not persistent. it works fine while the script is running, but the data is lost once the script ends and has to be reinstated the next time the script is run.

so, if you’re going to take advantage of a persistent script property you need to save your work in the .scpt format.

the next scripting lesson will show how a script property is used in a variation of the slug form script from lesson 25.

macgrunt icon