AppleScript to mount/unmount a drive

On my Mac Pro I have two internal hard drives I rarely use. Some times they “sleep” and while doing so, every file opening dialog, search in Spotlight or search in Dictionary might wake them up and that takes some time. I could, of course, disable sleeping in power settings, but then the drives would be unreasonably wasting power all the time, so I decided to write an AppleScript to mount or unmount a drive depending on it’s state.

I’d like to emphasize: this script is meant to unmount an entire drive rather than just a volume. It takes a volume as an identifier of the needed drive. This is mostly useful for Mac Pros.

I will make a proper release of the script some time, now you can have a half-baked source code. The script requires Growl to be installed in the system. Tested only on Mac OS X 10.6 “Snow Leopard”. Please use at your own risk!

Change myVolumeLabel variable to the label of any volume on the drive you want to (un)mount.

set myVolumeLabel to "Storage HD"

tell application "GrowlHelperApp"
	set the allNotificationsList to {"Disk mounted", "Disk unmounted"}

	register as application "Disk Mounter" all notifications allNotificationsList default notifications allNotificationsList icon of application "Disk Utility.app"
end tell

tell application "Finder"
	set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[1-9]*' "
	if not (disk myVolumeLabel exists) then
		do shell script "diskutil mountDisk " & diskDev
		tell application "GrowlHelperApp" to notify with name "Disk mounted" title "Volume mounted" description "Disk “" & diskDev & "” with volume “" & myVolumeLabel & "” has been successfully mounted." application name "Disk Mounter" icon of application "Disk Utility.app"
	else
		do shell script "diskutil unmountDisk " & diskDev
		tell application "GrowlHelperApp" to notify with name "Disk unmounted" title "Disk unmounted" description "Disk “" & diskDev & "” with volume “" & myVolumeLabel & "” has been successfully unmounted." application name "Disk Mounter" icon of application "Disk Utility.app"
	end if
end tell

Last updated: 30.05.10

Update: please note that unmounting an internal HDD screws up some links, for instance, if you had shared folders — they will not be shared after re-mounting the drive. Use the script only with the drives that you never/link use at all in Mac OS — in my case, this applies to the Bootcamp drive.

This entry was posted in Programming, Releases and tagged , . Bookmark the permalink.

13 Responses to AppleScript to mount/unmount a drive

  1. Frank says:

    Thanks for the AppleScript code. It works but on re-mounting (Snow Leopard 10.6.2), the drive does not appear in the “Finder”. Is there another command I need to attach?

    Thanks,
    f

    • Dae says:

      it works for me, checked just now

      possibly you have a detachable drive (connected using a USB or firewire), I guess there should be some different logic

      if that is true, please confirm and I’ll try to fix it for you

  2. avogadro23 says:

    Tried using it to unmount my “BOOTCAMP” partition, and it fails. “Finder got an error: unmount failed for disk”. Seems that “do shell script “diskutil unmountDisk ” & diskDev” can’t find my disk.
    Running 10.6.3

    • Dae says:

      This script is supposed to unmount a drive, not a partition. This is useful solely for Mac Pros.

  3. zero says:

    Very useful script indeed, thanks, and i’ve been looking for something like that now for ages.

    But i have a tiny problem with it, namely if the Volume you’re trying to mount/unmount has a space in it like ‘Macintosh HD’ or ‘Time Machine’, the script only results in an error upon execution.
    Like when i want to unmount the Volume ‘Time Machine’ if gives this error: ‘can’t find the volume ‘Machine’ ‘
    This is most likely the way this is programmed in Applescript, since when it involves a space, Applescript thinks you already mention the next argument then.
    Sadly i can’t rename all my Drives without a space or some other character, this would completely mess up the file structure i made.

    Any way to fix this?
    I’ve been trying all day to come up with something.

    • zero says:

      After further inspection it seems that diskutil itself has problems with spaces.
      A simple bash command with diskutil unmountDisk ${diskName} also fails. argh.

  4. Tom Payne II says:

    Thanks for the post! I just modified it to deal with SMB shares and it seems to be working great. I put my modified code up at: http://ctp2nd.com/post/669945321/applescript-to-auto-mount-a-smb-share and linked back here for the original. Thanks again. -Tom

  5. Luis says:

    Does a similar script exist that will mount and unmount a hard drive, not a volume.
    I’m trying to find something that will unmount/remount a continually connected external hard drive? Thanks for any help!

    • Dae says:

      As I say above,
      “I’d like to emphasize: this script is meant to unmount an entire drive rather than just a volume. It takes a volume as an identifier of the needed drive.”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>