Hello,
Can anyone tell me why this script works when run within Script Editor, but not when run within InDesign CS2?
What it does:
- Cuts selected text into a note
- Enter three ‘end nested style’ characters at the beginning of the note
- Enter the user’s name and the word "CUT: " at the beginning at the note, following the ‘end nested style’ characters
- Move the insertion point outside the note, right after the note.
The script is actually intended to run in InCopy CS2, not InDesign CS2, but most people here probably don’t have that application so I’ve changed it to run in InDesign. It works in exactly the same way in InCopy.
To run it: Create a document, make a text frame, fill with text, then open the Story Editor and hilight a range of text. Then run the script.
What fails when it runs within InDesign is the second select of ‘every text of myNote’.
Admittedly, I’m using a number of workarounds to achieve what I want to do, as I find InDesign/InCopy particularly hard to script. For instance, I can’t figure out how to move the insertion point to right after note, so I instead select the content of the note and keystroke two right arrows. Incidentally, this is the part that fails when running the script within the application but works when run in Script Editor.
I appreciate any help and suggestions!
Michael
tell application "Adobe InDesign CS2"
set myName to user name & ": " -- prepare the text to go in the note (user name:)
set myCutName to myName & "CUT: " -- prepare the text to go in the note (user name: CUT:)
end tell
tell application "Adobe InDesign CS2"
tell item 1 of selection
set myNote to convert to note
--set myNoteOffset to story offset of myNote
--log "myNoteOffset is " & myNoteOffset
set myNoteText to get text of myNote --uncomment if the section below is commented
set text of myNote to myCutName & myNoteText --uncomment if the section below is commented
end tell
--> The following moves the cursor to the beginning of the note, enters the 'end nested style here' character three times. If a way to include the 'end nested style here' character in the myNoteText variable can be found, that would probably increase the speed of the script.
tell application "Adobe InDesign CS2"
activate
select every text of myNote
end tell
tell application "System Events" -- move cursor one step left (into the note)
tell process "Adobe InDesign CS2"
key code 123 -- left arrow, positions the cursor at the beginning of the note
key code 42 using {control down} -- end nested style character
key code 42 using {control down} -- end nested style character
key code 42 using {control down} -- end nested style character
end tell
end tell
--> The following moves the cursor to right after the note.
tell application "Adobe InDesign CS2"
activate
select every text of myNote
end tell
tell application "System Events" -- move cursor two steps to the right (after the note)
tell process "Adobe InDesign CS2"
key code 124 -- right arrow
key code 124 -- right arrow
end tell
end tell
end tell