trash-rs: Can't implement `list`, `purge_all`, and `restore_all` on MacOS
It seems that there’s no publicly available API on MacOS that is needed to list the contents of the trash and to permanently remove or restore items from the trash. These would be needed for list, purge_all, and restore_all accordingly, which are the features planned for version 2. These all may be possible to implement by parsing and editing the DS_Store file but as its structure is proprietary, I wouldn’t want to take that approach.
All the three aforementioned functions and thereby all the added functionality of version 2 is currently implemented for both Windows and Linux. To allow users to take advantage of the new functionality on those platforms, version 2 will be released but it won’t provide those functions on MacOS.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 31 (13 by maintainers)
Since osascript is already used, I think this might be promising: applescript-finder-guide.pdf (see PG91: Trash-Object)
I don’t have a mac right now, so I can’t play around with it, but hopefully I can set up a VM or something along those lines.
Oh I didn’t try that yet. I think there might be a way with
mdls -name kMDItemWhereFromsbut haven’t tested it. I’ll let you know if I find anything.With all the information collected here and the references to the
trashproject, it appears like it’s possible to take a stab at thelistandpurgefunctionality even though it is still unclear how to doPut Back. All I could gather was that it stores put-back information in in.DS_Storefiles in a racy fashion, but actually putting a file back would require writing these metadata files which seems like a can of worms.From what I recall, the
macos.rsfile already contains examples for both AppleScript and for using theNsFileManagerclass, which should help. To me it seems fine to use apple script especially for a proof-of-concept, which can eventually be migrated to using theNsFileManagerif there is benefit and demand.See https://github.com/ali-rantakari/trash for a C version of using Finder to trash the file which provides the Put Back Information
Thanks for the analysis and the writeup, it’s much appreciated!
I wonder if there are ways to mimic what the OS is already able to do and get a working implementation that way. After all, the
.DS_Storefile seems to be broken enough to rather not use it.Mimicking the OS could work if it solely relies on files on disk that could be read, maybe easily, maybe with some reverse engineering. If MacOS doesn’t really seem to care about
.DS_Store, why should we? That could be option 4.I would take the number 1 option from that issue. Put Back using Finder has been reliable for me and I have not seen any reports of issue on the web.
One other point is that trash using AppleScript/AppleEvents does not work if you are not the user with the current desktop.
e.g. if you ssh into a mac you don’t have a finder and so the workarounds don’t work.
The fix is for Apple to fix their API - but that has been open as a bug for years.
This reminds me of a major shortcoming of the examples so far: They don’t seem to know where to put files back to. Shouldn’t they derive the correct put-back location from information in the trash item itself?
Edit: It does turn out that the current implementation is already using apple-script in one code-path.
A lot of stack overflow answers with AppleScript involve windows popping up, but I think that’s just because it’s easy to not look up the API and instead just use the GUI buttons. The script above doesn’t need any windows, and just as a sanity check, I tested it with closing all the finder windows and with having some finder windows open to make sure it didn’t modify them, both worked fine.
I’m sure an Objective-C API would be much more runtime efficient, but I’m actually skeptical that there is an API for restoring files from the trash. I believe Finder is the only one with that capability, so I think regardless AppleScript is going to be required to talk to Finder and ask it to restore files.
It’s a lot easier to test the AppleScript so at least for a 1.0 it might be nice to use it.
Agreed. How to even begin to reverse engineer Put Back is beyond me. Despite that I’ll give it a go but if anyone knows at least where to begin that’d be nice and I’ll help however I can.