Empty waste bin through Objective-C / Cocoa

I was wondering if there is a way to programmatically empty the contents of the garbage bin. I am currently deleting files that are there using:

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

However, after I use this operation, every time I drag the file to the trash, I get a message:

Are you sure you want to delete "xxxxxx.xxx"? This item will be deleted immediately. You cannot undo this action.

This continues until I log out or delete the rm -rf trash can.

Thank!

+3
source share
2 answers

NSWorkspace, - no , t, 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];
});

. .


Xcode 7.3, Swift, , , Finder.h. Objective-C.

+4

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];
+4

All Articles