How can I have my Mac mount a network drive when an application launches?

Using Automator and the excellent SleepWatcher utility, I have a setup that automatically connects to my network drive when the computer starts, then automatically reconnects when the computer wakes from sleep.

I've written a tutorial in a blog post.

An update in response to CajunLuke's question:

The Automator app connects to the network drive and incorporates a 10 second delay to ensure the network connection is ready.

SleepWatcher runs as a daemon, and allows you to schedule the execution of scripts when your computer goes to sleep or wakes from sleep. In this case you configure it to run a shell script when the computer wakes that calls the aforementioned Automator app.


And easy solution would be to automatically have your network shares mounted for you when you log in. A program like Network Drive Launcher would aid in automatically scripting and connecting to networked shares when you log in.

Alternatively you could write an Apple Script that would replace clicking on the iTunes icon to start iTunes, not ideal since you would have to click the script first.

  1. Open the AppleScript Editor application.
  2. Type in the following

:

--open the network location first
tell application "Finder"
    open location "smb://... OR afp://..."
end tell

--wait 10 seconds adjust this time to meet your needs
delay 10

--open iTunes
tell application "iTunes"
    activate
end tell
  1. Save the script File > Save

  2. Name it what you like but be sure to set the file format to "Application" on the "File Format:" dropdown.

  3. Run the script, you can treat it like any other Application.

Lastly, it looks like it may possible to write an Apple Script that does exactly what you want, here is a link to something that may be able to work with some modification. I could not get it to work on Lion, some syntax issues need to be resolved. Run a script whenever an application opens

What you need is an applescript that stays open all the time and performs its tasks periodically. So here's an example applescript for you. Save it as an application and check the "stay open" box to make it stay open after you launch it. The way a stay-open script works is there's 2 main handlers, the "on run" and the "on idle". "on run" runs once at application launch. "on idle" runs every so often. You set how often it runs by returning a number of seconds at the end of the handler. So here's the example... it sets up the application parameters in the "on run" handler and then in "on idle" it can check those parameters. Right now this script just displays the results but you can make it do whatever you want at this point. global applicationsToWatch, idleTime ...