This is odd. It works perfectly for one of my drives, "Time Machine", but not for the other two, "AUDIO" and "Installers & Sessions". Could that be something to do with the former's capitalisation and the latter's inclusion of the "&" character?
So those are the main macros that then trigger the sub macros provided by @DanThomas?
I'm confused because of the instructions on the original macros:
USAGE:
Call this using an "Execute a Macro" action using "with parameter". In the Parameter field, pass the name of the drive, followed by "|", followed by the name of the variable to receive the result.
Example: "5TB Time Machine|isMounted" (without the quotes).
You don't seem to have that at all, the Execute a Macro and then the parameter thing...
...and I suppose you could make a pick-list of ejectable drives.
I made a start, but have a few issues:
You can make multiple selections in the list prompt, but I'm not sure how to do a "for each" in the AppleScript. I thought it might be along the lines of:
set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set toEject to getvariable "Local__Eject" instance inst
end tell
tell application "Finder"
set theList to every disk whose ejectable is true
repeat with eachItem in toEject
eject eachItem
end repeat
end tell
...but apparently AS can't "handle objects of this class".
Also, the regex was removing both instances of lines containing "Macintosh HD", and now only one.
One issue would be that you can't pass an AS list object to a KM text-only variable then back to AS as a list object without doing some parsing.
I'd do it all in one AppleScript:
tell application "Finder"
set ejectableList to name of every disk whose startup is false and (ejectable is true or local volume is false)
tell application "System Events" to set theDisks to (choose from list ejectableList with title "Choose to Eject:" with multiple selections allowed without empty selection allowed)
repeat with eachItem in theDisks
eject disk eachItem
end repeat
end tell
You can combine clauses in the whose filter, so we're telling it to make a list of the names of every ejectable local volume and any network volumes, but leave out the startup disk (saves you filtering the system disk, regardless of name).
I've popped the "Choose" dialog using System Events so it'll work even if the currently active app isn't scriptable.
If you prefer to use a KM list widget, try passing just the names (one per line) back to KM and returning text with one chosen disk name per line which you can loop through in AS with each paragraph.
That looks nice and neat, but I tried it and it just runs indefinitely without anything visibly happening.
Do you know how to get a list of mountable drives and do the same thing in reverse? Presumably that would have to be a shell script, as Finder won't know about unmounted drives..?
Interesting -- runs fine here, ejecting both a mounted DMG and an USB stick at the same time. Files open on the disk, perhaps? Hidden processes like Spotlight can prevent a disk from ejecting, even in the Finder (which is where diskutil can win, and be more dangerous, with the force option).
I'm assuming you get the initial pick list!
I think that's going to depend on how you "removed" the disk in the first place. An AS eject makes the device ready for unplugging, so it's no longer visible to diskutil, whereas if you umount the volume it can be remounted by device number with eg diskutil mountDisk /dev/disk3.
So it's going to be a case of "what's your workflow, pick something that suits" (plus a "why are unmounting a physical volume without unplugging it anyway?" ).
tell application "Finder"
activate
set ejectableList to name of every disk whose startup is false and (ejectable is true or local volume is false)
set theDisks to (choose from list ejectableList with title "Choose to Eject:" with multiple selections allowed without empty selection allowed)
repeat with eachItem in theDisks
eject disk eachItem
end repeat
end tell
To be fair, I can think of some use cases -- for example, mounting a backup disk while backups are running and unmounting it after, for some protection against a ransomware attack (more useful for daily backups than Time Machine). But it that case you don't unplug so the device number remains the same and you can remount directly, without having to create a list.
I think it's some crazy Unix misdirection that wasn't thought of back in OS9!
"Not a local volume" is a very broad brush, so you'll probably want to narrow things down. So if you know you're going to be dealing with USB drives/sticks plus AFP and SMB network shares you could use:
set ejectableList to name of every disk whose startup is false and (ejectable is true or format is AppleShare format or format is SMB format)
Of course, if you know the names because you always use the same disks/volumes -- just use the names!