无法从命令行应用程序运行Apple脚本

时间:2016-04-14 10:39:38

标签: objective-c command-line applescript nstask applescript-objc

我为没有GUI的Mac OS创建了一个命令行应用程序。此应用程序位于/ usr / local / bin。在某些情况下,我需要在该应用程序中执行Apple Script。为此,我创建了一个NSTask并尝试运行以下命令:

NSTask *createTask = [[NSTask alloc] init];
    createTask.launchPath = @"/bin/bash";
    NSString *showAlert = [NSString stringWithFormat:@"/usr/bin/osascript -e 'tell application \"Finder\" to display alert \"My text.\"'"];
    NSArray *arguments = [NSArray arrayWithObjects:@"-c",showAlert, nil];
    createTask.arguments = arguments;
[createTask launch];

运行后,没有任何反应,仅在日志中显示消息:

Apr 14 15:35:15 Mac-mini kernel[0]: my-app: guarded fd exception: fd 0 code 0x2 guard 0x7fff8b9e12a8
Apr 14 15:35:15 Mac-mini com.apple.xpc.launchd[1] (com.apple.ReportCrash.Root[26852]): Endpoint has been activated through legacy launch(3) APIs. Please switch to XPC or bootstrap_check_in(): com.apple.ReportCrash.DirectoryService
Apr 14 15:35:15 Mac-mini diagnosticd[16097]: error evaluating process info - pid: 26851, punique: 26851
Apr 14 15:35:16 Mac-mini sandboxd[16099] ([26851]): my-app(26851) deny file-read-data /

但如果直接从终端运行此命令,则会正确执行。请告诉我,我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为问题可能在于您使用引号。当我尝试使用您的引用样式在shell中手动运行相同的命令时,它不会起作用。我的下面的例子确实有效。你能改变你的单引号和双引号吗?使用单引号包含您的初始调用,然后使用osascript周围的双引号?此外,无需使用tell application \"Finder\" to,因为显示警报不是Finder字典的一部分。

你有......

/usr/bin/osascript -e 'tell application \"Finder\" to display alert \"My text.\"'

尝试将其更改为...

/usr/bin/osascript -e "display alert \"My text.\""

或者更简单的版本......

osascript -e "display alert \"My text.\""
相关问题