InDesign scripting : lesson 31

in lesson 30 we looked at an applescript to change rows of a particular height to a new height throughout an entire InDesign table. here’s an example of slightly more complex table formatting.

no doubt you would use table and cell styles when setting up your own tables — to assist with formatting changes further down the track. but sometimes you need to work on tables that other, less diligent InDesign users have set up. and if you’re doing that on a regular basis, or if the tables are extensive, it makes sense to automate the tedium.

we’ll look again at the table from the last lesson :
screen grab of table before formatting

in this table, all cells have the same settings :
screen grab of control panel showing table settings

but now we want the reversed row heights to be a little wider and the blank rows to be narrower. first we need to change all the rows to a specific height before working on the other rows. to do this we change the ‘at least’ specifier to ‘exactly’ by changing auto grow to false :

tell application id "com.adobe.InDesign"
  tell active document
    tell table 1 of selection
      set auto grow of every row to false
      set height of every row to 5
      -- rest of script to go here
    end tell
  end tell
end tell

then we can use the cell fill colour to address the reversed rows only :

set height of (every row whose name of fill color is "C=0 M=100 Y=100 K=50") to 6.5

we need to use a different tactic for the blank rows. here’s one way — address every row that comes before a reversed row :

repeat with x in (every row whose name of fill color is "C=0 M=100 Y=100 K=50")
  set mgName to name of x
  try
    set height of row (mgName - 1) to 2
  end try
end repeat

the ‘name’ of a row is its number — so the first row in a table is row 1. that’s why we need the try statement — the first reversed row is row 1 and there is no row 0 to change to a height of 2 — so the script would throw an error without that try.

if we run that script the table will look like this :
screen grab of table after initial script run

the only remaining problem, as the observant will notice, is that there is at least one cell (and probably more) which is now too small and has overset text. an extra line in the script will fix that :

set height of every cell whose overflows is true to 9

the compiled script is short and simple :
screen grab of compiled script

… and the completed table looks like this :
screen grab of completed table

this is just a taster for what can be done with table formatting through applescript. whether or not you end up using scripting with your own tables depends on your workflow, how big your tables are, how often you work with tables, etc, etc. sometimes table are just too small to justify the time it takes to write or edit a script. but table scripting could be another powerful tool in your production arsenal — anything to minimise the monkey-work.

macgrunt icon

Advertisements

InDesign scripting : lesson 30

here’s a little quickie script to help those of you who like to specify exact row heights in your InDesign tables. changing column widths is easy, but changing row heights throughout a table is a pita.

let’s say you want to change all the reversed rows in a table like this, from 9mm high to 6mm high :
screen grab of original table

ordinarily you’d have to select each row, one after another, and set it to the new height manually, but with applescript we can automate it.

first we’ll create a dialog for the user to enter row heights into :

tell application id "com.adobe.InDesign"
  
  set mgDialog to make dialog
  tell mgDialog
    tell (make dialog column)
      tell (make dialog row)
        make static text with properties {static label:"Enter row height you'd like to change."}
      end tell
      tell (make dialog row)
        set mgOldHeightField to make text editbox with properties {edit contents:"current row height", min width:250}
      end tell
      tell (make dialog row)
        make static text with properties {static label:""}
      end tell
      tell (make dialog row)
        make static text with properties {static label:"Enter height you'd like it changed to."}
      end tell
      tell (make dialog row)
        set mgNewHeightField to make text editbox with properties {edit contents:"new row height", min width:250}
      end tell
      tell (make dialog row)
        make static text with properties {static label:""}
      end tell
    end tell
  end tell
  
  set mgResult to show mgDialog

  if mgResult is true then
    set mgOldHeight to edit contents of mgOldHeightField as number
    set mgNewHeight to edit contents of mgNewHeightField as number
    destroy mgDialog
  else
    error number -128
    destroy mgDialog
  end if

  -- rest of script to go here
  
end tell

first the specifications for the dialog are created, then the dialog is displayed to the user, then the results are captured into two variables — mgOldHeight and mgNewHeight. notice these variables are forced into numbers — for obvious reasons — so, if the user enters non-numerical data the script throws an error.

the resulting dialog looks like this :
screen grab of generated dialog

