One of my minor annoyances is when I log onto my Hackintosh and try to copy something to one of the network shares on my FreeBSD machine, only to discover that the share isn’t mounted. I know, it’s not a big deal, I can just select Finder, type ⌘K, pick the share from my favorites and it’s mounted. But I’d rather not have to do that.
“AppleScript,” I thought. “I can create an AppleScript app that runs on login and mounts the shares I want.” In fact, I’d written such a script a couple years ago:
try
set myIPAddress to do shell script "ifconfig -a inet 2>/dev/null| perl -ne 'if (/inet (\\d+.\\d+.\\d+).\\d+/) {print qq{$1
} if ($1 ne q{127.0.0});}'"
end try
if myIPAddress is "192.168.8" then
try
mount volume "smb://daffy.local/packy"
mount volume "smb://daffy.local/mp3"
end try
end if
I’d written it to do pretty much the same thing on my laptop. First, it queried the IP address of my laptop to see if I was on my home network (not many people set their internal class C network to 192.168.8.0/16, most use 192.168.0.0/16 or 192.168.1.0/16), and, if so, it mounted both my home directory and the MP3 share from my home server.
Well, I could ditch the first part, since this was for my desktop and I knew it was always going to be on my home network. And I also didn’t want to have to fire up the AppleScript editor every time I wanted to change which network shares were mounted. So I fired up the AppleScript help utility, did some reading on how to read files in AppleScript (since I’m not a frequent AppleScript coder, I frequently have to refer to the docs) and reworked the script into this:
set myHome to (path to home folder) as string
set rcFile to myHome & ".NetworkShares"
set Mounts to read file rcFile as text using delimiter ASCII character 10
repeat with share in Mounts
try
mount volume share
end try
end repeat
This script reads a .NetworkShares file in my home directory, which contains one remote volume URL per line:
nfs://gogo/usr/media smb://newlennon/Music
Now, when I want to change which shares are mounted, all I have to do is fire up Emacs and edit .NetworkShares.