Applescript to Auto-Mount a SMB Share
I’ve tried damn near everything to get my smb shares to mount and stay freaking mounted, but to no avail. The last solution I tried involved modifying the /etc/fstab file and all that did was cause my mac to crash when the share randomly became unavailable. Yesterday, I wrote a simple Applescript that had:
mount volume ‘smb://user:pass@server/MyShare’
and everytime I noticed the share would drop, I’d re-run that line and everything was pretty good.
Today, with a little patience and some Google-Fu, I found and modified this script: http://dae.cyberic.eu/blog/applescript-to-mount-unmount-a-drive/ to resemble the following:
property minutesBetweenSaves : 0.5
on run
tell application "GrowlHelperApp"
set the allNotificationsList to {"Disk mounted"}
register as application "Disk Mounter" all notifications allNotificationsList default notifications allNotificationsList icon of application "Disk Utility.app"
end tell
tell application "GrowlHelperApp" to notify with name "Disk mounted" title "About to start monitoring the MyShare mount" description "I'll update you in a few when I finish my first checksee" application name "Disk Mounter" icon of application "Disk Utility.app"
end run
on idle
set myVolumeLabel to "MyShare"
set myMountString to "smb://user:pass@server/MyShare"
tell application "Finder"
if not (disk myVolumeLabel exists) then
mount volume myMountString
tell application "GrowlHelperApp" to notify with name "Disk mounted" title "Volume mounted" description "Disk “" & myMountString & "” with volume “" & myVolumeLabel & "” has been successfully mounted." application name "Disk Mounter" icon of application "Disk Utility.app"
end if
end tell
return minutesBetweenSaves * 60
end idle
on quit
continue quit
end quit
Read the article above for a better understanding of the “big picture”, but in a nutshell, this script is meant to be saved as an application with “stay open” checked. When it runs, it’ll do the “on run” section and register itself with Growl then let you know its about to try to mount your share. Then, it moves into the idle section and checks to see if the volume “My Share” exists. If not, it will run the “mount volume” command from above and let you know that its done. This part of the script repeats every 30 seconds until I close the application.
Hope it helps someone out there. If so, shoot me and the page above a comment.
Thanks!
2 months ago | Permalink

