0

I am trying to translate a few Applescript (AS) into Javascript (JXA) for BBedit.

It's been a fun little thing to knock some rust off, but I'm stumped. With AS I can set an insertion point into a BBEdit document thusly;

tell application "BBEdit"
    activate
    tell text 1 of window 1
        select insertion point before line 40
    end tell
end tell

I'm totally stumped when it comes to JXA. I've been poking around in the line objects but I can't find a insertsionPoint property.

You can access selection properties like so;

currentLine = bbedit.selection().startline().

But it is read only. So I think you need access to that select method if you want to set a selection or insertion point. I have no clue how, or if you can with JXA.

Anyone know how to set a BBEdit insertion point and/or selection with JXA? Thanks.

2 Answers2

1

JXA doesn't implement insertion reference forms (before/after/beginning/end). Relative (previous…/next…) and range (from…to…) specifiers are also borked, and filter (whose…) clauses are horrible too. Lots of non-trivial stuff that works fine in AS breaks in JXA: like Apple's earlier Scripting Bridge API, JXA was half-baked when shipped and immediately abandoned to rot.

Crap like this is why I recommend sticking to AppleScript. The language may stink, but it's the only [marginally] supported option that actually implements Apple events right. Normally I'd recommend calling into AppleScript from other languages via the AppleScript-ObjC bridge as the least sucky solution, but Apple have managed to break that too in 10.13.

(If you enjoy living dangerously, NodeAutomation provides non-broken Apple event support for Node.js, but with Apple abandoning AppleScript automation I don't want to waste anyone's time promoting or supporting it, so caveat emptor.)

has
  • 92
  • 1
0

Below is an example of using Javascript (JXA) for BBedit insertion point object and select method. BBEdit does seem to work with JXA but there is absolutely no documentation for JXA from BBEdit and very little information on the web. Below is some code that I came up with after spending hours of trial and error. So I hope it helps.

(() => {
    /* Example to show using insertionPoints with BBEdit
        the script adds new text to the last line of a BBEdit text document
    */
    const strPath = '/Users/bartsimpson/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/insertionPointsExample.txt';
    
    const BBEdit = Application('BBEdit');
    const docs = BBEdit.textDocuments;
    //use select in case there are multiple BBEdit documents open
    BBEdit.open(Path(strPath)).select();
    let doc = docs[0];
    let insertionPoints = doc.characters.insertionPoints;
    let lines = doc.characters.lines;
    let indexLastChar = doc.characters.length;
    let indexLastLine = lines.length-1;
    //last line in doc is blank
    if (lines[indexLastLine].length() == 0){ 
        insertionPoints[indexLastChar].contents = 'some new text5';
        indexLastChar = doc.characters.length; //update after adding text
        insertionPoints[indexLastChar].select(); //puts cursor end of last line
    }
    //last line in doc has text so add a new line first
    else {
        insertionPoints[indexLastChar].contents = '\n';
        insertionPoints[indexLastChar+1].contents = 'some new text6';
        indexLastChar = doc.characters.length; //update after adding text
        insertionPoints[indexLastChar].select(); //puts cursor end of last line
    }
})() 
Rmattson
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 04 '23 at 11:54