【问题标题】:Return Applescript Shell Output to Objective C将 Applescript Shell 输出返回到 Objective C
【发布时间】:2013-07-20 18:47:54
【问题描述】:

我正在编写一个可以远程运行 shell 命令的应用程序。我需要以管理员身份运行命令:tc -l 455。我决定在具有管理权限的 applescript 中运行此命令,但在目标 c 中,我需要获取此输出并将其显示在 NSExView 中。这是我的代码:

NSAppleScript* runWithAdminPrivileges = [[NSAppleScript alloc] initWithSource:@"do     shell script \"nc -l 455\" with administrator privileges"];
NSDictionary *error = [[NSDictionary alloc] init];
[runWithAdminPrivileges executeAndReturnError:&error];
NSLog(@"%@", error);

命令正在运行,但我无法看到输出。有没有办法用我的代码做到这一点,或者有没有办法在目标 c 中以管理员权限运行这个 shell 命令并查看输出?

提前致谢,

【问题讨论】:

  • 你可以使用NSTask:How to use NSTask as root?
  • 架构 x86_64 的未定义符号:“_OBJC_CLASS_$_STPrivilegedTask”,引用自:AppDelegate.o ld 中的 objc-class-ref:未找到架构 x86_64 的符号 clang:错误:链接器命令失败退出代码为 1(使用 -v 查看调用)
  • 这是我运行 STPrivilegedTask 时遇到的错误
  • STPrivilegedTask 没有内置到 Cocoa 中。您需要下载文件并编译它们。
  • 我确实下载了它们并将它们导入到我的项目中。在源代码中没有错误。只是这个运行时错误。

标签: objective-c bash shell applescript


【解决方案1】:

-[NSAppleScript executeAndReturnError:] 返回您忽略的结果,这是脚本的结果。它是一个 NSAppleEventDescriptor,而不是 NSString,但你可以通过调用 -stringValue 来获得一个 NSString。此外,error 参数遵循与其他地方的NSError ** 参数相同的规则,因此您无需先用对象填充它。总结一下:

NSDictionary *errorInfo; // no initialization necessary.
NSAppleEventDescriptor *scriptResult = [runWithAdminPrivileges executeAndReturnError:&error];
if (scriptResult)
    NSLog(@“%@“, [scriptResult stringValue]);
else
    NSLog(@“%@“, errorInfo;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多