Okay, I'm back.
I'm writing a handy little application for you. I'll announce it in this thread when it's finished. (If I didn't have to eat and sleep, it might already be finished.) It watches for things which happen, and loads and executes scripts to match.
The events (with random capitalization):
Disk Mounted*
Disk Unmounted*, **
About To Sleep
Woke Up
Logout Requested
*
Switched Back To This Account Via Fast User Switching
Switched Away From This Account Via Fast User Switching
Application About To Launch
Application Has Launched
Application Has Quit
Frontmost Application Has Changed
*I'm thinking of creating two events for these scripts. One if the disk path -- which is what the event from the OS tells my program -- is in "/Volumes/", which will give the name of the disk as well as the path, and one otherwise which will just give the path.
**At the present time, I am not trying to do the "disk is going to unmount soon" event. It's already risky enough handling "machine is about to sleep" since that has a cutoff of 30 seconds...
*This one is kind of a duplicate of existing stuff. You could just create a logout item for the script. But it was in the list of events that NSWorkspace generates, so I thought I'd toss it in.
It has custom handlers you write in your script, like you do for Mail filter scripts, and loads the scripts from a folder in the Application Support folder in your user's Library folder. It will look for specific scripts before general ones. For example, when the frontmost application has changed, it will first look for a script whose name (ignoring extensions) is the same as the application in a subfolder of its scripts folder. It will then look for a script named for the event. (I haven't decided on the final names for the events yet. I've been working on getting the guts of AppleScript to work from inside Cocoa instead. I have verified that I know how to do this properly, so it's just a matter of building the pretty user-friendly parts to support it.)
Oh, and you can tell it which events to watch for; these settings are persistent between launches, so you can set it as a startup item.