Eject all external drives and shut down?

When I leave work I usually shut down my Mac before then turning off power to all devices via the switches on a pair of power strips. I've done it this way for many years and it's never been a problem as all external hard drives wind down when the Mac is powered off.

But now I have an external enclosure with several HDDs and SSDs in it (not a RAID) that works differently. The company that made it recommends manually ejecting all drives before shutting down the computer and turning off power.

Is it possible to write a macro that:

  1. Properly unmounts all external drives except for the TimeMachine drive.
  2. Shuts down the computer, but only if 1) has been successful

?

I cannot quite figure out how to approach this.

To unmount a drive called "External Drive 1" run the following command inside an Execute a Shell Script action with the following command.

diskutil unmount "External Drive 1"

Create an action like that for each of your other external drives. The default behaviours for the Execute a Shell Script action should suit your use case. Just change the third drop-down menu to read "ignore results" (so that you will not get any notifications unless an action fails).

Your macro's final action is of course Shut Down Computer.

The default settings for the shell script actions mean that if a drive cannot be unmounted, the macro will stop before the shutdown action can run.

Alternatively, I think for your needs it would be just as good to put the commands inside the same action, e.g.

diskutil unmount "External Drive 1"
diskutil unmount "External Drive 2"
diskutil unmount "My backup"

In the unlikely, but possible, event that diskutil is unable to unmount one of the drives, you can try the following (example) command instead:

osascript -e 'tell application "Finder" to eject "External Drive 1"'

I will try that, thank you!

1 Like

I use this AppleScript

try
	tell application "Finder"
		eject the disks
		display notification "Successfully ejected all disks." with title "KM - Sleep Computer Macro" sound name "Frog"
	end tell
on error
	display dialog "Unable to eject all disks." buttons {"Close"} default button "Close"
end try
3 Likes

Will try that as well, thanks!