Script works in Script Editor but not in Adobe InDesign CS2

Hello,

Can anyone tell me why this script works when run within Script Editor, but not when run within InDesign CS2?

What it does:

  1. Cuts selected text into a note
  2. Enter three ‘end nested style’ characters at the beginning of the note
  3. Enter the user’s name and the word "CUT: " at the beginning at the note, following the ‘end nested style’ characters
  4. 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

Anyone?

Thanks.

Michael

“Convert to note” actually deletes your text range, and both of your selection events return empty lists; this likely accounts for your issue. I don’t know what a note is useful for, but it seems that you probably need a reference instead.

The redirects to System Events might be replaced by a construction that sets the selection by comparison - “the character after the selection”, for instance. The end nested character part could be done with a find/replace or by inserting the ASCII equivalent of whatever that symbol is.

This may help:

tell application "Adobe InDesign CS2"
     tell the selection to set myNote to it
       set the end of myNote's item 1's text to (" " & (user name) & ": CUT: ")
  --insert your selection & move cursor commands
end tell

Note: I made a minor edit to line 2 of the above, as I didn’t test prior “ mynote is a single list item.