Mac OS X: Automatically connect to a network drive when your computer starts up or wakes from sleep
If you, like me, have dipped your toes into the world of network-attached storage (NAS), you've probably been enjoying the benefits of having your music and photos stored in a central location accessible from all your household Macs. Equally likely, you've run into the annoyance of starting an application such as iTunes, iPhoto or Picasa that relies on the network drive being available, only to discover that it, well, isn't.
Whereas it's fairly trivial to instruct your Mac to connect to a network drive on start up, there is no support in OS X for reconnecting to that drive when the computer wakes from sleep. Fear not, I have a solution for you that will ensure your network drive is always available. We'll be using Automator, shell scripts and the excellent SleepWatcher utility to make it work, so the tutorial assumes you have at least a passing familiarity with the command line.
At the time of writing, I'm running OS X Mountain Lion 10.8.2, but I've had this working on 10.6 and 10.7 as well. If you notice any differences or have any tips, please share them in the comments below.
Step 1: Create an Automator app that connects to your network drive
- Launch Automator.
- Choose Application from the new document dialogue.
- In the search field enter pause.
- Drag the pause action from the results to the right pane.
- Enter 10 seconds to slightly delay the connection attempt to ensure your network connection (wi-fi or otherwise) is ready.
- Do a search for server in the search field.
- Now drag Get Specified Servers and Connect to Servers (in that order) to the right pane.
- In the Get Specified Servers dialogue click Add... and select or enter the address to your network drive. Click OK.
- Disconnect from your network drive (eject it), and click Run in Automator to test it. Your network drive should connect after a 10 second delay.
- Now save your application. I created a folder called Scripts in my home directory and saved it in there as ConnectToNetworkDrive.
Step 2: Add the Automator script to your startup items to reconnect to the network drive when you restart
- In System Preferences select Users & Groups then your user account.
- Select the Login Items tab then drag your newly created Automator app onto the pane.
Step 3: Create a shell script that launches the Automator app
- Create a file on the command line or open your text editor and paste the following in there:
#!/bin/sh echo 'open ~/Scripts/ConnectToNetworkDrive.app/' | /bin/sh\&
- If you've chosen a different name for your Automator app, you'll have to update the script to reflect that.
- Save this to your Scripts folder as ConnectOnWake.sh or something equally descriptive.
- We'll have to make that script executable so launch Terminal.app and navigate to your Scripts folder. Assuming you've created it in your home directory and named the script as I have, you would do this:
chmod u+x ~/Scripts/ConnectOnWake.sh
Step 4: Install SleepWatcher
- Download SleepWatcher from http://www.bernhard-baehr.de/.
- Follow the SleepWatcher installation instructions in the enclosed readme.rtf. If you've never installed SleepWatcher before, make sure you follow the instructions under the heading Installation for new SleepWatcher users.
Step 5: Use SleepWatcher to call our shell script when the computer wakes from sleep
- We'll need to create a SleepWatcher configuration file (.plist) that specifies our shell script. We'll use one of SleepWatcher's example files as a starting point. They're located in the config directory in the SleepWatcher download, so go there.
- Make a copy of de.bernhard-baehr.sleepwatcher-20compatibility.plist. I simply called mine WakeNetworkDrive.plist.
- Edit the new file (either on the command line or with Xcode if you have it installed), so that it matches the following:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>wakestora</string> <key>ProgramArguments</key> <array> <string>/usr/local/sbin/sleepwatcher</string> <string>-V</string> <string>-w ~/Scripts/ConnectOnWake.sh</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> </dict> </plist>
- Now copy the plist file to either /Library/LaunchDaemons (to activate it for all users) or ~/Library/LaunchAgents (to activate only for the current user). I chose the latter option.
- Start the SleepWatcher daemon so that it runs automatically the next time you turn on your computer:
sudo launchctl load ~/Library/LaunchAgents/WakeNetworkDrive.plist
- If you get an error message from the above command, change the ownership of your plist file and then try again:
sudo chown root:wheel ~/Library/LaunchAgents/WakeNetworkDrive.plist
- Now test! Restart your computer. Your network drive should automatically connect after 10 seconds. Eject the network drive then put your computer to sleep (close the lid, for example), wait a few seconds then wake the computer. Your network drive should connect within 10 seconds (you might see an animated gear icon in the menu bar while this is happening).
If you have any tips or suggestions, please share them in the comments.
—
Posted Tue, 2 Oct 2012 - 20:30