send2trash icon indicating copy to clipboard operation
send2trash copied to clipboard

after deleted by send2trash there is no "put back" or "restore" in the right click menu in trash on OSX

Open MxJ24 opened this issue 8 years ago • 15 comments

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:

MxJ24 avatar Jun 02 '16 13:06 MxJ24

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.

ghost avatar Jun 02 '16 17:06 ghost

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.

acheronfail avatar Apr 06 '18 22:04 acheronfail

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.

ghost avatar Apr 06 '18 23:04 ghost

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.

ghost avatar Apr 06 '18 23:04 ghost

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.

acheronfail avatar Apr 11 '18 12:04 acheronfail

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.

ghost avatar Apr 11 '18 21:04 ghost

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.

acheronfail avatar Apr 12 '18 08:04 acheronfail

@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 avatar Apr 12 '18 10:04 acheronfail

@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.

ghost avatar Apr 12 '18 10:04 ghost

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.

BoboTiG avatar Apr 12 '18 11:04 BoboTiG

I think our options are:

  1. Include manually compiled module
    • ✅ easy to maintain
    • ❌ requires compilation to use the module
  2. Implement using ctypes
    • ✅ no need to compile to use feature
    • ❌ messy, difficult to debug/maintain
  3. Optionally depend on PyObjC
    • ✅ easier to use than ctypes
    • ❌ requires a dependency

acheronfail avatar Apr 12 '18 21:04 acheronfail

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.

petarnikolovski avatar May 22 '18 18:05 petarnikolovski

I am wondering if this bug ever got fixed? The issue still persists today. What was the suspicion to be wrong with it?

ben-hearn-sb avatar Dec 27 '21 13:12 ben-hearn-sb

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.

arsenetar avatar Jan 26 '22 02:01 arsenetar

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+'))

ben-hearn-sb avatar Jan 26 '22 08:01 ben-hearn-sb