Do stuff when drive is unmounted (disconnected)

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi,

Would anyone happen to know of a way to do stuff when a specific drive is unmounted (disconnected)?
I'm able to react to it being mounted (connected), by monitoring the `/Volumes/` folder with rules `Name is [Drive Name]` and `Date Added is after Date Last Matched`, but I want to execute some actions when that same drive is mounted again as well.

Any ideas?

Thanks!
—Douwe
DouweM
 
Posts: 12
Joined: Tue Oct 25, 2011 9:42 am

There's no way to deal with files that have disappeared at the moment. The best you can do is some script which would match some volume that is always present (like your boot drive) and then scan the folder itself and deal with any missing volumes, or something like that.

If you provide more details on what it is you are trying to do, there may be an alternate way of doing it that I can suggest.
Mr_Noodle
Site Admin
 
Posts: 11278
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

What I want to do is the following:
  • Intelligently switch between `~/Downloads` and `/Volumes/External HD/Downloads` as Safari's download folder
  • Intelligently switch between `~/Downloads` and `/Volumes/External HD/Downloads` as the source of the Downloads stack in my Dock.
I know how to realize both using shell scripts, but I don't know how to detect the unmount "event".

I take my MBP to school and work a lot, so currently this is something I need to do manually. I would love for it to be done automatically.
DouweM
 
Posts: 12
Joined: Tue Oct 25, 2011 9:42 am

Any suggestions?
DouweM
 
Posts: 12
Joined: Tue Oct 25, 2011 9:42 am

Sorry, can't think of a good way to do this at the moment. Maybe look into something like Sidekick (formerly NetworkLocation) if you need to reconfigure stuff depending on your location.
Mr_Noodle
Site Admin
 
Posts: 11278
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

With Hazel 3.0b you can do this with a passed shell script in the rule menu: ls /Volumes/ | grep "External HD"; This will match when the drive is ejected so long as the '/Volumes/' directory is monitored.

With Hazel 2.xx I'd do something like this, although I'm not sure if Hazel triggers rules on file deletion (directory/volume ejection): Rule monitoring /Volumes/ then run a script that checks the status of your downloads directory first. If the directory matches what it should be for the volumes mounted, then do nothing. Else, make the appropriate change.

If you're on SL, you can use an app I wrote called AirPort Location. Its free - http://airportlocation.wordpress.com.

Otherwise, if it were me, I'd write an Alfred script that I'd trigger when I got home.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

a_freyer wrote:With Hazel 3.0b you can do this with a passed shell script in the rule menu: ls /Volumes/ | grep "External HD"; This will match when the drive is ejected so long as the '/Volumes/' directory is monitored.

Another way to check for the directory's existence is like this:
Code: Select all
test -d "/Volumes/name"

In Bourne-like shells, test is a built-in command so no external commands are required and this also works:
Code: Select all
[[ -d "/Volumes/name" ]]
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene


Return to Support