I transfer a lot of data to my Mac using SD cards from video and audio equipment. The SD cards are mounted as disks (or volumes, whatever you want to call them) and displayed on the Desktop. After uploading, I needed a way to empty the trash on the individual SD cards without emptying the trash on all disks so I used Script Editor to create the following app and saved it to the desktop as an app. This was created years ago and worked perfectly.
on open dropped_item
tell application "Finder"
-- get path to drive.
set drivepath to POSIX path of dropped_item
end tell
if drivepath is "/" then
display dialog "not allowed on the boot volume. Exiting"
else
set trashpath to drivepath & ".Trashes"
set confirm to display dialog ¬
"Empty the trash on " & drivepath & ¬
"?" buttons {"No", "Yes"} default button "No"
if button returned of confirm is "Yes" then
do shell script "rm -rf " & trashpath
end if
end if
end open
I upgraded to Ventura a few months ago and the app appeared to work because there were no error messages, but I recently discovered that it was not deleting the trash. I gave the app, Script Editor, and Terminal Full Disk Access permission and this did not help. In fact, when I ran the app, the system revoked Full Disk Access for the app!!
When I tried some diagnostic edits and saved them, I started getting error messages (described below). It seems that scripts compiled by Monterey did not check for this error.
I stripped down the script to the following which demonstrates the root of the problem:
The following line is entered into Script Editor, compiled and saved as an app:
do shell script "rm -rf "/Volumes/SD_CARD_NAME/.Trashes"
Full Disk Access permission is given to Script Editor, the app, and Terminal.
If I run the script from Script Editor, .Trashes
is removed
If I enter rm -rf /Volumes/SD_CARD_NAME/.Trashes
in the Terminal command line, .Trashes
is removed.
If I run the app by clicking on the icon for the app, .Trashes
is not removed and I get the following error:
rm:/Volumes/SD_CARD_NAME/.Trashes: Operation not permitted (1)
and Full Disk Access for the app is disabled.
I tried several ways to solve this problem including the following:
tell application "Finder" delete folder "/Volumes/SD_CARD_NAME/.Trashes"
tell application "System Events" delete folder "/Volumes/SD_CARD_NAME/.Trashes"
And creating a shell script, saving it as an executable in ~/bin
, giving it Full Disk Access permission, and calling that from the AppleScript. In this case, FDA was revoked for the app but not for the shell script in ~/bin
.
None of these solved the problem.
I consider this a bug on several counts:
- Apple should not make it impossible to empty trash on a single volume. What horrible thing is going to happen if I remove trash on an SD card?
- The behavior is inconsistent between running the app from Script Editor and running it by clicking on it.
- The system should not revoke FDA without at least displaying an error message that tells me why and what I need to do to prevent the revoking.
I have filed a bug report, not that I expect Apple to fix the problem.
There have been several suggestions in the comments (thank you everyone who tried to help), but I've chosen a simple work around which is described in my "answer".
thanks again, to all.
Note: 2 October 2023
I just upgraded to Sonoma and had to change the script. If I delete .Trashes
, the next time I try to drag a file into the trash, it says that the file will be permanently deleted. So I changed the script to get the UID and delete .Trashes/[UID]/*
as shown below. (note there are many ways to write this script, mine is almost certainly not the best—script suggestions gratefully accepted)
#!/bin/csh -f
set X = `id | sed -r 's/uid=([0-9]*).*/\1/g'`
rm -rf .Trashes/$X/*
Note that if .Trashes/[UID]
is empty you will get a harmless error message that rm could not find any files.