Sys – Automatically Mount Server Using AppleScript

I recently discovered you can set your Mac to automatically mount a server when you start up.  Just drag the mounted server from your desktop to your ‘Login Items’ in your ‘User & Groups’ panel in ‘System Preferences’… and voila!

However it also opens a finder window directed at the server even when you click the ‘hide’ check box.  I started thinking of a way to do this with AppleScript, a language that I’m slowly getting more and more comfortable with.

What’s nice is that you can add saved AppleScripts to your ‘Login Items’.  Finder will then run the script at login.

I started by trying the following script which connects to the server and then closes the window.  Server path and server name are found by hitting cmd+i when your server is mounted and selected from your desktop.  My path for example starts with ‘afp://’.

#Connect to server.
tell application "Finder" to open location "server path/server name"
#Wait until the window opens.
delay 10
#Close window.
tell application "Finder" to close Finder window "server name"

This works perfectly but only at work.  When I’m at home and I can’t connect to the server the script will return an error when it can’t find the window (because the server has not been connected). So yesterday on the cycle home I’m thinking… ‘what I need here is an ‘IF’ statement.

An ‘IF’ statement is a way of saying – if a certain set of circumstances are true
then do this command other wise do this command instead. It’s perfect for this script as I want Finder to first check if the window is open. If it is open then close it otherwise do nothing and don’t return an error.

You can download my AppleScript here… MB_ConnectToServer_v01

And here’s my final code…

tell application "Finder" to open location "server path/server name"
#Wait until the window opens.
delay 10
#Close the window if it exists, if it doesn't do nothing.
tell application "Finder"
      if Finder window "server name" exists then
           tell application "Finder" to close Finder window "server name"
      end if
end tell
Advertisements

4 thoughts on “Sys – Automatically Mount Server Using AppleScript

  1. Didn’t work for me unfortunately. Adding the script to my login items is the same as double clicking it, which launches the script inside the editor instead of running it. Opening scripts in script editor is the default action for handling it in OS 10.12

    Also the Mac doesn’t remember the login even if you check it, so even with the login added in the script, the login dialog box appears every time and requires attention.

    Like

    1. Try saving the script out as an Application. In Script Editor go File>Export and change the File Format to Application. Add that to your Login Items and it should work.

      I’ve changed the uploaded .zip file to reflect this.

      Like

      1. Thanks, I noticed that error just after posting the above comment. And I couldn’t edit it since it was being reviewed.

        However my Mac still comes up with a login prompt every time using this script. Having read some more, this is apparently a security “feature” in the latest Mac OS and cannot be easily bypassed.

        I have however (hopefully) solved my problem. Another commentor on another forum has noticed that if you are logged into your shared disk and then drag this mounted drive icon onto the right side of the dock, it will automatically be opened on future startups as long as it is remembered in your keychain. No login item neccessary at all.

        We’ll see if this continues to work after more reboots and system updates. Not as cool as your solution but simple. As of right now it works! 🙂

        Thanks for helping out!

        Liked by 1 person

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