the rest of the script is simple as :

  tell active document
    tell table 1 of selection
      set height of (every row whose height is mgOldHeight) to mgNewHeight
    end tell
  end tell

here you’ll notice we’re addressing “table 1 of selection” which means that, for the script to work in this form, the first text frame containing the table must be selected (as shown in the screen grab above). you can also have all the text frames selected, but if you have nothing selected, or only the second frame selected, you’ll get an error.

that’s it. run the script, fill in your details, and all matching rows throughout the table will be changed almost instantaneously :
screen grab of filled dialog

screen grab of altered table

of course there are other ways to write the script — so that no selection is necessary, or every table in the document is addressed, or whatever — this is just the simplest form for this functionality.

the next scripting lesson will look a bit more at formatting InDesign tables with applescript.

til then, keep grunting.

macgrunt icon

InDesign scripting : lesson 29

the original version of this script was created, like so many scripts, in response to a problem. a necessary third-party export plugin was changing InDesign’s colour settings to a default custom set whenever it was used.

obviously, this is not an ideal situation for a colour-managed workflow and required each user to manually change the settings back every time the plug-in was run. needless to say, sometimes a user forgot to do this.

ordinarily, a script is not going to be of much use in this situation, because the user still needs to remember to run it. but this one takes advantage of yet another handy scripting feature supported by InDesign — the startup script. as you can probably guess, a startup script runs automatically whenever InDesign starts up — not an absolutely perfect solution to this problem, but at least a useful safeguard.

the structure of the script itself is quite simple :

tell application id "com.adobe.InDesign"
  
  set CMS settings of color settings to "macgrunt colour settings"
  
end tell

well, having gone that far, why not also reset a couple of other things, that might be periodically changed during the day, back to your preferred defaults at the start of the next day? this version of the script also resets some display preferences and the workspace :

tell application id "com.adobe.InDesign"
  
  set CMS settings of color settings to "macgrunt colour settings"
  set properties of display settings to {raster:proxy, vector:high resolution, transparency:medium quality}
  
  -----------------------------
  -- thanks to milligramme — adobe forums
  -- http://forums.adobe.com/message/3822734
  do script "app.applyWorkspace('macgrunt');" language javascript
  -----------------------------
  
end tell

that last command is an interesting one. if you look at the scripting dictionary for InDesign, you’ll see there’s an apply workspace command specified for applescript. problem is, it doesn’t work. but, as shown above, it’s possible to call the equivalent javascript using the do script command.

the finished compiled script looks like this :
screen grab of reset settings script

of course, you would need to substitute your own colour settings and workspace names.

well, that’s all very interesting, but how do you make this thing run every time InDesign starts up? rather than the scripts panel folder — just save it using the script file format into this folder :
Applications > Adobe InDesign CS6 > Scripts > startup scripts

what other things could you use a startup script for to help make your day a little easier?

macgrunt icon

InDesign scripting : lesson 28

we’ve already looked a fair bit at how to export pdfs from InDesign using applescript. see lessons 08, 09, 10, 11, and 12 plus InDesign scripting : bringing it together. but these scripts have relied on pdf export presets and, as it turns out, there’s a really good reason NOT to use an export preset when creating a pdf.

presets invariably export a ‘print’ pdf, but these often have tiny white lines criscrossing the screen. this is an unfortunate side effect of transparency flattening and is quite off-putting to clients, who incessantly ask “is it going to print like that?”. but if you export an ‘interactive’ pdf, you don’t get the dreaded white criscrossy lines (strange but true — who knows why?).

now, at this stage, you can’t save interactive pdf settings as export presets. but that don’t matter none — cause we got applescript.

this script is a stripped back version of a much larger one — with additional export options and email generation. this post will focus mainly on the syntax for exporting interactive pdfs — so refer back to the posts listed above if any of the other bits are confusing.

