How to know whether a file can be moved to the trash in Cocoa
Asked Answered
P

1

9

Currently I'm able to trash selected user files. The question is how to know whether the volume of that file supports trashing. For example, external USB sticks just have a local trash folder, while trashing a file on a remote Mac is not allowed (even from the Finder), it can only be deleted. I didn't find any appropriate constant among the NSURLVolume[...] ones to use with [NSURL getResourceValue:forKey:error].

Prayerful answered 23/12, 2015 at 10:35 Comment(8)
which way you trashing files now?Winchell
Sorry, what? It doesn't matter how files are trashed, they simply can't.Prayerful
Well, if you consider that local files can be moved and other files do not, then you just need to analyze the file path; in case the file path starts with '/Volumes/', then it isn't in the system disk.Achaea
@vitormm Local partitions alongside Macintosh HD which support trashing also reside in the Volumes folder.Prayerful
Uhh, true. Well, another option would be to identify the names of the disk0 (Mac hard disk) partitions and, in case the path is inside /Volumes/, only accept the ones that belong to these guys. I already have a code that does that, in case that solves your problem.Achaea
I've removed my answer since it doesn't help answering the question and makes it easier to find in case someone is looking for unanswered questions.Achaea
This can be accomplished by using the NSFileManager and trashItemAtURL:resultingItemURL:error: see #18480022Saari
With this method you trash the file directly, but my question is about how to know if a file can be trashed, for example when showing a menu item that would read "Trash file" if trashing is supported or "Delete file" otherwise.Prayerful
P
1

I ended up checking the result of the following method:

FileManager.default.url(for: .trashDirectory, in: .userDomainMask, appropriateFor: url, create: false)

where url is the url of the volume or a file on that volume.

Prayerful answered 12/12, 2023 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.