gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:59:49 26 4
gpt4 key购买 nike

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

    NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:fileToDelete error:nil];

但是,在我使用这个操作之后,每次我将一个文件拖到垃圾桶时,都会提示信息:

Are you sure you want to delete “xxxxxx.xxx”?This item will be deleted immediately. You can’t undo this action.

这一直持续到我注销或 sudo rm -rf 垃圾桶。

谢谢!

最佳答案

您可以尝试使用 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];

关于objective-c - 通过 Objective-C/Cocoa 清空垃圾桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5334445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com