dpspn60064 2012-11-17 01:23
浏览 99

too long

In my living room, I have a Mac Mini I using as both an HTPC as well as a home automation server. The software it uses for the automation is Shion, a free home automation app which is AppleScript capable. On that same Mac Mini, I'm running Apache and have built an interface through which I can send commands. (For what it's worth, the interface is built using jQuery Mobile.)

The trouble I'm running into is that the AppleScripts which work fine in the terminal and in the AppleScript editor are throwing parse errors in the Apache error logs. Since the terminal and AppleScript editor run the script successfully, I would guess the way I've coded my PHP is the problem. But when I check the error logs, it's actually an AppleScript error which shows up.

The AppleScript command is extremely simple:

tell application "Shion"
    execute snapshot "Evening Lighting"
end tell

Or even simpler:

tell application "Shion" to execute snapshot "Evening Lighting"

This was the original command I started working with, because I wasn't sure how to use the -e flag to break an AppleScript into multiple lines. When I would paste this into the AppleScript editor or into the terminal, it would execute without any problems. But running it in PHP wouldn't work:

$cmd = "osascript -e 'tell application \"Shion\" to execute snapshot \"Evening Lighting\"'";
exec($cmd);

In the log file, the error the script was returning was "A [sic] identifier can't go after this identifier". This was an AppleScript error which multiple people had run into, but I couldn't find any consistent solutions. The one lead I found was attempting to add 'using terms from application "Shion"' to the beginning of the script, so that it would look like this:

using terms from application "Shion"
    tell application "Shion"
        execute snapshot "Evening Lighting"
    end tell
end using terms from

I had to figure out how to break an AppleScript into multiple lines with osascript, which can be done using the -e flag. If I run this as a regular osascript command, it looks like this:

osascript -e 'using terms from application "Shion"' -e 'tell application "Shion"' -e 'execute snapshot "Evening Lighting"' -e 'end tell' -e 'end using terms from'

Once again, this runs in the terminal with no problem, as well as the AppleScript editor. But now I'm getting a different error in my log: "Expected end of line but found identifier".

  • 写回答

2条回答 默认 最新

  • duanbei3704 2012-11-17 02:52
    关注

    I don't think PHP syntax is the problem. I don't have Shion installed on my Mac, and here's what I see in Finder vs. PHP:

    $ 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)
    

    Note that I'm getting the same error you do in both shell and PHP, but the Finder event works in both. I suspect the problem is the context that the PHP script is running in: it's running under the apache process, and not within a user session, and therefore cannot "see" the Shion application.

    Unfortunately, if I'm right about this, I don't know a way to get it working.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)