Indesign CS3: trying to get rectangle with text in it

My ID CS3 documents have a graphic rectangle that has text placed in it. This rectangle is placed in a text frame and labeled with a text string. Think of a rectangle that will contain a signature graphic. As a QC measure the rectangle contains a text frame with this string. When the graphic comes in, the internal text frame is removed so the signature graphic is in place within the body of the letter.

I’m trying to get every one of these rectangles so that I can compare its label to a list and do something with it, like place its corresponding signature graphic.

OK, when I get the properties of this rectangle, I see that its object reference calls it a rectangle. But is is also called a text frame in all page items.

These all fail:


tell application "Adobe InDesign CS3"
	tell page 1
		set allRect to every rectangle --> fails
		set allRect to every text frame whose content type is graphic --> fails
		set allRect to every text frame whose label contains "CS46Signature" ----> fails
		set allRect to every page item whose label contains "CS46Signature" --> fails
		
		return count of allRect ---> 0
	end tell
end tell


I can get it to work without the embedded text inside it and outside the letter text frame. But that’s not the answer for this application.

Looking at the properties I’m not able to see what else I can try. I hope this is an easy one to answer, thanx, sam

I’m not in front of ID right now but you’re forgetting to talk to the document. You’re talking to the app and the page but the page belongs to the document, not the app.

Add a “tell document 1” (closing it properly with an “end tell”) and see how it works.

Cheers,
Jim

Im guessing that you are talking about labelled in-line graphics/boxes? If so then you should start with looking at the properties of your first object looking for something that you have put in a ‘rectangle’ carrying on from this you could then.

tell application "Adobe InDesign CS2"
	tell active document
		set x to properties of text frame 1
		set y to properties of item 1 of all page items of text frame 1
		set z to label of item 1 of all page items of text frame 1
		set myList to every item of all page items of text frame 1 whose label is "Marks Test"
	end tell
end tell

– returns {rectangle id 199 of text from character 135 to character 135 of text flow id 172 of document “Untitled-1”, rectangle id 201 of text from character 163 to character 163 of text flow id 172 of document “Untitled-1”, rectangle id 197 of text from character 192 to character 192 of text flow id 172 of document “Untitled-1”}

You can deal with this in a few ways, first is by page. The following should give you every text frame of the given label as theFrames and every inline frame with the given label as theEmbededFrames.

tell application "Adobe InDesign CS3"
	tell active document
		tell page 1
			set theFrames to every text frame whose label is "CS46Signature"
			set theEmbededFrames to every text frame of all page items whose label is "CS46Signature"
		end tell
	end tell
end tell

Addressing the document you get every text frame with the given label of every page that it appears on in theFrams and every inline frame of every story in theEmbededFrames. I find it best to work with stories when working with inline items if possible.

tell application "Adobe InDesign CS3"
	tell active document
		set theFrames to every text frame whose label is "CS46Signature"
		set theEmbededFrames to every text frame of every story whose label is "CS46Signature"
	end tell
end tell

Once you have you list you should just be able to put some graphic/image in each instance like so.

set myGraphic to (path to desktop as Unicode text) & "01.ai" as alias
tell application "Adobe InDesign CS2"
	tell active document
		set myList to every item of all page items of text frame 1 whose label is "Marks Test"
		repeat with thisItem in myList
			tell thisItem
				set x to place myGraphic
				fit x given proportionally
			end tell
		end repeat
	end tell
end tell

Yes, thank you Mark. I still think in Quark terms about addressing things directly. I see your point.

If I want to loop to get all the potential rectangles like this in the document should I iterate through every text frame to grab these? Sounds like a plan, thanx, again.

If you are looking for every rectangle, embedded in text or not in a document you should be able to use the following:

tell application "Adobe InDesign CS3"
	tell active document
		set theFrames to every rectangle whose label is "CS46Signature"
		set theEmbededFrames to every rectangle of every story whose label is "CS46Signature"
	end tell
end tell

where theFrames are those rectangles that are not embedded in the text flow and theEmbededFrames are those embedded in all the stories in the document.

Thank you Jerome and Mark.

I can confirm that Jerome’s simpler solution does work.

I’m stuck on stupid and I’m not seeing my issue. Following is a boiled-down version of my script and my attempt to incorporate Jerome’s rectangle tell.

The first portion goes through the text fields and applies the text merge. It works, but is not shown here since I’m stuck on image replacement via data merge. The second portion uses the image list built in the text iteration.

My stupid problem is that everything works except when I put it together. I’ve thought about this and looked at it fresh but it must be so obvious that I’m not seeing why I’m coming up with an empty list. In my attempt to figure out what I’m doing, you’ll see the comments the results I’m seeing. Everything is fine except in that last repeat statement when it comes up empty.

