Applescript - move to end of document

49 views
Skip to first unread message

Jan Erik Moström

unread,
Apr 14, 2021, 12:22:53 PM4/14/21
to BBEdit Talk
Basic AppleScript question: How do I move the insertion point to the end
of the current document?

I tried with something like this

tell application "BBEdit"
activate
open file_path
tell front window
select character after endLine
end tell
end tell

or "insertion point" but I'm doing it wrong. Could someone enlighten me
how to do this?

= jem

Rich Siegel

unread,
Apr 14, 2021, 12:28:28 PM4/14/21
to BBEdit Talk
This should work:

tell app "BBEdit"
activate
open file_path
select insertion point after last character of document 1
end tell

(It's best to always target documents, rather than windows; though
that's not the proximate issue here. "document 1" is always the active
document.)

R.

--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they sedate me.

Jan Erik Moström

unread,
Apr 14, 2021, 2:15:16 PM4/14/21
to BBEdit Talk
On 14 Apr 2021, at 18:28, Rich Siegel wrote:

> This should work:
>
> tell app "BBEdit"
> activate
> open file_path
> select insertion point after last character of document 1
> end tell
>
> (It's best to always target documents, rather than windows; though
> that's not the proximate issue here. "document 1" is always the active
> document.)

Thank you! I'm trying to get back into AppleScript to do some automation
... and I've forgotten most of what I knew about AS.

= jem

Christopher Stone

unread,
Apr 14, 2021, 10:15:06 PM4/14/21
to BBEdit-Talk
On 04/14/2021, at 11:28, Rich Siegel <sie...@barebones.com> wrote:
This should work:

tell app "BBEdit"
activate
open file_path
select insertion point after last character of document 1
end tell

(It's best to always target documents, rather than windows; though that's not the proximate issue here. "document 1" is always the active document.)


Hey Folks,

Here's another method:

tell application "BBEdit"
    tell text of front document
        select insertion point after it
    end tell
end tell

I nearly always prefer to use tell-blocks to reference objects rather than one-liners, because frequently when I forget to do so I end up having to refactor my code when I add or change something.

For this use-case I might do something like this:

tell application "BBEdit"
    tell front document
        tell its text to select insertion point after it
    end tell
end tell

But if I will take time to write the code “correctly”:

tell application "BBEdit"
    tell front document
        tell its text
            select insertion point after it
        end tell
    end tell
end tell

Then I have no problem coming back in later and doing more with either the document, or the text, or both.

Probably 8 times out of 10 I'll regret not using the above construct, because a short time later I'll be modifying it to something on the order of this:

tell application "BBEdit"
    tell front document
        tell its text
            set after it to linefeed & "Some more text..."
            select insertion point after it
        end tell
    end tell
end tell

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages