Replace Inline Graphics in ID with Applescript

As you know, I’m fond of low-tech solutions. A fellow posted a request to a forum and asked how he might script replacing 4000 inline graphics based on a specification like its shape or fill color. The high-tech solution proposed was to programmatically select each inline graphic, then use a complicated script to redraw each one. He was happy with that solution (also I wasn’t able to respond in time due to lots of overtime at work). So I don’t question it. However, I wrote a script that would solve his problem in a low-tech way and I want to share it with you.

So here’s how a document like this might look (click on it to see a larger image):

We want to replace the red box with a blue square that has a rounded corner at the upper left and lower right. The red square is 1 pica by 1 pica and so should the new item be.

First you need to create the new inline graphic. You can use the pasteboard for this. Draw a square with the dimensions 1 pica wide by 1 pica tall. Color it blue. (For this demo, I created a swatch called “Red” and a swatch called “Blue”.)

Then go to Windows→Automation→Scripts. This will open up the scripts panel. Select the folder “Application” then double-click “CornerEffects.applescript”.

You ‘ll this dialog:

I’ve clicked on the “Rounded” radio button (also the default). I’ve put the offset at 3 pts. From the Pattern pulldown menu, I selected “odd points”. This will select the first and third corners (the upper left and lower right) of my blue square. Click okay. Here’s what I got (I zoomed in a bit):

Now I give the blue box a script label. Go to the script label tab in the Automation panel set, or go to Windows→Automation→Script Label. With the blue box selected, type in a label. I’m using “bluebox”.

Now it’s time to run my script. You will have to copy the script below into Script Editor, compile it, and save it:

(*Create a new graphic (the graphic to replace the old graphic), then give it a script label.*)

tell application “Adobe InDesign CS3”
–change bluebox to your label
select (first page item of document 1 whose label is “bluebox”)
copy –copies my blue box to the clipboard
set myItems to (get every page item of (every story of document 1))
repeat with anItem in myItems
if class of anItem is not group then
if name of fill color of anItem is “Red” then –change fill color to your color; you can also use another parameter for this, such as class
select insertion point 1 of parent of anItem
paste –pastes the blue box in first
delete anItem –deletes the red item
end if
end if
end repeat
end tell


Now if you look at the document again, you’ll see there’s another graphic, the “Cool Beans” in a green circle. This is a grouped item that is also an anchored item. To ignore this item, and not replace it, the conditional “if class is not group” is needed.

So here’s the document with changes made. (click on thumbnail for larger view)

As you can see, the script replaced all the inline red boxes with the new rounded corner blue box, but it ignored the grouped “Cool Beans” item. So that’s how to do it low-tech.

Please feel free to copy and use this script. If you want to redistribute it, just put a link to this blog in the description window in Script Editor.

Note: (You could also use GREP find and change, but there would be limits to what you can find in terms of inline graphics. You can’t find just inline graphics based on fill color, for example.)

See ya soon.

Advertisements

About this entry