send2trash
send2trash copied to clipboard
after deleted by send2trash there is no "put back" or "restore" in the right click menu in trash on OSX
after deleted by send2trash command there is no "put back" or "restore" menu in the right click menu in trash can on OSX (10.10.x), but the files which were deleted by Command + Backspace i has that menu. By the way it works on win7. Thanks for your works. :+1:
This used to work in older OS X versions. I think that the API changed and that the old API, used in send2trash
doesn't work very well anymore.
See this Sublime Text issue for reference: https://github.com/SublimeTextIssues/Core/issues/2226
Possible solution may be implementing something like https://github.com/ali-rantakari/trash/blob/master/trash.m#L265-L340 in send2trash using ctypes.
According to https://github.com/ali-rantakari/trash#the-put-back-feature the only way to enable "Put Back" is to instead tell finder to put the file in the trash.
Going through finder is horribly slow. I'm not convinced that it's the only way. The "Put Back" feature used to work with send2trash and I'm sure that other native Cocoa/Swift apps are able to send files to trash without needing to ask Finder. We just need to call the API in the same way.
Hum, now that I look a bit more at https://github.com/ali-rantakari/trash, maybe I'm wrong... Maybe that going through finder is the only way.
My knowledge of python's ctypes isn't the best, but IIUC they don't provide a nice interface into Objective-C, so replicating the method in https://github.com/ali-rantakari/trash/blob/master/trash.m#L265-L340 might be rather difficult (at least, this SO answer makes it look cumbersome).
What do you think are our potential options for adding this functionality in send2trash?
Everything that comes to my mind (leveraging things like pyobjc, using a precompiled binary, etc) seems a little overkill just to have this feature... I may be missing something though, or it may be a lot easier than I think.
Yes, that would be complicated. One alternative is to add an optional compiled module. If you look at git history (v1.x tags), you'll see that the OS X code used to be straight objc code. I went to ctypes so that the library could be installed without needing a compiler. But by adding an optional compiled module, we could provide the feature to users who bother to compile the module.
I'm gonna try this using ctypes anyway, just because it's a huge benefit to not have to compile in order have an extra feature. If it ends up being too complicated/too much of a mess, then I'll give it a crack using obj-c instead.
@hsoft in your code you have:
class FSRef(Structure):
_fields_ = [('hidden', c_char * 80)]
I need to do the same for AppleEvent
and AEDesc
. Any pointers on how I can make structures for those? I don't know where you got those values from, and I'm unsure of the actual process to do it. 😅
For example, I need to make an AppleEvent
variable for this:
AppleEvent replyEvent;
OSStatus sendErr = AESendMessage([descriptor aeDesc], &replyEvent, kAEWaitReply, kAEDefaultTimeout);
@acheronfail sorry, I don't have much of a clue. The only time I ever used ctypes was for this library and I kept it simple. And it was a long time ago.
AppleEvent seems to be an alias of alias of AEDesc.
By quickly reading the doc of the init()
method, something more elaborate of this could be a starting point:
class AppleEvent(Structure):
_fields_ = [('descriptorType', DescType), ('dataHandle', AEDataStorage)]
Will need to find out and declare DescType
and AEDataStorage
.
I think our options are:
- Include manually compiled module
- ✅ easy to maintain
- ❌ requires compilation to use the module
- Implement using
ctypes
- ✅ no need to compile to use feature
- ❌ messy, difficult to debug/maintain
- Optionally depend on
PyObjC
- ✅ easier to use than
ctypes
- ❌ requires a dependency
- ✅ easier to use than
Something similar happens on Ubuntu 16.04, when trying to restore I get:
Could not determine original location of “01.jpg
The item cannot be restored from trash
Also, the "restore" option is missing from the context menu.
I am wondering if this bug ever got fixed? The issue still persists today. What was the suspicion to be wrong with it?
I had looked into alternative ways to see what might be possible on Mac OS, none of the options I tried allowed the "put back" when in Finder.
I actually found a way to do it outside of the library which works quite well. It requires the user to click ok for security preferences otherwise it works great :)
cmd = ['osascript', '-e', 'tell app "Finder" to move {' + ', '.join(files) + '} to trash']
subprocess.run(cmd, stdout=open(os.devnull, 'w+'))