来自可可的sudo chmod命令 [英] sudo chmod command from cocoa

查看:153
本文介绍了来自可可的sudo chmod命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Cocoa项目中运行以下命令. (隐藏聚光灯图标)

I would like to the run the following command from my Cocoa project. (hides the spotlight icon)

sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search

我发现了两种方法来调用命令并获得以下输出

I have found two ways how to call the command and get the following output

1)使用NSTask

1) Using NSTask

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)管道

NSString *command = @"sudo chmod 755/System/Library/CoreServices/Search.bundle/Contents/MacOS/Search";
fp = popen([command UTF8String], "r");

>>sudo: no tty present and no askpass program specified

我还没有找到一种在超级用户模式下运行这些方法的方法.如何提示用户输入密码,并最终以所需的特权运行此命令?

I have not found a way how to run any of these in super user mode. How can I prompt the user to enter his password and eventually run this command with the required privileges?

推荐答案

使用Apple Script Editor编写以下苹果脚本并将其保存在.scpt文件中.

Write the below apple script using Apple Script Editor and save it in a .scpt file.

do shell script "sudo chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search" with administrator privileges

现在将此文件添加到您的捆绑软件中.

Now add this file to your bundle.

然后从您的程序中调用此脚本,请使用以下代码:

Then to call this script from your program use the following code:

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

这将提示用户输入密码.

This will prompt the password request from user.

这篇关于来自可可的sudo chmod命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