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

Advertisements

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s