InDesign scripting : lesson 14

lesson 13 started talking about how to script the contents of documents — specifically, how to select items on a page. let’s continue by having a look at some things you can do with page items.

if you want to follow along — open a new document and put a bunch of different page items on the first page — make sure you have more than one ‘rectangle’ (text frames are not rectangles — even if they are rectangular). 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
    select every rectangle of page 1
    move selection by (5,1)
  end tell
end tell

the numbers in the brackets are the X (horizontal) and Y (vertical) locations and the unit increments will be whatever you have set as your default. you could replace those two lines with this instead if you like :

move rectangles of page 1 by (5,1)

notice the difference? the rectangles are moved without having to select them first. if you want to include items on the pasteboard, use this instead :

move rectangles of spread 1 by (5,1)

contrast the ‘move by’ command with the results of ‘move TO’ :

move rectangles of page 1 to (5,1)

which is similar to when you try to move items to a new page :

move rectangles of page 1 to page 2

that’s a problem if you want your page items to maintain their relative positions. you could get around the first problem like this :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    select rectangles of page 1
    set newGroup to make group with properties {group items:selection}
    move newGroup to {5, 1}
    ungroup newGroup
  end tell
end tell

… and the second problem like this :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    select rectangles of page 1
  end tell
  cut
  tell active document
    set newPage to page 2
    tell layout window 1
      set active page to newPage
    end tell
  end tell
  paste in place
end tell

notice in this one how you can not tell a document to cut and paste — you have to tell the application. when developing your own scripts, once you get a script like this to work — it’s a good idea to go back through it to see if it can be streamlined. here’s one way to simplify the previous script :

tell application "Adobe InDesign CS4"
  activate
  select rectangles of page 1 of active document
  cut
  set newPage to page 2 of active document
  set active page of layout window 1 of active document to newPage
  paste in place
end tell

this next script demonstrates a few other commands. before running it, make sure you have at least two rectangles on page 2. can you work out how InDesign determines which is rectangle 1 and which is rectangle 2? :

tell application "Adobe InDesign CS4"
  activate
  tell page 2 of active document
    set horizontal scale of rectangle 1 to 150
    set rotation angle of rectangle 1 to 10
    set stroke weight of rectangle 1 to 10
    set stroke color of rectangle 1 to "Paper"
    set shadow mode of rectangle 1 to drop
    set vertical scale of rectangle 2 to 200
    set fill color of rectangle 2 to "Black"
    set shear angle of rectangle 2 to 20
    set feather mode of rectangle 2 to standard
    set feather width of rectangle 2 to 5
  end tell
end tell

that’s it for now. the next lesson will show how to make new page items.

go to lesson 15

macgrunt icon

InDesign scripting : lesson 13

the first dozen lessons dealt with scripts that address InDesign documents or the application itself. the next few will focus on addressing the contents of files — the pages and the stuff on them. if you’re unconvinced that scripting the contents of files is useful to you — check out the short videos on youtube — one of those may change your mind.

the things on a page are collectively called page items — these include rectangles, text frames, ovals, polygons, graphic lines and, the frequently problematic, groups. fill a single-page file with different types of page items and then run this 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
    select every page item
  end tell
end tell

then change “page item” to “rectangle” and run it again. cool eh? you can use “rectangles” instead of “every rectangle” if it makes more sense to you.

if you wanted to select the rectangles and text frames but not the other page items, you could do it like this (notice that you can’t use “select every rectangle and every text frame”) :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    select every rectangle
    select every text frame existing selection add to
  end tell
end tell

well, that all works fine if you’ve only got stuff on one page. but it will fail with most documents because you cannot simultaneously select items on multiple pages — and “every rectangle” means every rectangle in the entire document. most of the time you need to specify a page or spread :

select every page item of page 3

you could also use “…in page 3″ — whatever takes your fancy.

previous lessons have mentioned how torturous applescript syntax can be. well, check this out — the way to select items of a particular layer of a particular page :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    select (page items of page 3 whose (name of item layer is "Layer 2"))
  end tell
end tell

all these examples have used the command “select”. no doubt you can work out what would happen if you substituted another command — such as “delete”, “duplicate” or “bring to front”.

go to lesson 14

macgrunt icon