{ Hi! I'm Mike }
I'm a core developer with The Horde Project and a founding parter of Horde LLC - the company behind the world's most flexible groupware platform. This is my personal blog full of random thoughts about development and life in general.
September 11, 2008

iTunes, a network share, and Applescript

I've been slowly getting into Applescript lately since I've moved back to Mac for the majority of my work. I recently went through the process of moving the iTunes library file from my PC desktop to my Mac. After all, you can't have a Mac and use iTunes on your PC!

Anyway, I keep all the actual media files on a network share to ease with backup chores and to allow accessing the files with other players etc...  This hasn't posed any problems in the past, but now that I use a Macbook Pro to interact with iTunes, the network share is not always present.  I wrote a small Applescript to check for the existence of the share and then mount it if it's not there before opening iTunes. Since it seems that catching an error from a failed SMB mounting operation is difficult to do in Applescript, I attempt to grab the computer's IP address and only attempt the mount if it contains a value indicating I'm connected to my home network.

Yes, there is probably a better way to do this, and if anyone knows of one, feel free to drop me a line.

The code:

--Avoid undefined errors probably a better way set check_network to "" set check_wireless to "" try set check_network to do shell script ¬ "ipconfig getifaddr en0" on error set check_wireless to do shell script ¬ "ipconfig getifaddr en1" end try if check_network contains "192.168" or ¬ check_wireless contains "192.168" then tell application "Finder" if not (exists disk "music") then open location ¬ "smb://user:pass@server/music" end if repeat until (list disks) contains "music" end repeat launch application "iTunes" tell application "iTunes" activate end tell end tell end if