this is the third of a series of lessons about how to script the contents of documents. so far we’ve looked at how to do things with items on a page. now it’s time to look at how to create those items in the first place.
if you want to follow along — open a new document. then run this script from script editor (found in applications > applescript) — as always, update the script to your version of InDesign :
tell application "Adobe InDesign CS4" activate tell active document make rectangle end tell end tell
pretty simple. eh? but who needs a tiny rectangle in the top left corner of the page? once the rectangle is created, change it to suit your needs :
tell application "Adobe InDesign CS4" activate tell active document make rectangle tell rectangle 1 set stroke color to "Black" set stroke weight to 2 set geometric bounds to {5, 5, 30, 50} end tell end tell end tell
notice that you don’t set the width and height of a page item — you set its geometric bounds. those four figures define, in order, the top, left, bottom and right edges of the rectangle. or, if you check your control panel, the Y and X coordinates of the top left corner, then the Y and X coordinates of the bottom right corner.
you can also set all the properties in one hit :
tell application "Adobe InDesign CS4" activate tell active document make rectangle tell rectangle 1 set properties to {stroke color:"Black", stroke weight:2, geometric bounds:{5, 5, 30, 50}} end tell end tell end tell
but the easiest way is to set the properties as you make the page item :
tell application "Adobe InDesign CS4" activate tell active document make rectangle with properties {stroke color:"Black", stroke weight:2, geometric bounds:{5, 5, 30, 50}} end tell end tell
there are lots of different properties you can set for rectangles (and other page items). the best way to learn what they are is to check the InDesign scripting dictionary. in script editor go file > open dictionary and choose InDesign from the list. enter ‘rectangle’ into the search field (top right corner) and look for the entry classified as ‘class’. here’s a screen grab of part of the CS2 entry (click to enlarge) :
the entries marked ‘r/o’ are read only — meaning you can’t change their value. all the others are at your mercy.
and now for your homework…
those of you who have been studying assiduously will be able to explain why this would fail when run on a blank document :
tell application "Adobe InDesign CS4" activate tell active document make rectangle with properties {stroke color:"Black", stroke weight:2, geometric bounds:{5, 5, 30, 50}} set properties of rectangle 1 to {content type:text type} select rectangle 1 end tell end tell
interestingly, the command that changes the content type could also be written like this in CS2 :
set content type of rectangle 1 to text type
but that functionality is broken in CS4 and CS5 (well, at least as far as script editor is concerned) — go figure.
next we’ll look at colours and swatches.