Set selection point to next line in AppleScript

148 views
Skip to first unread message

Rainer Krug

unread,
Aug 5, 2021, 6:15:09 AM8/5/21
to BBEdit Talk
I have the following AppleScript to execute het selected text in R and return the focus to BBEdit:

tell application "BBEdit"

set the_selection to (selection of front window as string)

if (the_selection) is "" then

try

set the_selection to line (get startLine of selection) of front window as string

on error

display dialog "The active window must be an editor window, with a file opened. You want to run something in R? Well, tell me what!" with icon note buttons {"Got it!"} default button 1

error number -128

end try

end if

end tell


tell application "R"

cmd the_selection

end tell


tell application "System Events"

tell process "BBEdit"

set frontmost to true

end tell

end tell

Now I would like to set the cursor in BBEDit to the next line after the selected text ore line it is in.

an I do this?

Thanks,

Rainer

Christopher Stone

unread,
Aug 5, 2021, 6:52:26 AM8/5/21
to BBEdit-Talk
On Aug 05, 2021, at 05:15, Rainer Krug <Rai...@krugs.de> wrote:

I have the following AppleScript to execute het selected text in R and return the focus to BBEdit:
...
Now I would like to set the cursor in BBEDit to the next line after the selected text ore line it is in.


Hey Rainer,

You can do something like this.

--
Best Regards,
Chris



tell application "BBEdit"

    

    set startLineNum to startLine of selection
    set selectedText to selection's contents

    

    if selectedText is "" then
        try
            set selectedText to contents of line startLineNum of front document
        on error
            display dialog "The active window must be an editor window, with a file opened. You want to run something in R? Well, tell me what!" with icon note buttons {"Got it!"} default button 1
            error number -128
        end try
    end if

    

end tell

tell application "R"
    cmd the_selection
end tell

tell application "BBEdit"
    activate
    tell front document
        try
            select insertion point before line (startLineNum + 1)
        on error
            select insertion point after line startLineNum
        end try
    end tell
end tell



Rainer Krug

unread,
Aug 5, 2021, 7:27:40 AM8/5/21
to BBEdit Talk
Perfect. Thanks.

Rainer Krug

unread,
Aug 5, 2021, 7:33:11 AM8/5/21
to BBEdit Talk
Replied to fast. 

I changed `startLine of selection` to `endLine of selection` and now it moves to the next line after the selection.

Thanks,

Rainer
Reply all
Reply to author
Forward
0 new messages