A quick-and-hacky AppleScript to eject every disk that can be ejected.
Written to make disconnecting a laptop easier. The idea: hit a keyboard shortcut, wait a couple of seconds, and safely unplug when told.
This script will read every disk that Finder reports as "ejectable", and attempts to eject them. It offers configuration options to disable ejection for specific disks, and makes its actions clear in a simple dialog after it runs.
It works well as a system-level service with a keyboard shortcut.
You can test it by opening eject-all-ejectables.applescript
in Script Editor or Script Debugger and hitting the
You can use this script with Alfred, simply by keeping the eject-all-ejectable.applescript
file somewhere on your device, and enabling AppleScripts
under Alfred Preferences -> Features -> Default Results.
This script is most useful as a system-level service, which you can use by invoking a global keyboard shortcut (like ⌃⌘⌥E
) to quickly untether from your desk.
Unfortunately, this is a bit complicated to set up.
- Open Automator and create a new document
- Select
Service
as your document type - At the top of the editor area, ensure that the following dropdowns are set:
Service receives selected
tono input
in
toany application
- On the left-hand side, search for "Run AppleScript". Drag the result into the editor area.
- Set the text area that appears to read (replacing the comment by copying-and-pasting):
on run
-- Replace this comment with the content of eject-all-ejectable.applescript. Remove any `use` lines at the start.
end run
- Save the service. I recommend the name "Eject all disks"
- Open System Preferences
- Go to
Security & Privacy
- Under
Privacy
, selectAccessibility
. - Click the padlock to unlock, if it's locked.
- Hit +. Add
Automator
from your/Applications
directory - Hit + again. Hit
⇧⌘G
, and browse to/System/Library/CoreServices
. SelectFinder
- Go back to the home of System Preferences. Select
Keyboard
. - Select
Shortcuts
- On the left, select
Services
- Hunt-and-peck for the "Eject all disks" item on the right. It will probably be under General.
- Ensure that the checkbox is checked.
- Select a keyboard shortcut by double clicking the area to the right of the title, and pressing the keyboard combination you want to use. I recommend
⌃⌘⌥E
. - Log out and log in again.
- Pray to the Apple gods that they have blessed you on this day, and will allow you to proceed without frustration
- Press
⌃⌘⌥E
(or whichever keyboard combination) you selected. Your disks should eject, and an alert should appear.
Obviously, this is not ideal.
At the top of the file are a number of configurable settings. You can change them as desired.
Name: safeMessage
Purpose: Specifies the title to show on the alert dialog when all ejectable disks were ejected.
Default: "Safe to disconnect"
Name: failMessage
Purpose: Specifies the title to show on the alert dialog when one or more disks failed to eject.
Default: "Some disks did not eject"
Name: showSkipped
Purpose: Show a line in the alert dialog listing disks which were intentionally not ejected.
Default: false
Name: drivesToSkip
Purpose: A list of disk names which should not be ejected, even if Finder reports that they're ejectable. This may be useful for network drives, SuperDrives, SD cards, or some internal drives.
Default: {}
Examples:
set drivesToSkip to {"External file storage"}
set drivesToSkip to {"Some network location", "SDCARD"}