在我的起居室里,我有一台Mini,它既是一台HTPC,也是一台家庭自动化服务器。它用于自动化的软件是Shion,这是一个免费的家庭自动化应用程序,它具有AppleScript功能。在同一台Mini上,我正在运行Apache,并构建了一个可以发送命令的接口。(就其价值而言,该接口是使用jQuery移动构建的。)
我遇到的问题是,在终端和AppleScript编辑器中工作良好的AppleScript正在抛出Apache日志中的解析错误。由于终端和AppleScript编辑器成功地运行了脚本,我猜我编写PHP的方式就是问题所在。但是当我检查错误日志时,它实际上是一个AppleScript错误。
AppleScript命令非常简单:
tell application "Shion"
execute snapshot "Evening Lighting"
end tell
或者更简单:
tell application "Shion" to execute snapshot "Evening Lighting"
这是我开始使用的原始命令,因为我不确定如何使用-e标志将AppleScript分解为多行。当我将其粘贴到AppleScript编辑器或终端中时,它将执行而不会出现任何问题。但是在PHP中运行它是行不通的:
$cmd = "osascript -e 'tell application \"Shion\" to execute snapshot \"Evening Lighting\"'";
exec($cmd);
在日志文件中,脚本返回的错误是"A sic标识符不能跟踪此标识符“。这是多个人遇到的AppleScript错误,但我找不到任何一致的解决方案。我发现的一条线索是试图在脚本的开头添加‘使用应用程序“中的术语”Shion“,这样看起来如下所示:
using terms from application "Shion"
tell application "Shion"
execute snapshot "Evening Lighting"
end tell
end using terms from
我必须弄清楚如何用osascript将AppleScript分解成多行,这可以使用-e标志来完成。如果我将它作为常规的osascript命令运行,它看起来如下:
osascript -e 'using terms from application "Shion"' -e 'tell application "Shion"' -e 'execute snapshot "Evening Lighting"' -e 'end tell' -e 'end using terms from'
这再次在终端和AppleScript编辑器中运行,没有任何问题。但是现在我在日志中得到了一个不同的错误:“预期的行尾但找到了标识符”。
发布于 2012-11-17 02:52:55
我不认为PHP语法是问题所在。我没有在我的Mac上安装Shion,以下是我在Finder vs.中看到的:
$ osascript -e 'tell application "Finder" to activate'
[Finder pops to foreground]
$ osascript -e 'tell application "Shion" to execute snapshot "Evening Lightning"'
28:44: syntax error: A identifier can’t go after this identifier. (-2740)
$ php -a
Interactive shell
php > exec("osascript -e 'tell application \"Finder\" to activate'");
[Finder pops to foreground]
php > exec("osascript -e 'tell application \"Shion\" to execute snapshot \"EveningLighting\"'");
28:44: syntax error: A identifier can’t go after this identifier. (-2740)
请注意,我在shell和PHP中都得到了相同的错误,但是Finder事件在这两种情况下都有效。我怀疑问题在于PHP脚本正在运行的上下文:它在apache进程下运行,而不是在用户会话中运行,因此无法“看到”Shion应用程序。
不幸的是,如果我是对的,我不知道有什么办法让它起作用。
发布于 2012-11-17 09:26:41
使用单引号对我有用,至少在交互式php中是这样的:
$cmd = 'osascript -e \'tell application "Shion" to execute snapshot "Evening Lightning"\''
https://stackoverflow.com/questions/13426728
复制相似问题