以编程方式放回垃圾箱中的物品 [英] Put Back Items from Trash Programmatically

查看:121
本文介绍了以编程方式放回垃圾箱中的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式还原放入垃圾箱的物品?我希望在我的应用程序中有一个移至垃圾箱"按钮,但我希​​望能够在不保留自己的副本的情况下将其撤消.能够撤消垃圾桶是不适当的,还是应该永久进行?

Is it possible to restore items put in the trash programmatically? I would like to have a "Move to Trash" button in my app, but I would like to be able to undo it without keeping my own copy. Is it inappropriate to be able to undo moving to trash, or should it be a permanent operation?

推荐答案

我上次检查时,放回"功能完全是Finder特有的.也就是说,以编程方式将文件移至垃圾箱甚至连Finder都无法将其放回原处. Finder只能退回Finder丢弃的物品. (即使将项目从Dock上的文件夹拖到垃圾箱中,也不允许将其放回原处,因为Dock会将其扔到垃圾箱中,而不是Finder.)

Last time I checked, the Put Back feature was totally Finder-specific. That is, moving a file to the trash programmatically doesn't allow even the Finder to put it back. Only items trashed by the Finder can be put back by the Finder. (Even dragging an item from a folder on the Dock to the trash doesn't allow it to be put back, because the Dock trashed it, not the Finder.)

即使对于Finder可以放回的物品,也没有通过编程方式进行放回操作的书面记录.

Even for an item which the Finder can put back, there's no documented way to do the Put Back operation programmatically.

但是,对于您自己的应用程序,您当然可以撤消操作而无需保留文件的单独副本(直到用户清空垃圾箱). -[NSWorkspace recycleURLs:completionHandler:]将为您提供垃圾箱中项目的URL.存储这些URL的书签数据和原始的包含文件夹以及原始名称.要撤消操作,请解析垃圾箱和原始包含文件夹中项目的书签数据,然后将前者移至后者并使用原始名称-除非确保不要破坏在其位置创建的任何新文件.如果无法顺利完成迁移,请尝试在文件名中添加后缀编号.不断递增,直到找到未使用的名称.

However, for your own app, you can certainly undo the operation without keeping a separate copy of the file (until the user empties the trash). -[NSWorkspace recycleURLs:completionHandler:] will give you the URLs for the items in the trash. Store the bookmark data for those URLs and the original containing folder, as well as the original name. To undo, resolve the bookmark data for the item in the trash and the original containing folder and move the former to the latter with the original name – except be sure to not clobber any new file that's been created in its place. If the move can't be completed without clobbering, try adding a suffix number to the file name. Keep incrementing until you find an unused name.

如果解析书签数据失败,则说明操作无法完成,您应该让用户知道.

If resolving the bookmark data fails, then the operation couldn't be completed and you should let the user know.

这篇关于以编程方式放回垃圾箱中的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