I’m hoping someone can see what I’m leaving out, thanx, sam


set dataImage to {}
tell application "Adobe InDesign CS3"
	tell data merge properties of document 1
		set myDataMergeProperties to properties
		set dmFcount to count of data merge fields -- of data merge 1
		--return dmFcount -- works
	end tell --datamerge prop	
	set myFields to field name of every data merge field of data merge 1 of document 1 --works
	set myFieldCount to count of myFields
	--return myFieldCount --works -->26
	set DMprop1 to properties of every data merge field of data merge 1 of document 1
	--return DMprop1 --works
	repeat with thisField from 1 to myFieldCount
		--return field type of item thisField of DMprop1 --works
		if (field type of item thisField of DMprop1 as string) contains "image" then copy (index of item thisField of DMprop1 as integer) to end of dataImage --as integer
	end repeat
end tell --app 1
--return dataImage

--this is broken separately here to solve a problem in how I address text data merge and the following image data merge

tell application "Adobe InDesign CS3"
	--return count of dataImage --works --> 1
	tell document 1
		repeat with thisFrame from 1 to (count of dataImage) --dataImageCount of (dataImage as list)
			--return (field name of data merge field (item thisFrame of dataImage as integer) of data merge 1) --works -->"CS46Signature_code__c"
			--return (every rectangle whose label is "CS46Signature_code__c") --works --> "rectangle id of ..."
			return (every rectangle whose label is (field name of data merge field (item thisFrame of dataImage as integer) of data merge 1)) --fails --> {}
			set allRect to (every rectangle whose label is (field name of data merge field (item thisFrame of dataImage as integer) of data merge 1)) --fails -->{}
		end repeat
	end tell
end tell


I’m don’t have a lot of experience with Data Merge. When I have needed to do something like this I just script placing the data as well. If I have time I will try to set something up to see what is going on, but an educated guess would be that there is a problem with this:

(field name of data merge field (item thisFrame of dataImage as integer) of data merge 1)

A common thing that ID might be doing that could cause the problem is returning a list for the field name instead of a string, so you get an error because it is really saying this:

set allRect to (every rectangle whose label is {"CS46Signature_code__c"})

If this is the case then you might try one of the following:

set allRect to (every rectangle whose label is (item 1 of (field name of data merge field (item thisFrame of dataImage as integer) of data merge 1))) 

set allRect to (every rectangle whose label is ((field name of data merge field (item thisFrame of dataImage as integer) of data merge 1) as string)) 

This is not tested in Script editor so I may have the parens wrong, but you get the direction I am going.

Another thing you might try changing it to:

set fieldName to (field name of data merge field (item thisFrame of dataImage as integer) of data merge 1) as string
set allRect to (every rectangle whose label is fieldName) 

Thank you Jerome for offering this bit of insight. You’re right in that a straight replacement of these items to the data is preferable, but for political reasons I’m having to use data merge. But it now seems to work.

Your examples coercing the list were used and the second one worked and is incorporated here. So it needed to be forced/coerced.

Thank you for helping me get over this last hump, sam



set dataImage to {}
tell application "Adobe InDesign CS3"
	tell data merge properties of document 1
		set myDataMergeProperties to properties
		set dmFcount to count of data merge fields -- of data merge 1
		--return dmFcount -- works
	end tell --datamerge prop	
	set myFields to field name of every data merge field of data merge 1 of document 1 --works
	set myFieldCount to count of myFields
	set DMprop1 to properties of every data merge field of data merge 1 of document 1
	repeat with thisField from 1 to myFieldCount
		if (field type of item thisField of DMprop1 as string) contains "image" then copy (index of item thisField of DMprop1 as integer) to end of dataImage --as integer
	end repeat
end tell --app 1
--this is broken separately here to solve a problem in how I address text data merge and the following image data merge

tell application "Adobe InDesign CS3"
	--return count of dataImage --works --> 1
	tell document 1
		repeat with thisImageSet from 1 to (count of dataImage) --dataImageCount of (dataImage as list)
			set allRect to (every rectangle whose label is ((field name of data merge field (item thisImageSet of dataImage as integer) of data merge 1) as string))
			repeat with thisRect in allRect
				set thisImg to data merge field (item thisImageSet of dataImage as integer) of data merge properties
				set myImagePlaceholder to make new data merge image placeholder with properties {field:thisImg, placeholder page item:thisRect} --(object reference of data merge field thisField of data merge 1)
			end repeat --iterate through each rect
		end repeat --each field in dataImage
		
	end tell --doc 1
end tell --app



For some strange reason the developers at Adobe like returning lists when you would think they should be returning strings. I’m sure that there is some reason for this madness on their end.