使用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
}
使用applescript脚本方式以管理员权限运行的更多相关文章
- EasyNVR摄像机H5流媒体服务器在windows上批处理脚本自动以管理员权限运行
很多时候, 我们需要以管理员权限来运行批处理脚本, 比如操作 windows 服务. EasyNVR 中提供安装服务的批处理脚本, 运行这个bat文件, 自动将 EasyNVR 以 windows 服 ...
- sharepoint 脚本 强迫以管理员权限运行
#region 关键代码:强迫以管理员权限运行 $currentWi = [Security.Principal.WindowsIdentity]::GetCurrent() $currentWp = ...
- 修改hosts文件(判断是否为管理员/以管理员权限运行脚本)
将以下命令保存为 HostsModify.ps1,然后执行即可 #该脚本用来添加hosts解析记录.脚本在执行的时候会判断当前用户是否为管理员,如果不是则弹出提示框口,要求输入相应密码 If (-NO ...
- bat代码中判断 bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT
· bat 代码中判断bat是否是以管理员权限运行,以及自动以管理员权限运行CMD BAT 一.判断bat是否是以管理员权限运行 @echo off net.exe session >NUL & ...
- C#程序以管理员权限运行
原文:C#程序以管理员权限运行 C#程序以管理员权限运行 在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员 ...
- 【转】vc++MCF/C++/C中怎样让应用程序获得或以管理员权限运行 ,ShellExecuteEX编程 --- 获取管理员权限
ShellExecuteEX编程 --- 获取管理员权限:http://blog.csdn.net/jhui163/article/details/5873027 怎样让你的应用程序获得管理员权限:就 ...
- C#程序以管理员权限运行【我采用了第二种,比较好用】
在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录.系统注册表 ...
- 【转】 C#程序以管理员权限运行
C#程序以管理员权限运行在Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC(用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无 ...
- InnoSetup 脚本打包及管理员权限设置
InnoSetup使用教程:InnoSetup打包安装 脚本详细 1. 定义变量 #define MyAppName "TranslationTool" #define MyApp ...
随机推荐
- 用回调函数创建一个XMLHttpRequest,并从一个TXT文件中检索数据。
<script> var xmlhttp; function loadXMLDoc(url,soyo) { if (window.XMLHttpRequest) {// IE7+, Fir ...
- java replaceAll 忽略大小写
public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...
- struts2什么情况用#和EL表达示
1:struts2标签使用中,什么时候用#,什么时候可以不用# 值栈中的对象的不使用#,非值栈中的对象使用#如果不理解值栈的作用,简单点理解:当前action,或者处于action链中的action所 ...
- redis取经之路
redis基本数据结构 Redis使用的是自己构建的简单动态字符串(SDS)[simple dynamic string,SDS]的抽象类型,并将SDS用做Rdis的默认字符串表示 redis> ...
- C# 截取字符串——
string strID ="NODE_aSDFghsdfgyuhjidfgh_45678" //得到_ 中间的数 int index = strID.IndexOf(" ...
- DropDownList 数据源绑定和获取
前台代码: <td>账户名称:</td> <td> <asp:DropDownList ID="DropDownListAccount" ...
- 【BZOJ1939】[Croatian2010] Zuma(动态规划)
题目: BZOJ1939(权限题) 分析: 这题很容易看出是DP,但是状态和转移都不是很好想-- 用\(dp[l][r][c]\)表示在\(l\)前面已经新加了\(c\)个和\(l\)一样的弹子时,使 ...
- [转]linux之pr命令
转自:http://www.bitscn.com/plus/view.php?aid=6638 本文介绍如何使用Linux的pr命令将大文件分割成多个页面进行打印,并在每个页面上加上标题. Linux ...
- Android 微信SDK图片分享(checkArgs fail, thumbData is invalid)
微信官网给的Demo中.图片的分享例子他是这么描述的: String url = "http://pic2.nipic.com/20090506/1478953_125254084_2.jp ...
- Android 微信分享图片
"; //微信 APPID private IWXAPI iwxapi; private void regToWx() { iwxapi = WXAPIFactory.createWXAPI ...