Indesign CS3: object reference of text

I’m hoping this is simple. I know I can do a find what in Indesign to find a string and change it. But before I would do that I need to get its horizontal offset and baseline for another purpose.

Thus I want an object reference to a string. If I was in Quark I would get object reference of first text whose it=“string” or something like that.

Looking at the Dictionary, I see text is contained by Story so I try that. But I guess my approach is bungled. My blind attempts yield errors. How would you get an object reference of a particular piece of text?


tell application "Adobe InDesign CS3"
	tell every story of document 1
		set a to object reference of first text whose contents = "[Signature 1]" ---> fails here
		set sig1x to horizontal offset of a 
		set sig1y to baseline of a 
	end tell
end tell

thanks for your time, sam

I was able to get part way. Maybe this will get things rolling. I got it to the point where if you can identify the insertion point of that text, you can get the baseline and horizontal offset:

tell application "Adobe InDesign CS3"
	tell document 1
		set GetTextFirst to (parent story of (every text of (every text frame whose contents contains "[Signature 1]")))
		set sig1x to horizontal offset of (insertion point 20 of item 1 of GetTextFirst)
		set sig1y to baseline of (insertion point 20 of item 1 of GetTextFirst)
	end tell
end tell
{sig1x, sig1y}

Just not sure how to identify the insertion point of the text you are looking for.

For the record, I am also a former Quark scripter now working in InDesign and I usually end up using Find and Replace. The old Quark ways where you could replace “every text whose it is “blah”” in one line doesn’t work that way in InDesign.

Hope this helps.

Model: iMac Intel 10.5.5
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)

You could do something like this to get every word of every story whose contents is “[Signature” and work on it from there:

tell application "Adobe InDesign CS3"
	set a to object reference of (words of every story of document 1 whose contents is "[Signature")
	repeat with b in a
		set sig1x to horizontal offset of b
		set sig1y to baseline of b
	end repeat
end tell

I tried using first word in various ways and it seems to return the first story with the word, not the first word of every story.

If you just need the first instance of each you could do something like this:

tell application "Adobe InDesign CS3"
	tell document 1
		set a to (parent story of (every text of (every text frame whose contents contains "[Signature 1]")))
		set b to {}
		repeat with i from 1 to count of a
			set end of b to object reference of (first word of item i of a whose contents is "[Signature")
		end repeat
		repeat with c in b
			set sig1x to horizontal offset of c
			set sig1y to baseline of c
		end repeat
	end tell
end tell

Whose filtration isn’t suited for obtaining references to strings (without clunky workarounds). A found text is a referenced object, hence its foundness. :smiley:

tell application "Adobe InDesign CS3"'s document 1 to set {sig1_HO, sig1_base} to (find text "[Signature 1]")'s item 1's {horizontal offset, baseline}

getting the following error on testing: Adobe InDesign CS3 got an error: Object contains no text for find/change.

Hey Jerome,

I typed some of that from memory, so it’s possible that I mistyped or incorrectly prioritized something, but I won’t have access to CS3 until tomorrow and can’t check on that until then. One possible source for the error message is the fact that I didn’t error trap; I should’ve reset the find/change values to nothing. You might want to go in there and manually check that there are no specific search options in affect.

No problem Mark, I was intrigued by what you wrote, I have never used this and it’s nice to learn something new.

Thanks very much. This looks to be elegant.

I am able to get Marc’s snippet to work. It yields {1.255652804298, 7.044191689896} which for the life of me, I cannot determine why Indesign yields this. You cannot see my test document but it’s not near it at all and it’s the only instance of this string.

My selection snippet yields a more accurate response:


tell application "Adobe InDesign CS3"
	set a to object reference of selection
	set q to {horizontal offset of a, baseline of a}
end tell

it yields the accurate response: {3.0, 3.875651041667}

But the above is not acceptable for automation.

What other info would be needed to help solve this discrepancy? Changing item 1 to 2 yields an expected error. There is no second instance of this string.

thanx, sam

I think that form used to work in CS2 but in CS3 you have to set all your find preferences up and then say “find text” to perform the find with the preferences you set up. This seems to work for me. You can change or delete the prefs that don’t matter for what you are doing:

tell application "Adobe InDesign CS3"
	set find text preferences to nothing
	set case sensitive of find change text options to false
	set include footnotes of find change text options to false
	set include hidden layers of find change text options to false
	set include locked layers for find of find change text options to true
	set include locked stories for find of find change text options to true
	set include master pages of find change text options to false
	set whole word of find change text options to false
	set find what of find text preferences to "[Signature 1]"
	tell document 1
		set {sig1_HO, sig1_base} to (find text)'s item 1's {horizontal offset, baseline}
	end tell
end tell

{sig1_HO, sig1_base}

Model: iMac Intel 10.5.5
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)

