Run a script when drive unmounts?

Im getting frustrated with osx leaving its trash folder (and other misc "hidden" files) on my usb drive when i eject, so im wondering if its possible to run an applescript or something when i eject the drive so i can make the applescript erase all the "hidden" files so they dont clog up my stereo seeing as my stereo sees the trash folder as justa normal folder and it thinks "._songname.mp3" is an actual mp3.

There is a way of doing what you describe, but you can't hook into it with a script (not at this point anyways). The only way I can think of doing this is to register to accept/deny unmount events through the DiskArbitration framework. This is a C API, and does involve callbacks so it is not the easiest to program with (although by far not the hardest). What you could do is to register for DARegisterDiskUnmountApprovalCallback, and then in your callback do the work you want to do, then allow it. You are going to have to do this very quickly (no nested searching), as otherwise you will beachball the user.

I should have mentioned that i am not programmer, so al of what you just typed was gibberish, lol.

I did work out a simple Service in Automator today from little snippets of shell script i picked up around the internet.

Pics work best:

The only problem i have with it is that it also tries to work on folders, which it cant because hdiutil trows out an error. I tried to make an applescript that determined the type of the item selected (volume or folder) but i could never figure out how to get applescripts to work inside automator.

The dialog window also looks like crap, but i will clean the text up on that later.

edit:
For some reason the service works when running it in Automator, but when i run it from the contextual menu it gives me an error (without telling me what the error is. yay!).
Any ideas?

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.