- (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
    }

使用applescript脚本方式以管理员权限运行的更多相关文章

  1. EasyNVR摄像机H5流媒体服务器在windows上批处理脚本自动以管理员权限运行

    很多时候, 我们需要以管理员权限来运行批处理脚本, 比如操作 windows 服务. EasyNVR 中提供安装服务的批处理脚本, 运行这个bat文件, 自动将 EasyNVR 以 windows 服 ...

  2. sharepoint 脚本 强迫以管理员权限运行

    #region 关键代码:强迫以管理员权限运行 $currentWi = [Security.Principal.WindowsIdentity]::GetCurrent() $currentWp = ...

  3. 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)

    将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...

  4. bat代码中判断 bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT

    · bat 代码中判断bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT 一.判断bat是否是以管理员权限运行 @echo off net.exe session >NUL & ...

  5. C#程序以管理员权限运行

    原文:C#程序以管理员权限运行 C#程序以管理员权限运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员 ...

  6. 【转】vc++MCF/C++/C中怎样让应用程序获得或以管理员权限运行 ,ShellExecuteEX编程 --- 获取管理员权限

    ShellExecuteEX编程 --- 获取管理员权限:http://blog.csdn.net/jhui163/article/details/5873027 怎样让你的应用程序获得管理员权限:就 ...

  7. C#程序以管理员权限运行【我采用了第二种,比较好用】

    在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表 ...

  8. 【转】 C#程序以管理员权限运行

    C#程序以管理员权限运行在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无 ...

  9. InnoSetup 脚本打包及管理员权限设置

    InnoSetup使用教程:InnoSetup打包安装 脚本详细 1. 定义变量 #define MyAppName "TranslationTool" #define MyApp ...

随机推荐

  1. 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

    最近在新项目中突然出现了  java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...

  2. PCB genesis连孔加除毛刺孔(圆孔与槽孔)实现方法(二)

    一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...

  3. 确定比赛名次(toposort)

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 #include <stdio.h> #include <string.h> ; ...

  4. 洛谷P1396营救(最小生成树)

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  5. 个人微信二次开发API接口

    通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...

  6. Android内存管理(9)*MAT:Heap Dump,Shallow Heap,Retained Heap,Dominating Tree,GC Roots等的含义

    原文: http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fconcepts%2Fheapdump.ht ...

  7. 关于vector.size()和string.length() 的返回类型 size_type

    今天写循环的时候碰到一个问题,发现:string.length()返回的类型是size_type.它是unsigned 类型.string::size_type它在不同的机器上,长度是可以不同的,并非 ...

  8. vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例

    第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...

  9. 本地mongochef连接其他计算机上的数据库认证失败解决方法

    关闭防火墙或者在信任程序列表添加运行目录下的mongod.exe即可

  10. Spring Cloud (11) Hystrix-监控聚合监控

    上一篇利用Hystrix Dashboard去监控断路器的Hystrix command,当我们有很多服务的时候,就需要聚合所有服务的Hystrix Dashboard数据了,这就需要Hystrix ...