Auto mount/unmount your Mac volumes when required

Earlier this month you might recall the solution I gave you for keeping unused volumes unmounted on your mac. The next part of the equation, automatically mounting those volumes when needed to run my backup scheme, took me a little longer to sort out. In fact I wasn’t able to write a solution on my own, try as I might, so I finally went searching for one.

I needed a script of some sort that would mount my unmounted volumes when it was time for ChronoSync to run and then unmount my volumes when ChronoSync was finished. After several IRC queries, forum posts here and there and countless Google searches I finally stumbled upon this post at Mac OS X Hints. This solution was the answer I needed and it works perfectly. I won’t recap the whole thing here, but I will give you the bits that were most important to me.

Copy the following script into Script (/Applications/AppleScript/Script, changing the diskname and appname to suite your needs:

property diskname : "MyDisk"
property appname : "ChronoSync"

on idle
  tell application "System Events"
    set x to the name of every process
    if appname is not in x then
      if (exists the disk diskname) then
        do shell script "disktool -l | egrep -i "Mountpoint = '/Volumes/" & diskname & "" | cut -d' -f2 | xargs -n1 disktool -p"
      end if
      do shell script "disktool -l | egrep -i "Mountpoint = '', fsType = 'hfs', volName = '" & diskname & "" | cut -d' -f2 | xargs -n1 disktool -m"
    end if
  end tell
end idle

Next you need to save it as a bundled app and select “Stay Open“, give it a useful name and save it where you will be able to find it. In my case I chose /Library/Scripts/ChronoSync/:

Then you have to make it run in the background. To do this, find your newly created app, right click on it, “Show Package Contents“, find the Info.plist and open that in your favorite plain text editor. Above the key that says CFBundleAllowMixedLocalizations you want to add the following:


Get out of the package and find your app again and double click on it. It should launch in the background but not show in the dock. You can see that it’s running by opening Activity (/Applications/Utilities/Activity

Now to truly make this process automated, you need this app to be on when your computer is on, so it needs to launch when you login. So open your Accounts preference pane in System (/Applications/System, select the Login Items tab, select the plus button and add your newly created app:

And that’s it! Next time your backup program fires up to do it’s regularly scheduled backups, your disk mounting app will mount your volume, wait for your backup app to finish and then quietly tuck your volume back up for the night.

Other references

  1. A Script to mount/unmount a volume on app launch
  2. An AppleScript to mount, run, unmount a disk image
  3. MountPart