0

I wrote a simple AppleScript to eject external disk (simply by "tell"ing Finder to do "eject") and it works as expected in simple cases.

However, when some Finder window is opened with a location with path inside the disk (to be ejected), then it blocks me from ejecting.

It shows an alert dialog with the following:

The disk "MyDiskName" couldn't be ejected because the Finder is using it.

Stop the Finder action and then try to eject the disk again.

First of all, I'd love to know why "loose" uses of disk are considered so seriously and block the same as with explicit uses, via scripting? To say, it's different when we "use" a disk by reading/writing files or running a shared/locked file in it, against simple Finder with folder in that disk. For example, when you try to eject disk manually (via GUI and mouse clicks) it will block you if the disk is "really" in use, but if it's only being "used" with an unrisky Finder file-listing, then it will allow you to eject without any warning, and will close that Finder window automatically without even asking.

Secondly, how can I safely bypass this limitation, by detecting a "loose" using of the disk, terminate it and then proceed with the ejecting as intended?

To be clear, I consider as "loose use", any operation that even the GUI of MacOS does not warn before breaking it. It will warn when a file is still opened and in use, or some file copying is running in the background, but will not warn before ejecting if we simply have some Finder windows open. It will also won't warn alert if some file is opened but not "locking" the disk (for example opening some image in the disk with the browser, so it "forgets" the origin after showing it).

P.S. By the way, I also saw that even if a Finder window show list of files inside a subfolder of an external-disk - if the disk is sleeping/offline (yet still connected and available as mounted, after some minutes without use, probably cached somehow) - then the ejection is working well. Why?

P.S. 2 my script is as follows:

set diskName to "MyDiskName"
tell application "Finder"
 if disk diskName exists then
  eject disk diskName
 end if
end tell

2 Answers 2

0

Finder is working correctly

I'd love to know why "loose" uses of disk are considered so seriously and block the same as with explicit uses, via scripting?

“Loose use of the disk” is a highly subjective term; computers don’t work this way as it has no idea what you intend to do since you happen to be in that directory. Finder has that directory mounted and accessed therefore it cannot be ejected.

Secondly, how can I safely bypass this limitation, by detecting a "loose" using of the disk, terminate it and then proceed with the ejecting as intended?

Close the Finder window of that directory. However, since Finder has no definition for your term “loose” you’ll need to define it, then measure it. Once you do that (i.e. check for open files), you can then close them, close thenFinder window(s) and eject.

You could also force unmount or eject the device, but that would require admin privileges (manually authenticate) thus negating this whole automation exercise.

2
  • When I click on the item of a disk in the Finder sidebar, its contents immediately appear in that Finder window. In the past, I could click on the eject item and the disk would dismount and the window would close. But since Ventura, that happens sometimes, and other times MacOS complains that it "can't be ejected because Finder is using it." And I have one disk for which nothing happens when I try to eject (stays mounted and no diagnostic message).
    – WGroleau
    Mar 31, 2023 at 3:27
  • I defined it, please read
    – Cocktail
    Apr 3, 2023 at 10:16
0

95% of the time, this following AppleScript code works for me.

do shell script "killall -KILL Finder"

After Finder quits, it should automatically re-open. After that, you should be able to successfully eject your tests.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .