Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Based on a great find by user950473 I've implemented his/her discovery as a method; thought I'd share the code in case it's helpful.</p> <pre><code>- (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:&amp;errorInfo]; // Check errorInfo if (! eventResult) { // Describe common errors *errorDescription = nil; if ([errorInfo valueForKey:NSAppleScriptErrorNumber]) { NSNumber * errorNumber = (NSNumber *)[errorInfo valueForKey:NSAppleScriptErrorNumber]; if ([errorNumber intValue] == -128) *errorDescription = @"The administrator password is required to do this."; } // Set error message from provided message if (*errorDescription == nil) { if ([errorInfo valueForKey:NSAppleScriptErrorMessage]) *errorDescription = (NSString *)[errorInfo valueForKey:NSAppleScriptErrorMessage]; } return NO; } else { // Set output to the AppleScript's output *output = [eventResult stringValue]; return YES; } } </code></pre> <p>Usage example:</p> <pre><code> NSString * output = nil; NSString * processErrorDescription = nil; BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id" withArguments:[NSArray arrayWithObjects:@"-un", nil] output:&amp;output errorDescription:&amp;processErrorDescription]; if (!success) // Process failed to run { // ...look at errorDescription } else { // ...process output } </code></pre> <p>It's very slightly hacky, but IMHO is a satisfactory solution. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload