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

min*_*pop 4 macos cocoa recycle-bin objective-c nsfilemanager

我想知道是否有办法以编程方式清空垃圾桶的内容.我目前正在删除位于那里的文件:

    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:fileToDelete error:nil];
Run Code Online (Sandbox Code Playgroud)

但是,在我使用此操作后,每次将文件拖到回收站时,系统都会提示我:

您确定要删除"xxxxxx.xxx"吗?此项目将立即删除.您无法撤消此操作.

这一直持续到我退出或sudo rm -rf垃圾桶.

谢谢!

Dav*_*vid 5

您可以尝试使用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];
Run Code Online (Sandbox Code Playgroud)