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 uses a volume name to identify the 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[0-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: 24.06.11

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.

44 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

  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.”

  6. Paul says:

    Script works like a charm! Thank you!

  7. Gavin says:

    This is great! It works with my external USB disk perfectly. Now I can mount it without having to turn the switch on and off again, having to enter terminal lines, or fudge around in Disk Utility.

  8. Davur i Dali says:

    I tried this, but it returned the following syntax error :/

    “Expected end of line, etc. but found “””

    I just copy pasted what you posted here. No-one else get the same error as me?

  9. Davur i Dali says:

    is the “myvolumelabel” what your harddrive is called – for example on your desktop?

  10. Davur i Dali says:

    also, how should I modify the script if I just wanted to have the mount/unmount function, without grown notifications?

  11. Ian says:

    I get error “Finder got an error: Unmount failed for disk” number 1, in the line

    do shell script “diskutil unmountDisk ” & diskDev

    I understand that the script you wrote makes the volume label point to the disk you want to unmount. I am using a Macbook Pro with two physical disk drives (I replaced the optical disc drive with another HDD) and am trying to unmount the drive that doesn’t have OS X 10.6.6 installed on it.

    Perhaps I can’t unmount disks but only partitions on a Macbook Pro.

    • autnagrag says:

      Ian —-

      My MacBook Pro is set up like yours. When I unmount the second internal drive, I just use a very simple script as below. You can probably figure out how to graft the growl notifications into it if you want to use them.

      First, look at the output of

      diskutil list

      in a few different situations (no external volumes, many external volumes) and make sure the internal drive has a consistent label. It will probably be disk1, with your boot drive being disk0.

      Here’s the script:

      tell application "Finder"
      	if (exists disk "foo") then do shell script "diskutil unmountDisk disk1"
      end tell

      I just tested it on my second internal drive, and it worked fine. The script is so simple even I can understand it.

      People who know more about Growl and Terminal than I do can probably make what Terminal returns after one runs the command (in a Terminal shell, not from an AppleScript) show up as a Growl notification.

      me$ diskutil unmountDisk disk1

      Thus, the unmounting is confirmed.

      • autnagrag says:

        Actually, Dae’s script works really well on the MacBook Pro with two internal hard disks under 10.6.7.

        Make sure there are no typos in line one when you specify the name of a volume whose parent disk you wish to be completely ejected.

      • Lewis says:
        set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*'"

        instead of

        set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[1-9]*'"

        the list of disk’s was limited and not 0 !

        (had the same problem)

  12. Russell Roberts says:

    Hi Dae
    Thanks for this script its just what I am looking for. I also have a raid drive but it only mounts one of the drives please advise

    thanks so much

    Russell

  13. Lewis says:

    I’ve skinned the code down to

    set myVolumeLabel to "Storage"
    
    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
    	else
    		do shell script "diskutil unmountDisk " & diskDev
    	end if
    end tell

    and for some reason I just get this error ‘error “Finder got an error: Unmount failed for disk” number 1’ without fail, everytime. It’s only started happening since I’ve formatted my drives and had a fresh install. I want to run the script on startup, I would just use something like ‘do shell script “diskutil unmountDisk disk0″‘ but for some reason my drives keep switching identifiers so I need something more adaptive and dynamic.

    Look forward to hearing from you Dae!

    Lewis.

    • Lewis says:

      Never mind

      set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[0-9]*'"

      instead of

      set diskDev to do shell script "diskutil list | grep \"" & myVolumeLabel & "\" | grep -o 'disk[1-9]*'"

      the list of disk’s was limited and not 0 !

  14. pingfreak says:

    works like a charm
    happy to have found this

  15. Dave says:

    Fantastic script! I have a really fast drive that I use for music but it’s way too loud and keeps spinning up/down. This script solves all these problems!

  16. Victor says:

    I am trying to partition my new internal hard disk so that I can install OS X on it. I am currently running my mac on an external drive. When I try to partition my drive, it get an error: “Could not unmount disk.” Can this script help me with my problem? Please help me. :'(

    • Dae says:

      You could try this in Terminal:

      diskutil unmountDisk force ...

      Put your internal HDD name at the ellipsis, e.g.

      diskutil unmountDisk force "Macintosh HD"

      Make sure your current external drive is not called the same as your internal drive though.

      You may also use a unix device path to it. (E.g. /dev/disk1). To find it, in Terminal, use

      diskutil list
      • Victor says:

        Do you think that this could be a problem with the Hard drive cable inside my computer? I was able to boot with my external drive as an internal on my sisters mac.

  17. Thanks for the post! After I modified it, it seems to be working great and good.This script solves all these problems!
    Thanks for this script, its just what I am looking for. It works with my external USB disk perfectly. Now I can mount it without having to turn the switch on and off again, having to enter terminal lines, or fudge around in Disk Utility.

  18. Guy Hemmings says:

    Brilliant! I have been looking for an easy way to mount my external Firewire drive only when needed, and this saves me a tedious trip to Disk Utility every time I need the disk available.

    Thanks very much!

    Guy

  19. Comparlo says:

    That is a clever bit of script. I wasn’t sure if it would work on my current macbook pro as i noticed this post was over a year old but it works great still. Thanks

  20. Carol Wright says:

    This surely did a great shortcut for me. With the Script, it eliminated going through many switches and the to and fros on terminals. You are doing many a great favor so keep up the good work. We may not be able to reciprocate a good gesture but hope someone will.

  21. Pete says:

    I’ve been trying to figure out a way to disable the optical drive (Superdrive) with applescript.

    I toyed around with this script, but it didn’t work; it’s probably not meant for it. Is there any tweak I could make to this script, or does anyone have a suggestion?

    I only want my burning software to default to the external USB optical drive. When I set the preferences it always goes back and my users freak out.

    Thanks,

    Pete

  22. Ralph Quinones says:

    My AppleScript script does not work anymore in OS X Sierra.
    \tell application “Finder”
    mount volume “smb//DiskStation/Video” as user name “admin”
    mount volume “smb://Diskstation/Music” as user name “admin”
    mount volume “smb://Diskstation/Photos” as user name “admin”
    mount volume “smb://Diskstation/Downloads” as user name “admin”
    mount volume “smb://Diskstation/Documents” as user name “admin”
    end tell

    What changes do I have to make to get this to work? I have a synology and I am trying to mount the drives after a reboot with this AppleScript. It worked before I upgraded to Sierra!

Leave a Reply

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