I just figured out how to trigger speech from PHP on MacOS X. I’m finding it handy for debugging, but it has opened up a whole new world of stupid possibilities.
if ($condition) { say("Hello"); } function say($something) { exec("osascript -e 'say "$something"'"); } |
Published at 3:04 pm on April 4th, 2003.
Topics: PHP Software
10 Comments
Have you actually gotten this to work? I’ve been trying to use a similar technique to get my website to display the song currently playing in iTunes. I have written an AppleScript that successfully returns the Artist, song name and rating but I am unable to run the osascript command from within php. It works fine in the Terminal but neither $thestring = shellexec(“osascript -e “myscript.scpt”") nor $thestring =
osascript -e "my_script.scpt"will return anything. I can’t get any other osascript-command to work either. Someone know whats wrong?Comment by Simon Fransson at 10:16 pm on November 13th, 2003 #link
The example worked when I posted it! Is your PHP running in Safe Mode?
Comment by Tim at 10:45 pm on November 13th, 2003 #link
No it’s not. I am using the standard PHP included with Mac OS X 10.3. I am however planning to install the Entropy PHP release in the near future. The thing is that I am able to run almost any command from shell_exec(), but not the osascript command. pwd, ls, uptime and so on will return strings to the php-script but I can’t get it working with the osascript command. Not even a simple script “osascript -e “return 2″” will work, even though this script and most others works fine when run in the Terminal.
Comment by Simon Fransson at 6:48 am on November 14th, 2003 #link
Not sure what to suggest! Hope you work it out. FYI the following works for me: printr (exec(“osascript -e ‘return “exec”‘”) . “\n”); printr (shellexec(“osascript -e ‘return “shellexec”‘”) . “\n”); print_r (
osascript -e 'return "backticks"'. “\n”);Comment by Tim at 9:26 am on November 14th, 2003 #link
Strange. None of them works for me. I just installed PHP 4.3.4R1 (Entropy) and I still get the same result. No idea why it’s like that. But it feels good to know that it isn’t impossible to solve. An alternative solution might be to write a perl och shell script that fetches the string from iTunes and let shell_exec run that instead. Thanks for your help and fast answers by the way!
Comment by Simon Fransson at 10:44 am on November 14th, 2003 #link
I’ve got the same problem, i was just wondering if any of the smart minds out there ever figured it out.
Comment by brimstone at 2:16 am on December 16th, 2003 #link
The problem is that apache is not running as the same user with who is currently logged in (you). Under OS X, Apache runs under the user www. This is easily fixed by editing the Apache config file and changing the user to you. Open terminal and enter:
sudo pico /etc/httpd/httpd.conf
Find the line “User www” and replace www with your username. Save the file, and then start or restart Apache.
Keep in mind that some Apple updates will change the user back to www. So if you run Software Update and it’s not working again, then you have to go through this again.
Comment by fentoozler at 11:54 pm on January 14th, 2004 #link
I’m not too sure about that. I can’t get ANY osascript to work through php. And I also succesfully ran “su www” followed by “osascript itunes_playing.scpt”. But you’re probably right. It’s most likely not a php error since “php playing.php” gives me the right output when run by my user.
Comment by Simon Fransson at 9:59 pm on January 21st, 2004 #link
Simon, this may be too simple but have you tried a full path to “osascript”? i.e. use the path from “which osascript”?
Comment by Tim at 12:03 pm on January 22nd, 2004 #link
Hi,
I was tinkering with this myself today, and the only way I could get things to work was to (as noted by fentoozler above) let Apache run as my User and Group (gui / uid, unix username and group).
Not very clean, but works.
Comment by Jorgen at 11:03 pm on March 11th, 2007 #link