Mounting a volume

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

Moderator: Mr_Noodle

Mounting a volume Thu Apr 25, 2013 3:40 am • by Freerider
I have a rule watching the /Volumes folder to check to see if my TV folder on MyBook Live NAS drive has been mounted.
If it is, I don't need to do anything.
If it isn't I want it to be mounted.

The rule is "if Kind is Volume" & "Name is not TV"
I am running an Applescript:
Code: Select all
tell application "Finder"
   try
      mount volume "afp://user:password@MyBook.local/TV"
   end try
end tell

The problem is, the script is being run regardless of whether TV has already been mounted or not.
This causes it to be un-mounted then re-mounted - at least I think it is - the icon on the Desktop disappears briefly then re-appears.

The log for this operation is:
2013-04-25 08:36:06.183 hazelworker[10315] Processing folder Volumes (forced)
2013-04-25 08:36:08.217 hazelworker[10315] Backup: Rule CHECK TV WAS MOUNTED matched.
2013-04-25 08:36:09.388 hazelworker[10315] File TV is busy. Skipping for now.
2013-04-25 08:36:11.395 hazelworker[10315] File TV is busy. Skipping for now.
2013-04-25 08:36:13.401 hazelworker[10315] File TV is busy. Skipping for now.
2013-04-25 08:36:17.653 hazelworker[10315] File TV is busy. Skipping for now.
2013-04-25 08:36:21.664 hazelworker[10315] Done processing folder Volumes

Is there a better way for Hazel to check this mount?
Freerider
 
Posts: 4
Joined: Thu Apr 18, 2013 1:42 pm

Re: Mounting a volume Thu Apr 25, 2013 9:13 am • by a_freyer
There are a dozen ways to do this, but I would add a passes shell script line in your rule conditions that checks to see if the volume exists before you run the script. I do it this way because a shell script will be substantially faster & will require substantially fewer process cycles to load than AppleScript.

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     passes shell script

Do the following to the matched file or folder:
     run apple script


Where then passes shell script is

Code: Select all
if [ -d "/Volumes/TV" ]; then exit 1; else exit 0; fi


This will exit 1 if the directory exists (telling Hazel to fail), but if the directory does not exist, it will exit 0 (telling Hazel to pass)
Last edited by a_freyer on Thu Apr 25, 2013 3:02 pm, edited 1 time in total.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Mounting a volume Thu Apr 25, 2013 10:38 am • by Freerider
Excellent!
This works perfectly - Thanks!

Now, if TV gets un-mounted for whatever reason it just gets mounted again. :D
Sickbeard will be happy.
Freerider
 
Posts: 4
Joined: Thu Apr 18, 2013 1:42 pm

Re: Mounting a volume Thu Apr 25, 2013 11:46 am • by a_freyer
Excellent! Glad to hear.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Mounting a volume Thu Apr 25, 2013 2:39 pm • by Freerider
Just a note for anyone else using this, I think the correct folder path should be:
Code: Select all
"/Volumes/Foldername"
Freerider
 
Posts: 4
Joined: Thu Apr 18, 2013 1:42 pm

Re: Mounting a volume Thu Apr 25, 2013 3:02 pm • by a_freyer
Edited to reflect.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Mounting a volume Sat May 11, 2013 11:17 am • by Woohoo
This works great Freerider, thank you so much
But how can I add a shell script or apple script to tell Hazel that mounting of volumes should only be performed when online.

Why would I need this?
Well, I use my MacBook also on the road and then I don't need mounting of volumes when I am not online or connected to my network.
That's why.

I'm really a newby with Hazel and scripting in general, so please help me out on this one.
Woohoo
 
Posts: 10
Joined: Sat May 11, 2013 11:13 am

Re: Mounting a volume Sat May 11, 2013 4:21 pm • by a_freyer
You can add this script as a condition:

Code: Select all
ping -o -c 1 -W 0.25 -n 8.8.4.4 2>&1 1>/dev/null


This will have an exit status of 0 when the ping works (i.e. you're online), but an exit status of 2 when you're offline.

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     passes shell script
     passes shell script

Do the following to the matched file or folder:
     run applescript
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Support