Moving the Cursor Position in Script Debugger

(Christopher Stone) #1

Hey Folks,

Having grown up with programming editors like Z, vi, and vim I think it’s crazy when an editor cannot rapidly move the editing location (cursor) around to various places – at least to the top, middle and bottom of the visible editing area. (And It’s a bonus when the current line can be moved to those areas as well.)

I was doing something unrelated with UI Browser, when it occurred to me that I might be able to get the visible text area of Script Debugger editor panel.

Sure enough.

My proof-of-concept script below moves the cursor to the top of the editing panel.

The script MUST be SAVED to function properly due to the mechanism it uses to prevent it from operating on itself. (If it is frontmost, it will operate on window 2.)

It’s set to work with SD6, but the SD5 id-string is in there for anyone who needs it.

I’ve tested the script with both version of SD.



# Auth: Christopher Stone
# dCre: 2017/02/18 21:30
# dMod: 2017/02/18 21:47 
# Appl: System Events, Script Debugger 6
# Task: Move cursor to top of visible text editing area.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Script_Debugger, @Move, @Cursor, @Top, @Visible, @Text, @Editing, @Area

set myPath to (path to me as text)
if myPath ends with "Script" then error "Script is not saved!"

# "com.latenightsw.ScriptDebugger5" -- Script Debugger 5

tell application id "com.latenightsw.ScriptDebugger6" -- Script Debugger 6
   if myPath = (get file spec of front document as text) then
      set scriptDoc to a reference to document 2
      set windowNum to 2
      set scriptDoc to a reference to document 1
      set windowNum to 1
   end if
   if scriptDoc exists then
      tell scriptDoc
         set startChar to item 1 of getSdVisibleCharacterRange(windowNum) of me
         if startChar > 1 then
            set sourceText to text startChar thru -1 of (get source text)
            set lineLength to length of (get paragraph 1 of sourceText)
            set startChar to startChar + lineLength + 1
         end if
         set selection to {startChar, 0}
      end tell
   end if
end tell

on getSdVisibleCharacterRange(windowNum)
   tell application "System Events"
      tell (first application process whose bundle identifier is "com.latenightsw.ScriptDebugger6")
         tell (window windowNum whose subrole is "AXStandardWindow")
            tell text area 1 of scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of group 2
               set charRange to value of attribute "AXVisibleCharacterRange"
               return charRange
            end tell
         end tell
      end tell
   end tell
end getSdVisibleCharacterRange

(Boyd Crow) #2

It is not clear how one is supposed to use this script. Could you describe, please?

(Christopher Stone) #3

Hey Boyd,

Save the script to Script Debugger script menu folder.

Give it a keyboard shortcut.

Run it.

I have three of these.

Move cursor to top.

Move cursor to middle.

Move cursor to bottom.

If you scroll your document such that the cursor is past the top or past the bottom of the visible editing area you have to resort to the mouse to bring the cursor into the current view for editing.

In old carbon text editing apps you could use Opt-UpArrow or Opt-DnArrow to reposition the cursor to the top or bottom of the current viewable edit space.

This doesn’t work in Cocoa apps.

I was able to solve the problem with System Events in Script Debugger, and I use my scripts all the time.


(Phil Stokes) #4

That still works in BBEDit.
fn-Opt-Up/Dn arrow still does that here in SD and most other text editors I’ve got (Xcode, Coda, Script Editor)

(Christopher Stone) #5

Hey Phil,

BBEdit isn’t strictly a Cocoa app.

While those key-combos work they do NOT do the same thing Carbon apps did — which was to reposition the cursor at the top or bottom of the editing area.

Now they page up or down half a page and reposition the cursor in the middle of the editing area.

And — fnOpt and fnOpt are really gnarly to reach on my Apple Extended Keyboard.  :)


(Nigel Garvey) #6

Hi Chris.

I think the second line needs updating. I’m getting a path ending with “Unsaved Script Debugger Document.scpt” when the script hasn’t been saved.

(Christopher Stone) #7

Hey Nigel,

I guess I need to change the error string…

The Move cursor to top of visible text editing area script must be saved in order to work.

I use this mechanism to prevent a script from operating on itself — it’s designed to work on window 2 if it is window 1.

I use it in all scripts that make alterations to the frontmost script, for both safety and more convenient debugging (i.e. I can step through window 1 and watch the results in window 2).