I am writing an app that will be deleting files. If the volume supports the Trash folder, I want to move the files there, otherwise just remove them.
It may just be my search mojo failing me, but I cannot find how to do this.
The intent here is to do something like (pseudocode):
if (itemURL on volume that supports Trash)
use trashItemAtURL:resultingItemURL:error:
else
use removeItemAtURL:error:
I am aware of recycleURLs:completionHandler: but, for SMB volumes for example, it puts up a dialog about how the file(s) will be immediately deleted. And I would like to deal the the files individually so that I can update a display. I suppose I could use recycleURLs:completionHandler: if File System Events work on SMB volumes but I am not sure they do.
Anyhoo..Is there some API I am missing or some recommended technique that I just cant seem to find?
A nice person from the Apple Dev Forums hunted up the answer:
From the 10.8 Foundation release notes:
NSFileManager Trash APIs In Mac OS 10.8, NSFileManager has new methods to manage the Trash. The -[NSFileManager trashItemAtURL:resultingItemURL:error:] will attempt to move the item at the given URL to the trash, returning the resulting URL by reference. As part of this operation, the system may rename the file to avoid name conflicts; if so, the resultingItemURL will reflect this new name.
[snip]
Some volumes may not support a Trash folder, so these methods will report failure by returning NO or nil and an NSError with NSFeatureUnsupportedError. NSFeatureUnsupportedError is a new error code in the NSCocoaErrorDomain that indicates a failure to perform the requested operation because the feature is not supported, either because the file system lacks the feature, or required libraries are missing, or other similar reasons.
Not sure why that doc never came up on search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With