AppleScript for auto-mounting Samba shares
My LAN consists of a Linux server that, among other things, exports a few Samba shares, and a number of Mac OS X and Windows clients that mount those shares when needed. On Windows it is possible to tell the system that it should automatically re-mount a share on login, but on Mac OS X there is no such option. Because of this, up until now I had to manually mount my shares each time I logged in on any of my Mac OS X clients. Not anymore!
I was pretty sure that there must be an automated solution to this problem, and after googling around a bit today and finding some hints, I decided to go for the AppleScript solution. The script below is the one I now use; it mounts two shares (daten
and media
) on server osgiliath
, authenticating as user patrick
:
tell application "Finder" mount volume "smb://patrick@osgiliath:139/daten" mount volume "smb://patrick@osgiliath:139/media" end tell
To make this work yourself, launch Script Editor.app, create a new and empty AppleScript and copy&paste the code above into the script. Modify the code as required, then save the script as type "application bundle". You could also save it as type "application", which would give you a single file instead of the bundle directory structure, but that single file would have a resource fork which usually means trouble when you want to transfer the file to systems other than Mac OS X. Specifically, I prefer the "application bundle" type because I want to place the script under Git version control. Also note that in the "save as" dialog you should not tick the "run only" checkbox, because ticking that checkbox will prevent you from re-opening and editing the script later on.
Now that you have saved the script into executable form, you can add it to your login items in the System Preferences.app.
The final step is to place the password you need for authentication into your Mac OS X keychain. You could also embed the password within the AppleScript (the general form of the connect string looks like this: smb://user:password@server:139/share
), but this is obviously much less safe. So if your password is not yet in your keychain, I recommend manually running the AppleScript by double-clicking it in the Finder. A dialog will pop up, asking you for the password. Enter the password and tick the checkbox that allows you to save the password in your keychain. If you want you can use Keychain Access.app to verify that the password has been properly stored.
Now logout and login again to verify that everything works as planned. Voilà.
The only drawback to the solution I have found so far is that the script application bundle will briefly appear in the Dock while it executes. This can be fixed by manually editing the file Info.plist
inside the application bundle. Use either Property List Editor.app (from Apple's developer tools) or any text editor, and add the following key/value pair to Info.plist
:
<key>LSUIElement</key> <string>1</string>
(see this reference for a list of possible Info.plist
keys, and their values)