A simple TouchBar widget to show mounted external drives and eject them all

Description
A very simple TouchBar widget I whipped up to both see and eject removable drives.

Disclaimer: I worked this out from various online posts regarding the commands used, and by testing it out on my Mac. There may be factors I have not taken care of, so use at your own risk!

I have, however, provided a run-down of my commands below if you want to explore the results before committing to use the widget.

The intended use case is when undocking your Mac laptop from a Thunderbolt hub/display and you need to eject all of the drives first. This widget shows you that this needs doing, provides the tap target to action the ejects, and feeds back that the ejects have been effective.

There are likely many improvements/optimisations possible, but it gets the job done in this form.

Screenshots
Touch Bar Shot 2022-10-16 at 16.54.01

Three mounted volumes are shown: Fencurch, Rupert, and Vogon. Tapping the button ejects them one by one. The widget updates every 5 seconds and disappears if no external drives are attached.

Preset
[
{
"BTTWidgetName" : "Drive Ejector",
"BTTTriggerType" : 642,
"BTTTriggerTypeDescription" : "Shell Script / Task Widget",
"BTTTriggerClass" : "BTTTriggerTypeTouchBar",
"BTTPredefinedActionType" : 206,
"BTTPredefinedActionName" : "Execute Shell Script / Task",
"BTTShellTaskActionScript" : "df | egrep '^/dev.+\s/Volumes.+' | sed -E 's|.+/(.+)|\1|' | xargs -n 1 diskutil eject",
"BTTShellTaskActionConfig" : "/bin/zsh:::-c:::-:::",
"BTTShellScriptWidgetGestureConfig" : "/bin/zsh:::-c:::-:::",
"BTTEnabled2" : 1,
"BTTRepeatDelay" : 0,
"BTTUUID" : "CF554E0E-F78B-4F53-BFF9-69F18C1209C1",
"BTTNotesInsteadOfDescription" : 0,
"BTTEnabled" : 1,
"BTTModifierMode" : 0,
"BTTOrder" : 2,
"BTTDisplayOrder" : 0,
"BTTMergeIntoTouchBarGroups" : 0,
"BTTTriggerConfig" : {
"BTTTouchBarOnlyShowIcon" : false,
"BTTTouchBarItemPadding" : 0,
"BTTTouchBarButtonColor" : "75.323769, 75.323769, 75.323769, 255.000000",
"BTTTouchBarIconInvert" : false,
"BTTTouchBarItemPlacement" : 1,
"BTTTouchBarAppleScriptStringRunOnInit" : true,
"BTTTouchBarAlwaysShowButton" : false,
"BTTTouchBarFontColorAlternate" : "255.000000, 224.000002, 97.000002, 255.000000",
"BTTTouchBarButtonHoverColor" : "248.880000, 146.115000, 128.010000, 181.050000",
"BTTScriptType" : 3,
"BTTTouchBarButtonHeight" : 15,
"BTTTouchBarButtonWidth" : 100,
"BTTTouchBarScriptUpdateInterval" : 5,
"BTTTouchBarItemIconHeight" : 22,
"BTTTouchBarButtonTextAlignment" : 0,
"BTTTouchBarAlternateBackgroundColor" : "75.323769, 75.323769, 75.323769, 255.000000",
"BTTTouchBarAppleScriptUsePath" : 0,
"BTTTBWidgetWidth" : 400,
"BTTTouchBarBorderColor" : "255.000000, 255.000000, 255.000000, 255.000000",
"BTTTouchBarColorRegex" : ".+(0.00/h)",
"BTTTouchBarItemIconWidth" : 22,
"BTTTouchBarFreeSpaceBeforeButton" : 0,
"BTTTouchBarButtonName" : "Drive Ejector",
"BTTTouchBarShellScriptString" : "df | egrep '^/dev.+\s/Volumes.+' | sed -E 's|.+/(.+)|\1|' | sort | tr '\n' ' ' | sed -E 's|(.+) |💿 \1\n|'",
"BTTTouchBarButtonFontSize" : 15,
"BTTTouchBarFontColor" : "255.000000, 89.239666, 79.767650, 255.000000",
"BTTTouchBarIconTextOffset" : 5,
"BTTTouchBarFreeSpaceAfterButton" : 5,
"BTTTouchBarAppleScriptString" : "let url = 'https://api.weather.com/v2/pws/observations/current?stationId=IWELLING421&format=json&units=m&apiKey=729cd4c0b5e2420a9cd4c0b5e2320aa0';\n\nfetch(url)\n .then((response) => {\n returnToBTT(response.json());\n })\n .then((data) => {\n returnToBTT(data);\n });"
}
}
]

The commands
There are two variations on the same command; one to generate the text for the button and one to action the ejects.

For display:
df | egrep '^/dev.+\s/Volumes.+' | sed -E 's|.+/(.+)|\1|' | sort | tr '\n' ' ' | sed -E 's|(.+) |💿 \1\n|'

The meat of this is df | egrep '^/dev.+\s/Volumes.+' which lists all filesystems and matches only those which:

  1. Are physical (start with /dev), and
  2. Are mounted as a volume (show a mount point inside /Volumes).

You can run this command in Terminal on your Mac and see what pops out. If it lists what you expect, then this should be a safe widget for you.

The rest of the command is just manipulating it for display — put it on one line, add spaces, and an emoji. Yes, I used a CD emoji, because that's the best I could do at a glance.

To execute:
df | egrep '^/dev.+\s/Volumes.+' | sed -E 's|.+/(.+)|\1|' | xargs -n 1 diskutil eject

You will see the same starting piece which nets the list of drives/volumes and these are then passed via xargs to the diskutil eject command which does the work.