Put file into Trash

In my app I have a list of files, and the ability to delete one or more, for those people who dont know how to use Finder or Explorer.

But folderitem.delete is immediately destructive, and there will always be the ones who click 'delete’, answer ‘Yes I really mean it’, and still want the file back later when they realise it was a mistake.

Is there a reliable way to put a file into the trash so that it can be recovered by OSX or Windows?

Move it to SpecialFolder.Trash?

You need declares to do it correctly. Messing with SpecialFolder.Trash and ~/.Trash can lead to problems.

Item.MoveFileTo SpecialFolder.Trash is the usual answer. It’s not clear whether it follows the same volume as the original item, but no one ever answered that after various users asked. It’s still the most common answer.

With the MBS plugin, you have MacFileOperationMBS.MoveToTrash.

An answer from 10 years ago (I hope it is still valid):

2 Likes

Can you describe the problems SpecialFolder.Trash can lead to as I don’t think our app has ever had a problem with it.

Not off the top of my head, but there were problems years ago when I was building Plugins Pro. Generally, when there’s an API to do things and you choose the hacky way, you’re going to see problems at some point.

Yes, should still work.

There is also a trashItem method on NSFileManagerMBS class.

1 Like

Here’s the API call for it, Xojo implementation left to someone smarter than myself:

Someone already did the declares for it

IIRC the Trash folder is locked for 3rd Party apps by default ( including the Terminal ) on recent versions of the macOS.

Last time I investigated this, it was actually preferable to use AppleScript over the API. The main reason is that using AppleScript to make Finder do it, enabled “Put Back”, allowing the customer to restore the file back from the trash, to where it was before.

That might have changed now.

I also vaguely recall something about you can use dragitems, so the customer can drag the file from your application into the trash and this bit gets fuzzy, but I seem to recall that the OS would then move to the file to trash for you.

1 Like

Generally speaking, you shouldn’t rely on the Finder being available to perform these functions, unless of course your application is specifically designed to only work if the Finder is running. For the general case of moving an item to the trash, use the API.

It’s also poor form to do an operation in the background that disrupts the user’s expectation of what Undo will… undo.

Typically I would agree with you. But the API behaves differently than the Finder, and has caused customers to ask “Why can’t use Put Back when I delete the file from your app”.

It is entirely up to you which you choose and why you choose, I am merely offering advice from experience. Don’t worry, I won’t get offended if you don’t use Finder to delete files.

That’s what I meant when I mentioned that your app might be specifically designed to integrate with the Finder. Other apps won’t.

It wasn’t.

I just re-tested our app on macOS 11.7.4 and macOS 13.2.1 and it can successfully move folders into the trash folder using SpecialFolder.Trash.

Make sure you try that technique on a file that’s not on the main drive. IIRC SpecialFolder.Trash is relative to the users folder and MoveTo will not work between drives.

This could be where i’m lucky as the folders the app is responsible for will always be inside the User’s Home folder.