Set BBEdit's active/front document?

This shouldn’t be so hard, but AppleScript has never been intuitive to me.

How do I set BBEdit’s active/front document (the one where a “paste” will go to) to a specific document I already know exists. For example (error handling has been removed for clarity):

tell application "BBEdit"
    set myDoc to first document whose name is "dan.txt"

    ????

    get front document -- will return the equivalent of "myDoc"
end tell

Dan, this is a great question, and I believe a very hard question (until you know the answer).

I have been testing using Script Debugger for about 30 minutes, and have done considerable Google searching, and I cannot find the command to make a specified document the "active document".

The "active document" is defined in the scripting dictionary, but it is defined as "get" (no "set"):

I have not been able to answer this by exploring the dictionary and testing in SD.

So, I think we will have to wait for the AppleScript master @ccstone (Chris Stone) to come to our rescue. :smile:

Thanks for trying. You probably tried everything I did, perhaps even other things.

Since not only can there be multiple documents in a window, there can be multiple windows, it might not be as easy as it sounds.

I agree - let’s wait for @ccstone’s expertise. :slight_smile:

Thanks again.

But there can be only ONE . . .

Immortal

err, ah, "Active Document" :wink:

1 Like

Is there some reason you cannot simply use:

tell application "BBEdit"
	activate
	open file "Zany:Users:peter:dan.txt"
end tell

But in any event, I believe the answer is:

tell application "BBEdit"
	set index of window "dan.txt" to 1
end tell

Good guess, Peter. That would be the standard command for most apps where each document is in a separate window.

But with BBEdit (and TextWrangler), that does not work.

Each window can have multiple text documents, and the window name would be the name of the current Active Document, not the document you want to make active.

It is very similar to a window having tabs, but BBEdit does not have an entity it calls "Tab". It is just Window > Document(s).
And I have not been able to find an "index" property for documents.

1 Like

Ahh. Right. I don’t use multiple documents in one window in BBedit, I’m too old school for that.

However since my guessing is good, I guessed another time:

tell application "BBEdit"
	set d to document "dan.txt"
	set w to window of d
	set index of w to 1
	select d
end tell
1 Like

Because the file doesn't actually exist. I'm using AS to get BBEdit to create two documents, "km.plist" and "km.json". The extension gets BBEdit to use the right syntax highlighting, but I don't save the files. They're just a way of editing KM actions/macros as text, in either plist or JSON format, so I can paste them right back into KM when I'm done editing.

I don't use multiple documents in one window in BBedit, I'm too old school for that.

Hey! I resent the implication that I'm not old-school. I've been programming since around 1972 or thereabouts. If that doesn't make me old-school, nothing does! I prefer to just say that old dogs can, in fact, learn new tricks. :stuck_out_tongue: :wink:


tell application "BBEdit"
	set d to document "dan.txt"
	set w to window of d
	set index of w to 1
	select d
end tell

Ding ding ding! Perfect. Works great! Thanks!!

@JMichaelTX - Thanks for jumping in and helping!

This seems to work too:

tell application "BBEdit" to select document "test.txt"

(Even if “test.txt” is not saved to disk.)

2 Likes

So here’s my final code. I pass in either “json” or “plist”, and either “BBEdit” or TextWrangler".

tell application "Keyboard Maestro Engine"
    set _documentName to "km." & (getvariable "kmon_esoObjectType")
    set _editorAppName to getvariable "kmon_esoEditorAppName"
end tell

tell application _editorAppName
    activate
    try
        select document _documentName
    on error errMsg number errNum
        make new document with properties {name:_documentName}
    end try
end tell

Thanks to everyone for their help!

Thanks for sharing, Tom. :thumbsup:

I knew it would be something simple, I just didn't know what.
I wasn't familiar with using the "select" command in that context.
It has now been Evernoted!

Tom get’s the brass ring on this one.  :smile:

In a similar vein:

Normally when I script BBEdit I work with the front document, but there are times when that is not convenient.

Sometimes you have to create a more robust reference to it, so it doesn’t get lost during processing.

------------------------------------------------------------------------------

tell application "BBEdit"
   set newDoc to make new document
   --> text document 1 of application "BBEdit"
   -->   Short-lived – Not unique once the window index changes.
   
   # IF the document's window index might change during processing it's desirable
   # to make the reference more robust.
   
   set newDocID to newDoc's ID
   set myDocRef to a reference to document id newDocID
   
   tell myDocRef
      tell its text
         set its contents to "Some Text..."
      end tell
   end tell
   
end tell

------------------------------------------------------------------------------

-Chris

1 Like