MacOS – How to prevent Script Editor from scrolling (and losing cursor location) on compile

applescriptmacosperformancetext-editor

I have some lengthy .scpt files. By "lengthy," I'm talking character counts (excluding spaces) of 200,000.

I use Script Editor.app to create and edit AppleScripts (simply because Script Editor was already installed).

If I save a long file in Script Editor (e.g., by pressing ⌘ command + S) the file will almost always scroll much further down in the file. The result is that I am far removed from the location in the script where I was just editing, and my text cursor is lost. If I type some text immediately after saving, the text is placed on the very last line of the document.

So, I have to spend a decent bit of time trying to find the line or area of code that I was just editing. I save often, so this time significantly adds up.

To make matters worse, after saving, the scrollbar in Script Editor is buggy and erratic for around 30 seconds. This makes scrolling manually largely counterproductive. Sometimes this erratic period is up to one minute.

Combined, these two errors result in a frustrating and time-consuming saving experience.

The only scenario where I have seen these two issues completely disappear is when the script is short. As in, short enough to not possess a scroll bar whatsoever.

I generally don't have many programs running on my computer when I am using Script Editor and they are never applications that are demanding. I usually have a couple Chrome windows, a couple TextEdit windows, one Script Editor window, and that's it. My machine is an Early 2013 Retina MacBook Pro with 16 GB of RAM.

Is this Script Editor behavior normal? Is there anything that I can do to mitigate it?

Best Answer

I just tried what you explained with script editor in Sierra and did not experience the same problem you are experiencing. This may not be the best solution to your questions but it will definitely save you some time. Why not just throw in a little comment at the place in the script where you want your cursor located after a file save. Just adding something like --cursorand copy that commented --cursor to your clipboard then after you do ⌘ command + S , then go ahead and click ⌘ command + F (which will open the find field right there in your script editor window) then you could click ⌘ command + V (which would automatically paste that short commented line into the find field) this of course will highlight that text in your script and bring you right to its location.


UPDATE - NEW DIFFERENT APPROACH

Just add this handler to the bottom of any one of your script files.(I like to put my handlers at the bottom of the script to keep them from cluttering up the rest of the body of the script)

Calling the handler at any point in your script will ultimately get the coordinates of your scrollbar, then it will automatically save the current script with the keycode command , then will return your scrollbar back to its original coordinates. Just comment or un comment the calling of the handler depending on whether you want to save or not save your document. Leaving the calling of the handler as un commented As I have it set now, Will automatically save your script every time you click the “RUN” button within script editor.

on setScrollPosition()
    tell application (path to frontmost application as text) to (path to me as text)
    set theFile to result
    set theFile2 to alias theFile
    tell application "Finder"
        set theName to name of theFile2
    end tell
    delay 1
    tell application "System Events"
        get value of value indicator 1 of scroll bar 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theName of application process "Script Editor"
        set thePosition to the result
        key code 1 using command down
        delay 10
        set value of value indicator 1 of scroll bar 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theName of application process "Script Editor" to thePosition
    end tell
end setScrollPosition

setScrollPosition()

SIDE NOTE: I have the delay set 5 which gives me time to play around with the scroll position after clicking “Run” to see if it actually works or not (which it does in the latest version of Sierra) you can adjust the delay value or comment it out if you don't need it.