zoukankan      html  css  js  c++  java
  • 使用applescript脚本方式以管理员权限运行

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

        // 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;
        }
    }

        NSString * output = nil;
        NSString * processErrorDescription = nil;
        BOOL success = [self runProcessAsAdministrator:@"/usr/bin/id"
                        withArguments:[NSArray arrayWithObjects:@"-un", nil]
                               output:&output
                                errorDescription:&processErrorDescription
                      asAdministrator:YES];

        if (!success) // Process failed to run
        {
             // ...look at errorDescription
        }
        else
        {
             // ...process output
        }

  • 相关阅读:
    spring 的简单了解
    leetcode 刷题锻炼算法思维
    REDIS学习笔记
    mark:如何使用FileZilla连接虚拟机上的Fedora
    尝试在virtualbox fedora21 下安装additions和mount share folder
    字符集与Mysql字符集处理(二)
    字符集与Mysql字符集处理(一)
    MYSQL开发性能研究——INSERT,REPLACE,INSERT-UPDATE性能比较
    MYSQL开发性能研究——批量插入的优化措施
    Marven笔记贴
  • 原文地址:https://www.cnblogs.com/watchdatalearn2012620/p/3145888.html
Copyright © 2011-2022 走看看