Using Keyboard Maestro and AppleScript to Eject External Drives

I’ve been using a laptop + monitor setup for a while now, and I like it more than I expected. The last time I tried this, I had lots of problems getting the Mac and monitor to talk to each other when I’d plug it in. I prefer to keep the laptop closed when doing this (often called “clamshell” mode), and for years macOS did not like that.

Maybe it is the Apple silicon switchover or improvements to macOS, but that’s no longer a problem. I plug it in and start typing.

However, another friction point is disconnecting. I have a Time Machine and external storage drive connected to my Mac when attached to the monitor. It’s important that I properly dismount those drives before unplugging. (Just yanking a drive out of your Mac is a terrible idea.)

So … dismounting. There are several ways to pull it off. You could go in the Finder and eject the drive under “Locations” in the Sidebar. There is a handy little Eject button next to each removable drive. (In my case, Dagobah is my Time Machine Drive, and Batuu is my extra storage.) This solution works but is tedious.

There are also some excellent apps to solve this problem. My favorite is EjectBar. This is a menu bar app that, once you click it, lets you eject from the menu bar, saving you a trip to the Finder. A power tip with EjectBar is to Command-Click the menu bar icon, and it automatically ejects all connected drives.

I wanted, however, something more automated. So I made a simple AppleScript that ejects all external drives. Here’s the script:

tell application "Finder"
eject the disks
display dialog “Successfully ejected disks.” buttons {“Close”} default button “Close”
end tell
on error
display dialog “Unable to eject all disks.” buttons {“Close”} default button “Close”
end try

As AppleScripts go, this one is pretty basic.

I run this script as part of a Keyboard Maestro Macro. First, it quits all applications. I do this because I have very particular screen setups when I connect to the big monitor. I prefer to close everything out when disconnecting. (I have separate scripts for setting up screens on the laptop screen or the monitor when I plug back in.) Then I eject the drives by running the above AppleScript. I use the Hyper Key plus Q to activate it (⇧⌃⌥⌘Q). I’ve also mapped it to a button on my Stream Deck, which is how I usually activate it. This is the method that has stuck with me. When I’m ready to take the laptop, I push the button, wait a moment, and then unplug and head out. It really isn’t much trouble at all.

Here’s a download link to the Keyboard Maestro Macro.

If you’d like to get better at Keyboard Maestro, I’m putting up a $5 discount code on the Keyboard Maestro Field Guide for one week. Use code KMLAPTOPWARRIOR. 🙂

And here’s a screenshot of the Macro: