Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benspaulding/1287814 to your computer and use it in GitHub Desktop.
Save benspaulding/1287814 to your computer and use it in GitHub Desktop.
AppleScript that will save all documents with unsaved changes when BBEdit loses focus.

BBEdit ApplicationWillSwitchOut Attachment Script

The script itself has most of the documentation. Just know that there are two scripts here:

  1. A plain-text version (.applescript)
  2. A compiled version (.scpt)

The plain text is here so you can actually see the script before downloading. However, BBEdit will need a compiled version. So either download the .scpt file and save it to the proper location, or copy the contents of the .applescript file, open AppleScript Editor, paste it in a new document, and save it as a .scpt in the proper location.

(*
File:
Application.applicationWillSwitchOut.scpt
Abstract:
This script will automatically save all on-disk text documents with unsaved
changes when BBEdit loses focus.
Version:
0.9.2
Author:
Ben Spaulding <http://benspaulding.us/>
Details:
This is an application attachment point script which, when saved to the
proper location, will automatically save all text documents with unsaved
changes when the application is put in the background.
This script must be saved to the `Attachment Scripts` directory within
BBEdit's `Application Support` folder.
For more information, see:
- http://groups.google.com/group/bbedit/msg/a817516ca10a217b
- BBEdit User Manual
*)
on applicationWillSwitchOut(theApp)
-- Some BBEdit-specific properties, such as the document class's `on disk`
-- property, require that they be contained in a tell block. To prevent
-- frustration, simply wrap the whole thing in the tell block.
tell application "BBEdit"
-- Save all on-disk text documents with un-saved changes.
-- -----------------------------------------------------
set docList to every text document in theApp
repeat with doc in docList
(* Note that we verify that the doc is on disk. This is because
backgrounding BBEdit yeilds a bonk and dialog to save files with
changes that are not on disk. That is less than effective as the
purpose of saving when backgrounding is to save keystrokes when
testing/previewing code. Off-disk docs will rarely be a part of
that workflow. What's more, they are not at risk of being lost
because of BBEdit's state and un-saved change handling when the
app is closed. *)
if doc is modified then
if doc is on disk then
-- This is a normal, modified, on-disk file, so save it.
save doc
else
-- When the command line tool ``bbedit`` is given a path
-- to a non-existent file, it opens a window that, when
-- saved, will save to the given path. Unfortunately the
-- file is marked as not on disk, and there is no file
-- property either. However, files opened this way do have
-- their name set to read-only, like on-disk files do. So
-- trying to set it will throw an error, which lets us know
-- that this is one of those files.
try
set name of doc to (name of doc) as string
on error
save doc
end try
end if -- doc is on disk
end if -- doc is modified
end repeat
end tell
end applicationWillSwitchOut
@devilgate
Copy link

It doesn't seem to work now on Mojave 10.14.4, and BBEdit 12.6.3, unfortunately.

@joncfoo
Copy link

joncfoo commented May 12, 2023

The following script works:

const app = Application.currentApplication()
app.documents()
	.filter(doc => doc.modified() && doc.file())
	.forEach(doc => app.save(doc))

With Script Editor save it as a JavaScript script as ~/Library/Application Support/BBEdit/Attachment Scripts/Application.applicationWillSwitchOut.scpt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment