In working from home, I shuttle between two MacBooks in my home office setting. I use a Thunderbolt dock to connect external drives (both spinning and solid state) that I want access to on each computer. In addition, I have a USB-connected keyboard and I re-use my mouse by changing Bluetooth channels. But ejecting all the drives each time has led to me having to take too much time to manually find the drives in the Finder, eject them, and deal with any “Force Eject” issues that may come up.
One of the reasons I often get this “Force Eject” is because my photo library is too big for my internal SSD. I therefore use one of the external drives to house the photo library. It’s important to quit the Photos app first. Therefore, for me, I included this in the script below.
In order to facilitate this, open Shortcuts on MacOS. I am using macOS Sequoia for this solution. Create a new Shortcut and name it. I used “Eject All Disks.” Then add an AppleScript to your Shortcut by searching ‘Run AppleScript” and dragging that over to the left. Then paste the following code.
-- Quit Photos if it is running
tell application "System Events"
if exists process "Photos" then
tell application "Photos" to quit
-- Wait for Photos to quit fully (optional)
repeat 10 times
if not (exists process "Photos") then exit repeat
delay 0.5
end repeat
end if
end tell
-- Force-eject all external drives
do shell script "
#!/bin/bash
for disk in $(diskutil list | awk '/external/ {print $1}'); do
# Forcefully unmount all volumes on the disk
diskutil unmountDisk force $disk >/dev/null 2>&1
# Attempt to eject the disk after forced unmount
diskutil eject $disk >/dev/null 2>&1
done
"
If you do not need the Photos app thing, you can eliminate the first block. You may have other apps you want to quit, and you can modify that code to include your app.
You can test the app for your environment by clicking the Run button (play). Then, when you’re satisfied, I added mine to the Dock (File > Add to Dock.) You can trigger the script in other ways, including putting it in the menubar or adding it as a system extension, but the Dock for me seemed the most logical. You can also use Siri to run the script.
When you add the application to the Dock, your app will appear in your home folder under Applications (~/Applications/…). You may need to give the app full disk access to handle the ejecting. To do this, go to System Settings > Full Disk Access > and add your new application to the list, with it turned on.
I am sharing this because it took me some time to develop and hope this is helpful to other Mac users who use a docking station situation in their setup.