Last active
August 28, 2024 05:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better version of this using diskutil CLI: https://gist.github.com/pascalpp/56d0f8d70a7771eb1204d544459ffd88