简体   繁体   中英

How to run Shell Script/AppleScript in PHP?

I'm trying to run AppleScript in PHP. 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" . The middle part (3 exec commands) have no output result at all. I don't understand.

As far as I know you can't execute AppleScript from a webserver for security reasons. 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. By default apache will run as _www as it's shells and _www user can't run osascript on the command line.

The workaround is running the shell command as console user or as root.

Create an AppleScript Application and save it with the app extension, then link from the PHP page. I use it on some projects in localhost, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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