Objective-C/Cocoaを介した空のゴミ箱
-
26-10-2019 - |
質問
ゴミ箱の内容をプログラム的に空にする方法があるかどうか疑問に思っていました。現在、以下を使用してそこにあるファイルを削除しています。
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:fileToDelete error:nil];
ただし、この操作を使用した後、ファイルをゴミ箱にドラッグするたびに、メッセージが表示されます。
「xxxxxx.xxx」を削除する必要がありますか?このアイテムはすぐに削除されます。このアクションを元に戻すことはできません。
これは、私がログアウトするか、ゴミ箱にrm -rfをログアウトするまで続きます。
ありがとう!
解決
ごみに物を入れることができます nsworkspace, ただし、ゴミを削除することは、プログラムにとってノーノーのようなものであるため、APIを見つけることはできません。したがって、最善の策はScriptbridgeを使用することです。
追加 ScriptingBridge.framework
ビルドターゲットに、次を使用してファインダー用のヘッダーファイルを生成します。
sdef /System/Library/CoreServices/Finder.app/ | sdp -fh --basename 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];
});
を参照してください スクリプトブリッジのドキュメント 詳細については。
Xcode 7.3の時点で、Swiftでこれを試みると、Finder.hで定義されたクラスを見つけようとするリンカーエラーが表示されます。したがって、Objective-Cラッパーを作成する必要があります。
他のヒント
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];
所属していません StackOverflow