the first part of the script generates a dialog like this :

  set mgDialog to make dialog with properties {name:"How you want this exported then?"}
  tell mgDialog
    tell (make dialog column)
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"PDF export setting : ", min width:140}
        end tell
        tell (make dialog column)
          set mgResolutionButtons to make radiobutton group
          tell mgResolutionButtons
            make radiobutton control with properties {static label:"72ppi layout", checked state:true}
            make radiobutton control with properties {static label:"96ppi layout", min width:180}
            make radiobutton control with properties {static label:"144ppi layout"}
          end tell
        end tell
      end tell
      tell (make border panel)
        tell (make dialog column)
          make static text with properties {static label:"Document option : ", min width:140}
        end tell
        tell (make dialog column)
          set mgDocumentsButtons to make radiobutton group
          tell mgDocumentsButtons
            make radiobutton control with properties {static label:"active document only", checked state:true}
            make radiobutton control with properties {static label:"all open documents", min width:180}
          end tell
        end tell
      end tell
      
      set mgResult to show mgDialog
      if mgResult is true then
        set mgResolution to selected button of mgResolutionButtons
        set mgDocuments to selected button of mgDocumentsButtons
        destroy mgDialog
      else
        error number -128
      end if
    end tell
  end tell

that gives you a dialog that looks something like this :
screen grab of script dialog

and captures the results into two variables : mgResolution and mgDocuments

it’s important to remember that a radiobutton group returns an integer – starting at 0 – not the label assigned to the selected button. for example, if you select the second option in the top radio button group you will get ‘1’ as the result (not “96ppi layout”)

so, the next thing we need to do is assign variables depending on the choices made in the dialog :

  if mgDocuments is 0 then
    set mgDocList to name of active document as list
  else if mgDocuments is 1 then
    set mgDocList to name of every document
  end if
  if mgResolution is 0 then
    set mgResolution to seventy two ppi
  else if mgResolution is 1 then
    set mgResolution to ninety six ppi
  else if mgResolution is 2 then
    set mgResolution to one hundred forty four ppi
  end if
  
  my mgExportInteractive(mgResolution, mgDocList)

you’ll notice here that when re-specifying the variable mgResolution we don’t use quotation marks (it’s seventy two ppi not “seventy two ppi”). this is because the three options shown above are constants defined by the developers. if you want a different resolution you can use an integer between 72 and 300 (eg. set mgResolution to 200).

that last line passes the two final variables (mgResolution and mgDocList) to the subroutine that does the actual work (mgExportInteractive).

now, it’s not really necessary to use subroutines, but they are a great way to make scripts modular. it makes it easy to cut and paste whole chunks of reusable functionality into a future script (and you WILL do this as you get more and more into scripting).

there are two subroutines in this script, the second one is called from within the first one :

on mgExportInteractive(mgResolution, mgDocList)
  tell application id "com.adobe.InDesign"
    set properties of interactive PDF export preferences to {interactive PDF interactive elements option:include all media, generate thumbnails:true, pdf magnification:fit page, pdf page layout:single page, flip pages:false, PDF JPEG quality:maximum, open in full screen:false, page transition override:from document, PDF raster compression:JPEG compression, include structure:false, page range:all pages, export layers:false, export reader spreads:false, view PDF:false}
    set raster resolution of interactive PDF export preferences to mgResolution
    repeat with i from 1 to (count of mgDocList)
      set active document to (every document whose name is item i of mgDocList)
      my mgProcess()
      tell active document
        export format interactive PDF to mgFilePath without showing options
      end tell
    end repeat
  end tell
end mgExportInteractive

on mgProcess()
  tell application id "com.adobe.InDesign"
    set mgFolder to file path of active document
    set mgDocName to name of active document
    set text item delimiters of AppleScript to {"."}
    set mgDocName to text item 1 of mgDocName
    set text item delimiters of AppleScript to ""
    set mgFilePath to mgFolder & mgDocName & ".pdf" as string
  end tell
end mgProcess

now, if you scroll all the way out to the right, you’ll see an interesting thing. oh, all right, you don’t have to scroll all the way out — I’ll tell you — there’s a piece of code there that says “… export reader spreads:false …”. why is this interesting? because, in CS5 (the version this script was written for), exporting reader spreads in an interactive pdf is the default and there’s no option to change it in the standard export dialog box (this has been rectified by CS6). but you CAN change it through applescript — a rare case of something you can do with applescript which cannot be done with the native program. running a script containing that piece of code will change all future exports as well — so you may want to add another line to your final script to reset it back to the default “true” — it’s up to you.

so, that’s the basic syntax for exporting ‘interactive’ pdfs rather than ‘print’ pdfs. check the dictionary for all the other settings you can adjust through applescript.

to download the complete functioning script, you can go here.

have a good one

macgrunt icon

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