Hi all,
I use the FileManager trashIitem function to put a file in the trash. If it is only one file, then the option to put it back is available.
If, however, several files are deleted, the option to put it back is only available for the first deleted file. All others cannot be put back.
The problem has been known for at least 10 years. See Put back only works for the first file.
NSWorkspace recycle has the same problem.
It seems to be due to .DS_Store in the trash. The files that are in the trash are stored there. This may also lead you to believe that the trashItem function is working properly because the deleted files are still in the .DS_Store file.
If I call trashItem or recycle several times and wait 2 seconds between calls, then the option to put it back is available for all of them. That obviously can't be the solution. Waiting less than 2 seconds only offers to put the first file back.
So trashItem and recycle are the same as remove, with the difference that you can look at the files in the trash can again, but not put them back.
Are there other ways?
The Finder can also delete multiple files and put them all back.
That link includes a bug number (r. 23153124), which allowed me to look up the state of this. Yay!
My tests suggest that this isn’t as reproducible as you’ve described. Consider this test program:
import Foundation
func main() throws {
let f1 = URL(fileURLWithPath: "/Users/quinn/f1.txt")
try "file1".write(to: f1, atomically: true, encoding: .utf8)
let f2 = URL(fileURLWithPath: "/Users/quinn/f2.txt")
try "file2".write(to: f2, atomically: true, encoding: .utf8)
try FileManager.default.trashItem(at: f1, resultingItemURL: nil)
try FileManager.default.trashItem(at: f2, resultingItemURL: nil)
}
try main()
I ran it on my main Mac (15.2) and it didn’t reproduce the problem. I opened the trash and found that both f1.txt
and f2.txt
had File > Put Away enabled.
I deleted those files and tried again and this time the problem did reproduce. Weird. And it then continued to reproduce.
It took me a while but I think I know what’s going on. In the second test the Finder had its trash window open. Once I closed that window things started working again.
That’s not much of a workaround but it’s a useful titbit. I’ve added it to the bug report.
Unfortunately I don’t see a good workaround for this. I suspect that Finder and File Manager are bumping into each other when it comes to managing the .DS_Store
file )-: which is why things work when you add a delay. However, I don’t know this code well enough to say anything definitive.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"