Run a script when drive unmounts?

Discussion in 'macOS' started by NoSmokingBandit, Oct 16, 2009.

  1. NoSmokingBandit macrumors 68000


    Apr 13, 2008
    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.

    So essentially i want to:
    start unmount process
    run applescript to erase every ".*" file
    finish unmount process
  2. larkost macrumors 6502a

    Oct 13, 2007
    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.
  3. NoSmokingBandit thread starter macrumors 68000


    Apr 13, 2008
    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.

    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?

Share This Page