1

I need my application to perform a command while in the Sandbox. This is the code I have so far:

// Set up the task
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/bash"];
NSArray *args = [NSArray arrayWithObjects:@"-l",
                 @"-c",
                 @"rm -rf .Trash/*",
                 nil];
[task setArguments: args];

// Set the output pipe.
NSPipe *outPipe = [[NSPipe alloc] init];
[task setStandardOutput:outPipe];
[task launch];

I get a log output of:

/bin/bash: /etc/profile: Operation not permitted

Any ideas?

2 Answers2

3

Sudo commands are not allowed in sandbox.

usain
  • 746
  • 5
  • 16
2

If you just want to empty the trash then rather than trying to do it via brute force just let the Finder take care of it, e.g. using AppleScript:

tell application "Finder"
    empty trash
end tell

You can run AppleScript commands directly from Objective-C like this:

NSAppleScript *command = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to empty trash"];
[command executeAndReturnError:nil];
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Nice idea but the AppleScript did not work. I got this log output:`NSAppleScriptErrorAppName = Finder; NSAppleScriptErrorBriefMessage = "Application isn\U2019t running."; NSAppleScriptErrorMessage = "Finder got an error: Application isn\U2019t running."; NSAppleScriptErrorNumber = "-600"; NSAppleScriptErrorRange = "NSRange: {29, 11}";` –  May 20 '13 at 17:03
  • Odd - I tried it here first and it ran fine, but that was just in AppleScript Editor. – Paul R May 20 '13 at 21:27
  • I think that the sandbox is the problem. Any ideas? –  May 21 '13 at 06:39
  • 1
    I got it working! I just needed to add a temporary entitlement. Thanks! –  May 21 '13 at 06:49
  • 1
    @AlexanderMacLeod, how did you do that? – Suran Sep 22 '13 at 10:40