简体   繁体   English

如何在 PHP 中运行 Shell Script/AppleScript?

[英]How to run Shell Script/AppleScript in PHP?

I'm trying to run AppleScript in PHP.我正在尝试在 PHP 中运行 AppleScript。 The following is my code:以下是我的代码:

<?php
    echo "Hello World!";
    shell_exec('osascript -e \'tell app "Finder" to make new Finder window\'');
    shell_exec('osascript -e \'display dialog "hello"  \'');
    shell_exec('osascript ~/openExcel.scpt');
    echo "DONE";
?>

When I run each command individually in terminal, I can get the expected result.当我在终端中单独运行每个命令时,我可以获得预期的结果。 However, when I open this .php file in server as a webpage, I can only get "Hello World!DONE" .但是,当我在服务器中将这个 .php 文件作为网页打开时,我只能得到"Hello World!DONE" The middle part (3 exec commands) have no output result at all.中间部分(3 个exec命令)根本没有输出结果。 I don't understand.我不明白。

As far as I know you can't execute AppleScript from a webserver for security reasons.据我所知,出于安全原因,您无法从网络服务器执行 AppleScript。 The problem is that AppleEvents can only be send through the window server by the user that's only currently logged in as the windowserver user (console user) or as root.问题是 AppleEvents 只能由当前仅以 windowserver 用户(控制台用户)或 root 登录的用户通过窗口服务器发送。 By default apache will run as _www as it's shells and _www user can't run osascript on the command line.默认情况下,apache 将作为 _www 运行,因为它是 shell,而 _www 用户不能在命令行上运行 osascript。

The workaround is running the shell command as console user or as root.解决方法是以控制台用户或 root 身份运行 shell 命令。

Create an AppleScript Application and save it with the app extension, then link from the PHP page.创建一个 AppleScript 应用程序并将其与应用程序扩展一起保存,然后从 PHP 页面链接。 I use it on some projects in localhost, it works.我在本地主机的一些项目上使用它,它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM