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.
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
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
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
This script is supposed to unmount a drive, not a partition. This is useful solely for Mac Pros.
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.
After further inspection it seems that diskutil itself has problems with spaces.
A simple bash command with diskutil unmountDisk ${diskName} also fails. argh.
I’ll try to fix the script accordingly.
Bug fixed, see the updated version above. I simply forgot the quotes, shame on me :-)
Thanks for the report!
Thanks Dae, that worked :)
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
Cool, thanks! I’ve linked back your post in my “recommended” list on the home page.
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!
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.”
Script works like a charm! Thank you!
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.
I’m glad to know it helped!
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?
I copy/pasted it too, works fine (on Snow Leo).
is the “myvolumelabel” what your harddrive is called – for example on your desktop?
my hard drive is called “Storage HD”
obviously you need to adjust the first line to the name of your hard drive
also, how should I modify the script if I just wanted to have the mount/unmount function, without grown notifications?
Remove the first “tell” block and all lines that start with “tell application “GrowlHelperApp””
thanks a lot :)
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.
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
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:
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.
Thus, the unmounting is confirmed.
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.
instead of
the list of disk’s was limited and not 0 !
(had the same problem)
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
I’ve skinned the code down to
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.
Never mind
instead of
the list of disk’s was limited and not 0 !
What a stupid mistake. Thank you Lewis, I updated the above script.
works like a charm
happy to have found this
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!
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. :'(
You could try this in Terminal:
Put your internal HDD name at the ellipsis, e.g.
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
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.
Have you managed to unmount the drive using Terminal? Why do you think its a cable issue of all sudden?
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.
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
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
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.
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
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!
You don’t need a script to mount a drive automatically after a reboot. Simply drag the proxy icon (what is a proxy icon?) of your network drive to Login Items (System Preferences > Users & Groups > select user > Login Items).