New questions about AppleScript Application attachment points

54 views
Skip to first unread message

Peter Steiner

unread,
Aug 13, 2025, 12:22:10 PMAug 13
to BBEdit Talk
Hello, I'm a seasoned programmer but pretty new to Applescript and BBEdit, so please bear with me if I'm asking obvious stuff. 

I asked support about auto-save and got a quick answer to look into the 'applicationWillSwitchOut' attachment point.

I then found these old threads mentioning applicationWillSwitchOut:

but without examples ("This is largely an exercise for the reader :-) [...], and the group can probably help you with the scripting itself.")

To start dabbling with AppleScript I put this compiled script into the mentioned folder (~/Library/Application Support/BBEdit/Attachment Scripts/Application.scpt):

on applicationWillSwitchOut:(theApp)
tell application "TextEdit"
activate
make new document
set text of front document to "hello applicationWillSwitchOut"
end tell
end applicationWillSwitchOut:

(I made similar subroutines for the other 4 application attachment points)

I then started BBEdit, switched focus, then back to BBEdit and then quit. Nothing happened with TextEdit, so I'm not sure if the scripts were even run.

Am I doing this wrong? How can I debug attachment scripts? I can't run them directly in the Script Editor, can I? The manual is silent on this, probably because mac users are supposed to know that?

Or even better, has somebody already a script that saves open files when quitting or switching out?

Thanks in advance!

Peter

jj

unread,
Aug 13, 2025, 1:32:22 PMAug 13
to BBEdit Talk
Hi Peter,

Remove the colon and it should work.

use AppleScript version "2.8"

use scripting additions

--

on applicationWillSwitchOut(theApp)

tell application "TextEdit"

activate

make new document

set text of front document to "hello applicationWillSwitchOut"

end tell

end applicationWillSwitchOut


HTH,

Jean Jourdain

Peter Steiner

unread,
Aug 16, 2025, 10:42:29 AMAug 16
to BBEdit Talk
Hello Jean

Thanks for the hint, it worked! And I managed to create a script that does what I want. If there are better or more elegant ways to achieve saving of all open files I welcome all feedbacks!

on applicationWillSwitchOut(theApp)
tell application "BBEdit"
set allProjects to every project document
repeat with project in allProjects
set currentlyOpenDocuments to every text document of window of project
repeat with doc in currentlyOpenDocuments
save doc
end repeat
end repeat
end tell
end applicationWillSwitchOut

Regards, Peter

jj

unread,
Aug 17, 2025, 4:15:19 AMAug 17
to BBEdit Talk
Hi Peter,

Good to know it worked and you could achieve what you were after.

Here is a version using a whose clause and error handling:

use AppleScript version "2.8"

use scripting additions

--

try

tell application "BBEdit"

set vDocuments to its text documents whose modified is true

repeat with vDocument in vDocuments

save vDocument

end repeat

end tell

on error aMessage number aErrorNumber

if aErrorNumber ≠ -128 then -- Error number -128 (User Cancelled).

display dialog aMessage

end if

return

end try


HTH


Jean

Message has been deleted

Peter Steiner

unread,
Aug 24, 2025, 6:45:40 PMAug 24
to BBEdit Talk
Hello Jean

Thanks for showing me about "whose modified is true" and the error handling. :-)

I'm just not sure if the "display dialog" is the best way to communicate the error, as the BBEdit manual states that

You should also avoid using ‘show dialog’ or similar verbs during applicationWillSwitchOut, because that will leave the resulting item on screen until you switch back to BBEdit 

Is there a way to write the error into some log file? Or what would be a way to avoid interference with the application switching?

Regards, Peter
Reply all
Reply to author
Forward
0 new messages