Skip to content

Instantly share code, notes, and snippets.

@pascalpp
Last active August 6, 2023 16:32
Show Gist options
  • Save pascalpp/6a694bdacbc04a4451da27147a25e8e7 to your computer and use it in GitHub Desktop.
Save pascalpp/6a694bdacbc04a4451da27147a25e8e7 to your computer and use it in GitHub Desktop.
An AppleScript to unmount all ejectable disks so I can unplug my MacBook from my dock
-- An AppleScript to unmount all ejectable disks so I can unplug my MacBook from my dock
-- map this script to a hotkey with an app like Keyboard Maestro - I use Cmd+End
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
display notification "Please wait…" with title "Unmounting disks"
-- some of my disks are finicky so I repeat this three times
-- but it's usually pretty fast
repeat 3 times
set theDisks to every disk whose ejectable is true
repeat with theDisk in theDisks
set theDiskname to name of theDisk
if disk theDiskname exists then
eject disk theDiskname
end if
end repeat
end repeat
-- make sure it worked
set notEjected to 0
repeat with theDisk in theDisks
set theDiskname to name of theDisk
if length of theDiskname < 20 then
if disk theDiskname exists then
set notEjected to notEjected + 1
end if
end if
end repeat
if notEjected is equal to 0 then
display notification "Get out of here." with title "All disks unmounted" sound name "Glass"
else
display notification "There was a problem unmounting some disks." with title "Oops!"
end if
end tell
@pascalpp
Copy link
Author

pascalpp commented Aug 6, 2023

Better version of this using diskutil CLI: https://gist.github.com/pascalpp/56d0f8d70a7771eb1204d544459ffd88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment