Overview:
Create a launchd agent (a bash script in this case) to mount us whatever shares we like every couple of minutes. It is not perfect but it works. This is for Samba shares, but you can modify it to do other types.
Made with help from Dave Nicoll's about sharing windows/x iTunes libraries.
I'm using my laptop's wireless card to determine if I am connected to my home SSID. You can of course use anything you'd like for conditional execution.
If you're going to use SSID as a condition for mounting as I have you may find it benificial to alias the Airport utility that ships with OS X to your /usr/bin/. If not skip ahead to the next section.
Open Terminal and run:
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport
Remember the output of this:
airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'
The Script:
My conditionals check for
- The share is not already mounted (line 1-4)
- The SSID of our Airport card network is not null (line 5-8)
- That our SSID must be equal to a pre-defined SSID, in my case "2600leagues" (line 9)
Open your favorite text editor and start a new file, I called mine MountShares.sh
if [ -d '/Users/kyle/Music/iTunes/Podcasts/' ]; then
#echo Nothing to do, share is mounted
exit
else
if [ -z `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` ]; then
#echo SSID is Null, we're not connected with the Airport to any Network.
exit
else
if [ `airport -I | grep '.* SSID:' | sed 's/^.* SSID: //'` == "2600leagues" ]; then
#echo SSID is 2600leagues!
/sbin/mount -o nodev,nosuid -t smbfs '//Kyle:NotmyPassword@192.168.1.5/media/Music' '/Users/kyle/Music/'
fi
fi
fi
Next we need to edit line 11 to represent your specific needs. You only need to edit the bold parts:
/sbin/mount -o nodev,nosuid -t smbfs
'//User:Password@ServerIP_or_Name/Share'
'/Where/You/Want/it_to_mount/'
Example:
/sbin/mount -o nodev,nosuid -t smbfs '//Kyle:NotmyPassword@192.168.1.5/media/Music' '/Users/kyle/Music/'
Now that we have our edited mount command, try running it in Terminal. If it is successful, your share should be available at the mount location. (in my example /Users/kyle/Music/)
Edit the line 1 of the bash script to reflect a file or directory inside the mounted share. If you are going to use the SSID as a condition as I have, you'll want to change line 5 by replacing 2600leagues with your SSID.
Save the bash script in a convenient location. (I use ~/Library/Scripts/NetworkMounts/MountShares.sh)
You now need to make the bash script executable, we use chmod to do this.
In Terminal:
chmod 777 ~/Library/Scripts/NetworkMounts/MountShares.sh
If the network share is still mounted, make sure you unmount/eject it now. (Finder works)
Try executing the script via Terminal:
~/Library/Scripts/NetworkMounts/MountShares.sh
If all goes well your share should be mounted. Now all that is left to do is make it so the script runs every so often. Normally you would use crontab for this sort of thing, but 10.6 has deprecated it. Apple would rather you use their launchd service.
Making the launchd Agent
To help you make a launchd agent get Lingon from sourceforge.
- Open Lingon
- Create a new User Agent (My Agent)
- Give it a name such as
com.kyle.MountShares
- Choose or type the path to the bash
script
~/Library/Scripts/NetworkMounts/MountShares.sh
- Specify when you would like it to
run. (I have Run when it is loaded
by the system and *Run it every
10 minutes)
- Save it
- Exit Lingon
That's it
Let me know if this helps; I typed all this out as fast as possible.