来自cocoa的sudo chmod命令
我想从Cocoa项目中运行以下命令。(隐藏聚光灯图标) 我找到了两种方法来调用命令并获得以下输出 1) 使用NSTask来自cocoa的sudo chmod命令,cocoa,popen,sudo,nstask,Cocoa,Popen,Sudo,Nstask,我想从Cocoa项目中运行以下命令。(隐藏聚光灯图标) 我找到了两种方法来调用命令并获得以下输出 1) 使用NSTask NSTask *writeTask = [[NSTask alloc] init]; [writeTask setLaunchPath:@"/bin/chmod"]; [writeTask setArguments: @[@"755",@"/System/Library/CoreServices/Search.bundle/Contents/MacOS/Search"]];
NSTask *writeTask = [[NSTask alloc] init];
[writeTask setLaunchPath:@"/bin/chmod"];
[writeTask setArguments: @[@"755",@"/System/Library/CoreServices/Search.bundle/Contents/MacOS/Search"]];
[writeTask launch];
>>chmod: Unable to change file mode on /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search: Operation not permitted
2) 管道
我还没有找到一种方法,如何在超级用户模式下运行这些。如何提示用户输入密码并最终以所需权限运行此命令?在10.7之前,您可以使用授权ExecuteWithPrivileges()以权限执行任务。然而,这在10.7中已被弃用,苹果建议使用
ServiceManagement
框架来执行特权任务
SMJobBless演示了如何安全地安装帮助工具
执行特权操作以及如何将工具与
调用它的应用程序
SMJobBless使用Mac中引入的ServiceManagement.framework
OSXV10.6雪豹
对于雪豹来说,这是管理特权的首选方法
应在Mac OS X上升级,而不是更早使用
诸如BetterAuthorizationSample或直接调用
授权ExecuteWithPrivileges
Apple的使用
Apple脚本编辑器
编写以下Apple脚本,并将其保存在.scpt
文件中
do shell script "sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search" with administrator privileges
现在将此文件添加到您的包中
然后,要从程序中调用此脚本,请使用以下代码:
- (void) runEmbeddedScriptFile: (NSString*)fileName
{
NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];
}
这将提示用户请求密码。您需要使用获取root权限,然后使用sys/stat.h中的chmod()可能重复的
do shell script "sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search" with administrator privileges
- (void) runEmbeddedScriptFile: (NSString*)fileName
{
NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];
NSDictionary* errors = [NSDictionary dictionary];
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
[appleScript executeAndReturnError:nil];
[appleScript release];
}