|
(* |
|
|
|
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 where name of it contains "Scratchpad" |
|
|
|
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 |
I appreciate this!