Automatically mounting a shared drive in Mac OS X

Windows has had mapped shared drives for years, but for some reason maintaining a persistent mount in OS X is a chore. Anyway, I ended up bashing out this script (tested on OS 10.8 Mountain Lion) to mount the drives on my NAS. I added to the Login Items so it runs every time I log in, and if you want to add to cron so it runs every so often, go for it.

set AirportName to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | sed ‘/ SSID: /!d; s/^.* SSID: //’"
tell application "Finder"
if AirportName is "Cosmic" then tell application "Usable Keychain Scripting"
set myInternetKeys to every keychain item of current keychain whose name contains "Asura"
repeat with i from 1 to (count of myInternetKeys)
set theKey to item i of myInternetKeys
set theAccount to account of theKey
set thePassword to password of theKey
end repeat
end tell
end tell
tell application "Finder"
mount volume "afp://Asura/Share" as user name theAccount with password thePassword
end try
end tell

How does it work? Well first it checks if I’m connected to my wifi (called “Cosmic”), then it uses the marvellous Usable Keychain Scripting to get the login credentials to mount with network shares with. Simples.