Shortcut to eject all external hard drives but not MobileBackups

So far I've been using the following AppleScript to eject all external drives at once conveniently using a shortcut:

tell application "Finder"
    eject (every disk)
end tell

This script is is stored in /Library/Scripts and I have assigned a shortcut to trigger it in FastScripts.

But there's one problem. When you have Time Machine Backups enabled, OS X mounts a virtual MobileBackups volume in /Volumes to store local snapshots. This volume will be ejected alongside all the physical external volumes. I want to avoid this as this will stop local snapshots from being made (until it's mounted again at next login).

How can I add an exception to exclude /Volumes/MobileBackups in the AppleScript above?


Solution 1:

This AppleScript code:

tell application "Finder"
    set diskList to the disks
    repeat with mountedDisk in diskList
        if name of mountedDisk is not "MobileBackups" then
            eject mountedDisk
        end if
    end repeat
end tell

ejects all mounted disks except when named MobileBackups, that is, /Volumes/MobileBackups.

Solution 2:

You can do the following instead:

tell application "Finder"
  eject (every disk whose ejectable is true)
end tell

If you have partitioned disks mounted, hold down the "Option" key, run your script, then release the key when done.

Solution 3:

I've slightly modified this to do exclusions as a set.

set exceptionsList to {"MobileBackups", "startup disk", "home", "net"}
tell application "Finder"
    set diskList to the disks
    repeat with mountedDisk in diskList
        if name of mountedDisk is not in exceptionsList then
            eject mountedDisk
        end if
    end repeat
end tell