but thats not working out. I get these logs in system.log :
Oct 19 18:41:09 interzone /usr/bin/osascript: kCGErrorIllegalArgument : initCGDisplayState: cannot map display interlocks.
Oct 19 18:41:09 interzone /usr/bin/osascript: kCGErrorIllegalArgument : CGSNewConnection cannot get connection port
/usr/bin/osascript /path/to/script works fine from a terminal BTW.
Anyone knows how to execute applescripts from a php page ? can it be done ?
Thanks for any clues.
hi greg, actually no, it doesn’t. What you describe looks like a mac-specific protocol helper, and thats not what I’m after.
What I need is to have any user, on mac, windows or unix that access a particular page (php web page) to launch an applescript on my machine that will access mysql, import files into indesign, generate a PDF, upload the file, etc, etc…
Thus it needs to be platform agnostic, and must be launched by apache/php.
Right now, what I do is have an applescript that checks the content of a folder for a text file containing a serial number, and upon finding that fires up the pdf creation process, then delete the text file. But it forces me to have a ‘always running’ applescript checking every second, and I find this less than ideal.
I really would like to get that <? shell_exec("/usr/bin/osascript /path/to/script") ?> going, as it would act upon demand, instead of my kludge workaround.
This should work but it doesn’t for me–I’m guessing I have permission problems:
<?php
shell_exec("osascript -l AppleScript -e 'tell application "Finder" to open file "path:to:Script Application.app"'");
?>
The code itself does work from the Terminal (remove the end quotes and escapes, of course).
I use the stay open app/PHP bridge all the time and don’t have problems with performance when using it. You don’t have to have your idle routine checking every second. I set mine to check every 5 minutes. You could also implement the bridge via a folder action on the folder where PHP writes the file.
I think Rainy day has hit the nail on the head. The messages:
Oct 19 18:41:09 interzone /usr/bin/osascript: kCGErrorIllegalArgument : initCGDisplayState: cannot map display interlocks.
Oct 19 18:41:09 interzone /usr/bin/osascript: kCGErrorIllegalArgument : CGSNewConnection cannot get connection port
give the game away.
The CG in the error codes refers to core graphics and core graphics requires the WindowServer process to be running. It may be that nobody is actually logged into the Macintosh at the time, which means that the WindowServer process is not running. Or worse (though better security) and more difficult to get around, the WindowServer process may have to be run as the user trying to run the AppleScript, in this case user www and you cannot log in as user www. It does not help if the AppleScript that runs does not do anything that effects what is being displayed on the screen.
A great deal can be accomplished with shell and perl scripts, or even PHP itself. There may be another way to attack your problem which doesn’t involve AppleScript.
As already noted, the problem is that the user cannot bind to the WindowServer, either because it’s not logged in, or because some other user is.
The solution is simple - run the process as root.
While not necessarily ideal to have web-launched processed run as root, an AppleScript run as root can bind to the WindowServer even if it’s not logged in.
Change your PHP call to launch osascript as root and see if that fixes your problem.
This really shouldn’t be done at all. If and only if the web server is not connected to the InterNet should this even be considered. It’s just too much of a security risk.
It would be better to have a man-in-the-middle daemon process running which would accept certain requests from the www user and relay them to a process running as a logged-in user.
I agree that the root option is probably not a good idea at all. Once again, I suggest using the solution I’ve offered several times in the past (PHP writes a file that a stay open AS reads and and acts on when it appears–any properties that need to be passed between the PHP script and AS can be written to the file and read in as necessary).
Your hint has enlightened me on the direction of how to solve it. However, I would prefer a folder action to this solution which calls “on adding folder items”. However, I am new to Folder Action (in fact, new to AppleScript as well) and I am not sure about whether a file written by php in apache will trigger the folder action. Let me try it out and post my finding later.