I’m looking for a way to programmatically position the cursor at the end of a Script Debugger document so that I can append text to it without altering the visual appearance of the code above it. If I update the entire source text it will all appear formatted as new, uncompiled code. I would prefer for only the newly added text at the bottom of the script to appear formatted this way.
I find the editable ‘selection ASObjC range’ value to manipulate the cursor, but no method to determine the limits of the ‘selection ASObjC range’ values. I can do a select all and then get the range of the ‘selection ASObjC range’ to find the end of the document’s text range then set the origin of ‘selection ASObjC range’ to that value and it’s range to 0. It seems to be more reliable to select all and press the right arrow to jump to end-of-selection.
Does anyone here know a better or more reliable method for doing this? This one can only operate on the frontmost document due to the select all keystroke command.
on ScriptDebugger_Move_Cursor_To_End()
tell application "Script Debugger"
tell document 1
tell application "System Events"
keystroke "a" using command down
end tell
set endOfSelectionRange to item 2 of (get selection ASObjC range)
set selection ASObjC range to {endOfSelectionRange, 0}
end tell
end tell
end ScriptDebugger_Move_Cursor_To_End
Paul. I’ve included my suggestion below. In limited testing, it does not change the visual appearance of the code above it, and you can set the document number to something other than 1.
use framework "Foundation"
use scripting additions
tell application "Script Debugger" to tell document 1
set allCode to source text
set codeLength to (length of allCode)
set selection ASObjC range to {codeLength, 0}
end tell
Much better. I would swear I tested source text vs selection ASObjC range while all text was selected and found they didn’t agree. I’ll test this after lunch. Thanks!
Paul. My script does seem to break if the script contains 32-bit characters. I’m not sure of the reason for that, as the codeLength variable appears to return the correct value. The following seems to fix this issue, but it seems backwards. I think you need someone with more knowledge on this topic than me.
tell application "Script Debugger" to tell document 1
set allCode to source text
set codeLength to (length of allCode) + 1
set character range of selection to {codeLength, 0}
end tell
@peavine Your updated script pointed me towards the solution! I also could swear I previously experimented with excessively large values here without success. This method works consistently.
on ScriptDebugger_Move_Cursor_To_End()
tell application "Script Debugger"
tell document 1
set character range of selection to {99999, 0}
end tell
end tell
end ScriptDebugger_Move_Cursor_To_End
The ASObjC range property returns the length of an ASObjC string, which is effectively the number of 16-bit codes, whereas AppleScript’s length property returns the number of characters, or in Unicode terms, composed character sequences.
You could also use:
use framework "Foundation"
use scripting additions
tell application "Script Debugger" to tell document 1
set allCode to source text
set codeLength to (current application's NSString's stringWithString:allCode)'s |length|()
set selection ASObjC range to {codeLength, 0}
end tell
although that’s fairly pointless. Where there is a point to ASObjC range is where you already have an NSString, such as when you are searching the contents using ASObjC methods.
BTW, I did get an error when testing your script suggestion and the easy fix was to use application id. If anyone wonders the reason for this, Shane explained this to me before in: