How to empty the Recycle Bin?

Hello,
Can the Trash be emptied with Xojo command(s)?
In the meantime I tried with an AppleScript.

Shell.Execute "/usr/bin/osascript -e 'tell application ""Finder"" to empty trash'"

It works when the trash is full but when it is empty it displays an error message.
How to test if it is full or empty?
Thanks.

It is kind of dangerous to empty the trash, unless you make it clear to the user all files there will be erased, and ask for confirmation before proceeding.

The trash metaphor is meant as a way for users to have the possibility to recover files dragged there unintentionally. Or to provide a way to manage files destined to be deleted, with the possibility to get back on that decision.

The trash is meant to be managed by the user. Not by a program.

3 Likes

In addition, the script above will open the Finder if it’s not running (some users, for instance, use alternative apps and keep the Finder closed). Launching an app when the user doesn’t expect this (more specifically, just o empty the trash) is bad design.

You say, “The trash is meant to be managed by the user. Not by a program.”

Many programs, especially those that clean disks (CleanMyMac, Onyx, etc.) do this. And that’s part of the goal of my program.
But that doesn’t answer my question.

You realize this is a conversation, right ?

FWW with the latest macOS versions, this is not possible anymore (even not with a hack).
The Finder will always run.

I’ve just tried, in Script Editor:
tell application “Finder” to quit

And the Finder is no longer running.
Mac OS 12.4. Are you referring to the not-yet released Mac OS?

If you run Disk Utilities on your boot disk, the Finder is shut down too.

When the check is done, the Finder is fired back.
Monterey 12.4.

Far previous MacOS do not behave that way.

Some do when using the migration assistant, for instance.

I believe you (I never used it).

The script works perfectly and, in my program, it is the user who explicitly requests the execution of this function!
My question was can we do the same thing without AppleScript?

You can, of course, remove the items you find in the various trash folders. The trash folders are in your home folder and in every external volume (there can be exceptions, obviously, like a read-only volume won’t have such a folder created).
Trash folders are named “.trash”. You have to loop through each folder to make it empty (since you can’t delete non-empty folders) and then delete the folder. Recursion could work, but is limited to a depth (because of the app’s stack size), so you better have to use arrays to “simulate” recursion.
It’s doable, but takes time to implement.

I’ve already seen apps that trigger the Finder to complete something I asked them to perform. When I want the Finder to be kept closed, such behaviours are unwanted and I usually trash the app considering it’s badly written (and it’s not just me…).
Also Apple agrees with that, as the “Finder location” can point to arbitrary applications and alternative apps can be used instead (e.g. to reveal files).

1 Like