The below is obviously not the final script. But it illustrates a problem I’ve run into.
Make a text box, fill with text, select a word. Run the following script:
tell application "Adobe InCopy CS2"
if exists document 1 then
tell document 1
set mySel to object reference of selection
log mySel --> you get something like "(*text from character 911 to character 918*)"
end tell
end if
end tell
“text from character 911 to character 918” is apparently not the true value of the variable ‘mySel’ but a reference to the selected text because if I coerce it like this:
display dialog mySel as text
it displays the actual word that was selected, not “text from character 911 to character 918” as a string.
How do I coerce this variable into the string “text from character 911 to character 918”?
I created a text box in indesign text= “hello world”
selected “hello” and got character 1 to character 5 which is correct!
then ran this:
tell application "Adobe InDesign CS2"
if exists document 1 then
tell document 1
set mySel to text of object reference of selection
log mySel
end tell
end if
end tell
and got the word “hello”
am i missing something!
sorry for any confusion if i’m not understanding what your after.
My point is that I want “text from character 911 to character 918” (or whatever it may be) as a text string in a variable, not the actual word that was selected.
If I had the final script I’d post it but I need to do things such as determine if the text selected is part of the main text or of a note. If it’s part of a note, the text range selected (e.g. text from character 911 to character 918) will be larger than the character count of the story.
A couple of caveats: I’m actually writing the script for Adobe InCopy CS2, moreso than InDesign but I think few people here have access to InCopy and the scripting part is nearly identical between the two. And notes are not properly recognized as objects in InCopy/InDesign. If you select text within a note, you don’t get ‘text from character 2 to character 7 of note id 204 of text…’. The reference is only to the text, even though notes do exist as objects and have identifying IDs. So to identify if a selected range of text is part of a note or not, I need to do some workarounds.
If this all more confusing than enlightening, then my first question is still really all I’m after.
tell application "Adobe InDesign CS2"
if exists document 1 then
tell document 1
set mySel to object reference of selection --> text from character 266 to character 278 of text flow id 173 of document "Untitled-1"
log mySel --> (*text from character 266 to character 278*)
display dialog mySel
end tell
end if
end tell
Produces this error: ‘Adobe InDesign CS2 got an error: Can’t make text from character 266 to character 278 of story id 173 of document “Untitled-1” into type string.’
How would I coerce this variable to display “text from character 266 to character 278”?
Sorry!
Like i said i was slightly confused at what you wanted.
its not that easy to get the reference and actually use it as in a display dialog.
these guys may have had some luck though albeit a bit messy: http://bbs.applescript.net/viewtopic.php?id=14229
you can retrieve this information by catching the error and extract the part you need
tell application "Adobe InDesign CS2"
if exists document 1 then
tell document 1
set mySel to object reference of selection --> text from character 266 to character 278 of text flow id 173 of document "Untitled-1"
log mySel --> (*text from character 266 to character 278*)
try
display dialog mySel
on error e
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "Can't make "}
set objRef to text item 2 of e
set AppleScript's text item delimiters to " of "
set objRef to text item 1 of objRef
set AppleScript's text item delimiters to TID
end try
end tell
end if
end tell
objRef --> text from character 266 to character 278