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!
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.frameworkto your build target, and generate a header file for Finder using:Then you can ask Finder to prompt the user to empty the trash:
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.