InDesign scripting : lesson 15

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) :

screen grab of part of 'rectangles' entry in InDesign scripting dictionary

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.

go to lesson 16

macgrunt icon

Advertisements

InDesign tip : #13

InDesign offers some really helpful text editing features. this tip is mostly about working with the most pesky of text issues — overset text — text that falls outside the frame.

when you have text selected, the info panel gives you a count of characters, words, lines and paragraphs (click to enlarge) :

screen grab of info panel showing information about selected text

notice that the words, lines and paragraphs counts include partially selected elements — that is, for example, you do not have to select a whole word for it to be included in the count.

but you probably already knew that. now look what happens if you select ALL the text in a story with overset text (again, click to enlarge) :

screen grab of info panel showing information for overset text

the figures before the ‘+’ are for everything visible, the figures after are for the overset portion. so this story has 138 overset words. notice that InDesign doesn’t know how to determine how many lines that is.

now, many users still create an extra text frame on the pasteboard so they can see and edit the overset text. but there’s an easier way. select some text or a text frame and hit command-y. this opens up a story editor window — a great way to edit text (once you get used to it) :

screen grab showing closeup of story editor

story editor windows are especially good for editing lots of tiny text without having to continually zoom in and out of your page. select a whole bunch of caption frames, hit command-y and you’ll get a whole bunch of story editor windows. if you can’t see that left-hand column that shows the paragraph styles go to : view > story editor > show style name column.

but what’s this got to do with overset text? :

screen grab of story editor showing overset text

scroll down your story editor window and you’ll see that the overset portion is clearly marked. these overset markers are updated instantly as you edit text in the window. this is much easier than mucking about with creating and deleting superfluous text frames.

really, try editing in the story editor, once you get started you’ll wonder how you ever coped without it. and you can play around with the story editor preferences (InDesign > Preferences > Story Editor Display) until you get an interface that suits.

still not convinced? then check out Keith Gilbert’s Top Ten reasons to use the story editor feature. his claim that it is “one of the best features of InDesign” may be overstating it a bit. but it’s still a bloody handy tool.

macgrunt icon

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 tip : #12

since CS4 you have been able to place multiple images. the old way was to place only one image at a time — now you can load a whole bunch of images into the place cursor.

when you select more than one image to place, your cursor will have a little counter showing how many images are loaded and ready to place, plus a ghosted preview of the next image to be placed :

screen grab showing multi-image loaded cursor preview

if you have your links panel open you’ll notice that all the images are added to the panel BEFORE being placed on the page (they have a blank in the page column). the next image to be placed is marked “LP” :

screen grab showing loaded links panel

you can scroll through the list of upcoming images by using your up and down arrow keys to access the right one to place next.

if you accidentally load an image that you don’t need, you don’t have to place it and then delete it. just hit your escape key (top left) when that image is the next to place — it will be removed from the loaded cursor and the links panel.

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