通过 Objective-C/Cocoa 清空垃圾箱

发布于 2024-10-24 01:00:00 字数 378 浏览 2 评论 0原文

我想知道是否有一种方法可以以编程方式清空垃圾箱的内容。我目前正在使用以下命令删除位于此处的文件:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2024-10-31 01:00:00

您可以尝试使用 AppleScript 来执行此操作:

NSString* appleScriptString = @"tell application \"Finder\"\n"
                              @"if length of (items in the trash as string) is 0 then return\n"
                              @"empty trash\n"
                              @"repeat until (count items of trash) = 0\n"
                              @"delay 1\n"
                              @"end repeat\n"
                              @"end tell";
NSAppleScript* emptyTrashScript = [[NSAppleScript alloc] initWithSource:appleScriptString];

[emptyTrashScript executeAndReturnError:nil];
[emptyTrashScript release];

You could try using AppleScript to do it:

NSString* appleScriptString = @"tell application \"Finder\"\n"
                              @"if length of (items in the trash as string) is 0 then return\n"
                              @"empty trash\n"
                              @"repeat until (count items of trash) = 0\n"
                              @"delay 1\n"
                              @"end repeat\n"
                              @"end tell";
NSAppleScript* emptyTrashScript = [[NSAppleScript alloc] initWithSource:appleScriptString];

[emptyTrashScript executeAndReturnError:nil];
[emptyTrashScript release];
饮惑 2024-10-31 01:00:00

您可以使用 NSWorkspace,但是删除垃圾对程序来说是一种禁忌,因此您将找不到 API。因此,最好的选择是使用 ScriptBridge。

ScriptingBridge.framework 添加到您的构建目标,并使用 Finder 生成头文件:

sdef /System/Library/CoreServices/Finder.app/ | sdp -fh --basename Finder

然后您可以要求 Finder 提示用户清空垃圾箱:

#import "Finder.h"

FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.Finder"];

// activate finder
[finder activate];

// wait a moment (activate is not instant), then present alert message
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  [finder emptySecurity:security];
});

请参阅 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:

sdef /System/Library/CoreServices/Finder.app/ | sdp -fh --basename Finder

Then you can ask Finder to prompt the user to empty the trash:

#import "Finder.h"

FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.Finder"];

// activate finder
[finder activate];

// wait a moment (activate is not instant), then present alert message
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  [finder emptySecurity:security];
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文