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