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.
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.
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.
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
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
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!
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
------------------------------------------------------------------------------