If you are looking for an easier solution, I wrote this general implementation, which should achieve what you want:
- (BOOL) runProcessAsAdministrator:(NSString*)scriptPath withArguments:(NSArray *)arguments output:(NSString **)output errorDescription:(NSString **)errorDescription { NSString * allArgs = [arguments componentsJoinedByString:@" "]; NSString * fullScript = [NSString stringWithFormat:@"'%@' %@", scriptPath, allArgs]; NSDictionary *errorInfo = [NSDictionary new]; NSString *script = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript]; NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:script]; NSAppleEventDescriptor * eventResult = [appleScript executeAndReturnError:&errorInfo];
Usage example:
NSString * output = nil; NSString * processErrorDescription = nil; BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id" withArguments:[NSArray arrayWithObjects:@"-un", nil] output:&output errorDescription:&processErrorDescription]; if (!success)
This is very slightly hacked, but IMHO is a satisfactory solution. Thanks for opening this SO question .
Carlos P
source share