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 Editor.app (/Applications/AppleScript/Script Editor.app), 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
    else
      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:

<key>LSBackgroundOnly</key>
<string>1</string>

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 Monitor.app (/Applications/Utilities/Activity Monitor.app):

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 Preferences.app (/Applications/System Preferences.app), 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
 

Adam Merrifield

 

2 thoughts on “Auto mount/unmount your Mac volumes when required

  1. Nice! I’ve been looking for options to selectively spindown my disks, as pmset (which manages the power config in os x) only has a global setting for all disks. How is the idle time defined in your script, is it the display sleep time in the power settings?
    Is it possible somehow to set the idle time individually on each disk? I really want to use something like this on my server but I don’t know how to define the idle state as I’m mostly using it as an afp server.

    1. All the script does is look for a particular app name in the active processes. When it’s detected the script spins up the required disk. No timing involved.

Leave a Reply