Is there a 'unmount device' macro?

Hello,
would it be possible to create a macro which would list the attached USB external drives (or just list the devices) and allow me to select a device and unmount that device. I find that it gets a bit tedious to always have to go back to Disk Utility or Finder and go through the unmounting process.
thanks very much

Hi Ronald

You can find inspiration from this post:

2 Likes

This script should do exactly that (well, it lists not only USB disks):

tell application "Finder" to set diskList to name of every disk whose ejectable is true
tell application (path to frontmost application as text) to set diskToEject to choose from list diskList with title "Eject Disk" with prompt "Select disk(s) to eject:" with multiple selections allowed
if diskToEject is not false then tell application "Finder" to eject diskToEject

Hold down ⌘ to select multiple disks.

Here as KM macro:

List and Eject Disks.kmmacros (2.0 KB)

3 Likes

thank you VERY MUCH !

I often get into device ejection problems (probably spotlight searching the disk) and the OS asks if I want to ‘force eject’ at the risk of damaging the external drive or USB key. Would you know if it is OK to force eject, and do I have to repeat the same procedure every time (make drive private in spotlight pref, etc).
I wonder if this risk of damage by force ejection is somewhat of a myth (unless the drive is being actively used by a backup for example)
thanks again very much.

As far as I know force-ejecting will not damage a drive, and the worst thing that will happen is data or file system corruption.

Here are a couple of tips/ideas how to deal with Spotlight in such cases, and how to determine if there are other processes preventing you from unmounting the disk.

1 Like

I made the script a bit nicer:

  • It won’t push the Finder to the foreground.
  • The list window has a proper title and text.

Just download it again to get the new version.


BTW: If you choose “USB Device Key Trigger” from KM’s trigger selection menu you can assign the Media Eject key as trigger, if you have no other use for it:

2 Likes

thank you.
Ideally, shouldn’t the eject command be preceeded by a turn off spotlight command, and then a turn back on?
The cannot eject problem is a real pain and is common.
thanks again

It’s the operating system that should take care of Spotlight when unmounting a disk (and usually it does). But, of course, you can add any command to the script. But before doing that I would make sure that everything is OK. I think it’s not normal if you get the “cannot eject problem” very frequently:

Maybe the Spotlight index is corrupted? Maybe the disk is physically damaged? Problem with the disk controller? USB hub problem? Is the file system catalog on the disk corrupted? Maybe it’s not Spotlight, but another process?, etc.

The cannot eject problem is a real pain and is common.

Well, I see that problem rarely, and then in most cases another process was the culprit.

1 Like

thank you
good point. I think that the problem is the anti virus Bitdefender. Somehow, according to the documentation, the scanning process is linked to spotlight

Perhaps you can exclude that device from automatic scanning. You’d have to decide if it was worth it to you, of course.

1 Like

the problem is that everytime you eject a device, the exclusion configuration is excluded, even if you install another deivce with the same name.
thanks for your answer

1 Like

Hello Tom,
The macro works very well.
When I run the macro, I have to click inside the dialog box and pick the device i want to eject (99% of the time only one device).
Is there any way to end the macro by putting the cursor inside the box, so that I can use the up down arrows to pick the device to eject and press Enter?
thanks again very much

OK, try this:

List and Eject Disks [ver 2.1].kmmacros (2.3 KB)

It will now remember the previously ejected disk and preselect it in the dialog. If it’s the desired disk you can press OK, if not you can navigate with the arrows.
If the previously ejected disk is not available, it will select the first disk in the list.

tell application "Keyboard Maestro Engine" to set diskToEject to getvariable "previousDisk"
tell application "Finder" to set diskList to name of every disk whose ejectable is true
if diskList does not contain diskToEject then set diskToEject to first item of diskList
tell application (path to frontmost application as text) to set diskToEject to choose from list diskList with title "Eject Disk" with prompt "Select disk(s) to eject:" default items diskToEject with multiple selections allowed
if diskToEject is not false then
   tell application "Finder" to eject diskToEject
   tell application "Keyboard Maestro Engine" to setvariable "previousDisk" to diskToEject
end if
4 Likes

fantastic !! thank you very much !

tiny glitch: at the end, the following box is displayed

Probably it has nothing to do with the script. Seems that you have installed a Scripting Addition that is not compatible.

If the script runs fine, then set the option of the Execute AppleScript action to “ignore results”. (However you won’t see any error messages then.) Better: Update the incompatible Scripting Addition, or remove it from the path indicated in the message.

1 Like

thank you for your reply.

I don’t have the script addition app in question (24U Appearance OSAX).

Do you suggest I purchase it?

Could it perhaps be Bartender 2 which I purchased a month ago?

https://www.macbartender.com/

I once got an error message containing the word OSAX

------- Console Output -------

Sep 14, 2016, 13:38:16 Bartender 2: SecStatucCodeCreateWithPath() error for file:///Library/PrivilegedHelperTools/com.surteesstudios.Bartender.BartenderInstallHelper: -67068
Sep 14, 2016, 13:38:28 kernel: Sandbox: ditto(1949) System Policy: deny(1) file-write-create /System/Library/ScriptingAdditions/BartenderSystemHelper.osax

Can’t suggest to purchase it, because I never heard of that Scripting Addition before :wink:

Have you looked into the indicated folder?
~/Library/ScriptingAdditions/

(Your message clearly says that there is something.)

Maybe you have installed a demo version long time ago.

Could it perhaps be Bartender 2 which I purchased a month ago?

I don’t think that it is related.

1 Like

you are incredible !

I found the folder below as you had predicted

what should I do ? thanks !

First: I predicted nothing. The message window you have posted before has “predicted” it :wink:

Second: You are in the wrong folder. You are in…

/Library/ScriptingAdditions/

This is the global Scripting Additions folder, which is not the one you need.

Look again at the message: it tells you the correct folder:

/Users/jlette111/Library/ScriptingAdditions/

This is the User’s Scripting Additions folder und you’ll find the offending “24U Appearance OSAX.osax” there.

To open that folder…

  1. copy the path from here /Users/jlette111/Library/ScriptingAdditions/
  2. go to the Finder
  3. press ⇧⌘G
  4. paste the path into the text field of the sheet
  5. click OK.

…and then…

what should I do ?

…delete the “24U Appearance OSAX.osax” if you don’t need it. (Or try to get an update if you want to keep it.)

1 Like