Yes, thanks to you all. I can confirm this works.

This thread is a keeper as I’m always trying to find strings as objects, thanx, sam

I second that…Thanks Mark. I don’t script much with text but this looks to open up new doors in manipulating text in InDesign.

Jerome

Thanks again. I’m trying to extract more info from the object such as what page/spread it is on and what is its text insertion, or character numbers.

My first attempt was to add its object reference as string, but AS bails because it cannot convert “text from character 1235 to character …” as string. I thought I could set the beginning of the text insertion to “word 4 of (object reference as string)”
The parent of the text flow is the document which jumps past the insertion point. As you’ll see below, I was trying to use the tell me structure to disassociate it from within its tell structure as the text that makes up the object reference still means “[signature 1]” which has no word 4.


tell application "Adobe InDesign CS3"
	set {find text preferences's find what, find change text options} to {"[Signature 1]", nothing}
	tell document 1 to set {sig1_HO, sig1_base, sig1_textPar, textObjchar} to (find text)'s item 1's {horizontal offset, baseline, parent, object reference}
	set sig1Textframe to parent of textObjchar
	(*
tell me
		set sigChar1 to word 4 of (textObjchar as text) --as integer --word four is "1235"
	end tell
*)
	
end tell

I’m looking through the dictionary and other online guides but their examples fail. I know page/spread is not a property of the text, but I’m trying to move up the parent chain to get to it.

I’m wanting to place an inline graphic above the [Signature 1].

I wouldn’t think that all of this is out of scope for the ojbect reference, but I’m not seeing how to glean extra info from it. Thanks for your time, sam

What you need is the parent (the object reference of the page) of the parent text frames (returned as a list, so you will need item 1 of this). I checked this with a document of 2 pages with a text frame on each that were linked. The returned result is the page that the text is on and not the first one in the frame. You might need to add a loop to make sure that you have the page object by checking it’s class. If you need the page number get the name of the parent of (…) If you need the offset in the document use the document offset of the parent of (…)

tell application "Adobe InDesign CS3"
	set {find text preferences's find what, find change text options} to {"[Signature 1]", nothing}
	tell document 1 to set {sig1_HO, sig1_base, sig1_textPar, textObjchar} to (find text)'s item 1's {horizontal offset, baseline, parent, object reference}
	set sig1Textframe to parent of (item 1 of parent text frames) of textObjchar
	(*
tell me
		set sigChar1 to word 4 of (textObjchar as text) --as integer --word four is "1235"
	end tell
*)
	
end tell

Many thanks, that takes care of extracting the page number of this object.

Now, I’m trying to extract the text insertion point, or the first character of the “text from character 1245 to character …”

If I can get it as a string, then I can get “word 4 of object reference as string” to get the placement needed for an insertion point of the inline graphic. Object reference as string yields not “text from character …” but “[Signature 1]” back. I’ve played around with options to get this from the object reference itself, but they’re failing.

How would you get “1245” from the object reference? many thanx for all help so far, sam

As usual, I was overthinking it. It came to me a few hours later:


tell application "Adobe InDesign CS3"
	set {find text preferences's find what, find change text options} to {"[Signature 1]", nothing}
	tell document 1 to set {sig1_HO, sig1_base, sig1_textPar, textObjchar} to (find text)'s item 1's {horizontal offset, baseline, parent, object reference}
	set sig1Textframe to parent of (item 1 of parent text frames) of textObjchar
tell beginning of textObjchar to place "path:to:sig.tif" as alias 	
end tell

Now, the trick is to find a way to refer to this new graphic rectangle so I can apply some attributes to it. Adding “with properties {.}” didn’t cut it. Thanks, sam