2

私のリビング ルームには、HTPC とホーム オートメーション サーバーの両方として使用している Mac Mini があります。自動化に使用するソフトウェアは、AppleScript 対応の無料のホーム オートメーション アプリである Shion です。同じ Mac Mini で Apache を実行しており、コマンドを送信できるインターフェイスを構築しました。(なんといっても、インターフェイスは jQuery Mobile を使用して構築されています。)

私が直面している問題は、ターミナルと 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);

ログ ファイルで、スクリプトが返したエラーは「[sic] identifier can't go after this identifier」でした。これは複数の人が遭遇した AppleScript エラーでしたが、一貫した解決策が見つかりませんでした。私が見つけた 1 つの手がかりは、「アプリケーション "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 エディターだけでなく、ターミナルでも問題なく実行されます。しかし、今ではログに別のエラーが表示されています:「予期された行末ですが、識別子が見つかりました」。

4

2 に答える 2

0

PHP構文が問題だとは思いません。私の Mac には Shion がインストールされていません。これが Finder と 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)

シェルと PHP の両方で同じエラーが発生していますが、Finder イベントは両方で機能することに注意してください。問題は、PHP スクリプトが実行されているコンテキストにあると思われます。ユーザー セッション内ではなく、Apache プロセスで実行されているため、Shion アプリケーションを「見る」ことができません。

残念ながら、私がこれについて正しければ、それを機能させる方法がわかりません。

于 2012-11-17T02:52:55.960 に答える
0

少なくともインタラクティブなphpでは、一重引用符を使用するとうまくいきました。

$cmd = 'osascript -e \'tell application "Shion" to execute snapshot "Evening Lightning"\''
于 2012-11-17T09:26:41.970 に答える