user1445205
user1445205

Reputation:

Perform commands in Mac application sandbox?

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?

Upvotes: 1

Views: 928

Answers (2)

Paul R
Paul R

Reputation: 212949

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

Upvotes: 2

usain
usain

Reputation: 756

Sudo commands are not allowed in sandbox.

Upvotes: 3

Related Questions