InDesign CS3: Automating ANCHORED OBJECTS

Good afternoon,

I’m working on automating several complex processes in InDesign and have stumbled upon one of particular challenge. Personally, I’m inclined to figure it out because of the sheer challenge of it.

SUBJECT
Anchored Objects. There are some definitions for modifying anchored object settings within the CS3 AppleScript dictionary, however there is no direct way (as far as I can tell) to refer to the object itself.

GOAL
Place image into anchored object. (This will eventually be automated based on a folder of eps files and an indd template with corresponding anchored objects.)

some examples of the issues:

(copy and paste are used just to illustrate the anchored object issue)
My anchored object is placed directy after the first character (A) in text frame 1.

tell application "Adobe InDesign CS3"
	tell document 1
		get contents of text frame 1
		set selection to (contents of text frame 1 as string)
		copy
		paste
	end tell
end tell

that script returns the following error:

tell application "Adobe InDesign CS3"
	get contents of text frame 1 of document 1
		"A?
B
C
"
	get contents of text frame 1 of document 1
		"A<FFFC>
B
C
"
	set selection of document 1 to "A<FFFC>
B
C
"
		"Adobe InDesign CS3 got an error: Invalid value for set property 'selection'. Expected list of objects, object or nothing, but received \"A<FFFC>
B
C
\"."

Notice how the anchored object appears as (?) in the get statement, but as in the set statement? I researched a little into Unicode (UTF-8, <>) to no avail. I also tried pasting ASCII character 180 (“yen” symbol), which is what InDesign uses to represent the anchored object (when hidden characters are visible). This just resulted in a displayed yen symbol as text.

Any suggestions would be greatly appreciated!!

Thanks in advance!

Model: Intel Mac
AppleScript: Editor: 2.1.1 (81), AppleScript: 1.10.7
Browser: Firefox 2.0.0.9
Operating System: Mac OS X (10.4)

One more tidbit of info that might help:

I found that InDesign/AppleScript does have a way to refer to the object, despite the first glance.

My InDesign document consists of two linked text boxes whose contents are ABC and DEF (with returns between letters and column breaks between text frames). My anchored object is placed directly following the A in text frame 1.

tell application "Adobe InDesign CS3"
	tell document 1
		get all page items
		get all graphics
	end tell
end tell

Notice how the anchored object is not considered a page item, and as you have seen it is not well represented as part of the contents of a text frame. However, it is recognized as a graphic. Now, how do I refer to one anchored object in particular, in order to place an image in it?

tell application "Adobe InDesign CS3"
	get all page items of document 1
		{text frame id 211 of page id 178 of spread id 173 of document "anchor.indd", rectangle id 300 of text from character 2 to character 2 of text flow id 193 of document "anchor.indd", text frame id 237 of page id 178 of spread id 173 of document "anchor.indd"}
	get all graphics of document 1
		{PostScript picture id 302 of rectangle id 300 of text from character 2 to character 2 of text flow id 193 of document "anchor.indd"}
end tell

You are on the right path with the page items. Now looking at the dictionary, or just making a call to the properties of an object on a page we see that there is a property for all page items of a text frame as well as a character. These items are the anchored objects that are placed in the text flow. The list of all page items for an object is a list of the object references for that object, and this is best way to manipulate these items. So I could do the following:

set TheImage to (choose file) as string
tell application "Adobe InDesign CS2"
	set AnchoredObjects to all page items of text frame 1 of document 1
	repeat with i from 1 to count of AnchoredObjects
		place TheImage on (item i of AnchoredObjects)
	end repeat
end tell

This places the same image in every anchored rectangle in the text flow. Since I am getting the list from the text frame it does not include any rectangles which are not anchored in that text frame. Of course this assumes that there are only rectangles placed as inline graphics in the text flow and would need additional work to make it ignore inline text frames or groups of objects placed inline.

Now to try to target things a bit more exactly you could step through the text in some way and get the All Page Items property of a character which will be a list with a single item in it:

tell application "Adobe InDesign CS2"
	tell text frame 1 of page 1 of document 1
		set AnchoredObjects to all page items of character 3
	end tell
end tell

…and returns {rectangle id 196 of text from character 3 to character 3 of text flow id 173 of document “Untitled-1”}. Now you can use that item to refer to the inline object and place a graphic or manipulate it in some other way.

Hope this helps work out the problem.
Jerome

Jerome,

PHENOMENAL!!! That was EXACTLY what I needed. After a bit of playing with your suggestions and adding a few more lines, I was able to get everything working just fine. I replaced “text frame” for “story” and it suits my needs perfectly!

THANKS A TON!!!

Glad to help. I usually use story when dealing with text as well, it eliminates some problems when dealing with text that has moved to overset and just seems to work better overall.