通过 Objective-C/Cocoa 清空垃圾箱
我想知道是否有一种方法可以以编程方式清空垃圾箱的内容。我目前正在使用以下命令删除位于此处的文件:
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:fileToDelete error:nil];
但是,在使用此操作后,每次将文件拖到垃圾箱时,系统都会提示以下消息:
您确定要删除吗 “xxxxxx.xxx”?该项目将被删除 立即地。您无法撤消此操作 行动。
这会持续到我注销或 sudo rm -rf 垃圾箱为止。
谢谢!
I was wondering if there is a way to programmatically empty the contents of the trash bin. I'm currently deleting files that are located there using:
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:fileToDelete error:nil];
However, after I use this operation, every time I drag a file to the trash, I am prompted with the message:
Are you sure you want to delete
“xxxxxx.xxx”?This item will be deleted
immediately. You can’t undo this
action.
This lasts until I either log out or sudo rm -rf the trash bin.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 AppleScript 来执行此操作:
You could try using AppleScript to do it:
您可以使用 NSWorkspace,但是删除垃圾对程序来说是一种禁忌,因此您将找不到 API。因此,最好的选择是使用 ScriptBridge。
将
ScriptingBridge.framework
添加到您的构建目标,并使用 Finder 生成头文件:然后您可以要求 Finder 提示用户清空垃圾箱:
请参阅 Scripting Bridge 文档 了解更多详细信息。
从 Xcode 7.3 开始,如果您尝试使用 Swift 执行此操作,您将在尝试查找 Finder.h 中定义的类时遇到链接器错误。所以你必须创建一个 Objective-C 包装器。
You can put stuff in the trash with NSWorkspace, however deleting the trash is kind of a no no for programs so you aren't going to find an API. So your best bet is using the ScriptBridge.
Add
ScriptingBridge.framework
to your build target, and generate a header file for Finder using:Then you can ask Finder to prompt the user to empty the trash:
See the Scripting Bridge documentation for more details.
As of Xcode 7.3, if you attempt this with Swift you will get linker errors trying to find classes defined in Finder.h. So you'll have to create an Objective-C wrapper.