我为Mac OS创建了一个没有GUI的命令行应用程序。此应用程序位于/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 /但是,如果您直接从终端运行此命令,它将正确执行。请告诉我,我做错了什么?
发布于 2016-04-15 00:48:05
我认为问题可能出在您对引号的使用上。当我尝试在shell中以您的引用风格手动运行相同的命令时,它将不起作用。我下面的例子是可行的。你能换一下你的单引号和双引号吗?用单引号将初始调用括起来,然后在osascript两边使用双引号?此外,不需要使用tell application \"Finder\" to,因为显示警报不是查找器字典的一部分。
你有...
/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.\""https://stackoverflow.com/questions/36620637
复制相似问题