Automatically Connect Your Mac to Shared Windows Network Folders

This posting covers how to create a script (application) for your Mac to automatically connect to a shared Windows folder when you login to your Mac.

Creating the Script

  1. Open the Apple Script Editor: Applications > Utilities > AppleScript Editor
  2. Enter the script code below:
    (Note: if you copy/paste this code you may need to re-type all the “double quotes” to correct the syntax.)
  3. Click the Compile button to validate your code.

Script:

tell application "Finder"
if not (exists disk "SHARED") then mount volume "smb://MyServer/MyShare"
end tell

Saving the Script File as an AppleScript file

This will save the script in a format that can later be edited, if you chose to make changes.

  1. File > Save
  2. Specify a name for your script. For example: MapNetworkServers.scpt
  3. File Format: Script
  4. Run Only: Unchecked

Saving the Script File as an Application

This will save the file in an executable format. This is the file that will run at login.

  1. File > Save As
  2. Specify where you want to save your script file, and specify a file name. For example: MapNetworkServers.app. I created a folder called “Commands” in my Documents folder where I store my script and application file.
  3. Set the File Format to Application.
  4. Check the Run Only check box. This will cause the script to run and exit.
  5. Click Save.

You should now have two files:

  • MapNetworkServers.scpt – keep this file so you can modify this script in the future.
  • MapNetworkServers.app – this is the script you will use to configure to execute when you login.

Configure the Script to Run at Login

  1. Go to System Preference > Accounts
  2. Select the appropriate user; the user you want this script to run when they login.
  3. Select the Login Items tab.
  4. Click the “+” button at the bottom of the list of applications to add a new startup application.
  5. Browse to your MapNetworkServers.app file and select it.
  6. You should now see it in the list.
  7. Optional: You can check the Hide check box, if you like. I suggest not doing this until you know your script is working correctly.

That’s it. Your shared Windows server folders should now be mapped automatically when you login.

This sample script only maps one folder, the “MySharedFolder” folder. However, on my home network I have several folders on the same server that exist. If you have previously selected all the shared Windows folders (using Finder > Go > Connect to Server), and provide your login credentials to that server, all the shared folders on that server will appear; which is ultimately what I want anyway. This also allows your script to be shorter and easier to maintain by having to map one shared folder in your script instead of having to write a line of code for each and every shared foler on that Windows server.

Resources:
http://support.apple.com/kb/ht1568

Advertisements

4 thoughts on “Automatically Connect Your Mac to Shared Windows Network Folders

  1. Pingback: mounting servers | macgrunt

  2. G’day Mark

    Just for your info … You don’t actually need to choose the ‘run only’ option when saving this app. Run Only is used when you want to create an app which cannot be opened in a script editor — that is, it’s a way to protect your code when distributing an app. But you are definitely right that if you DO decide to go down the ‘run only’ path, you need to Save As, because once it’s ‘run only’ even the script author cannot open that script in an editor again — ever.

    Have a good one
    m.

  3. G’day John

    Never tried it… but according to the Standard Additions dictionary it should be :

    tell application “Finder”
    if not (exists disk “SHARED”) then mount volume “smb://MyServer/MyShare” as user name “YourUsername” with password “YourPassword”
    end tell

    m.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s