Tim’s
Bandwagon

Talking PHP

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"'");
}

10 Comments

  1. 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?

  2. The example worked when I posted it! Is your PHP running in Safe Mode?

  3. 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.

  4. 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”);

  5. 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!

  6. I’ve got the same problem, i was just wondering if any of the smart minds out there ever figured it out.

  7. 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.

  8. 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.

  9. Simon, this may be too simple but have you tried a full path to “osascript”? i.e. use the path from “which osascript”?

  10. 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.

Leave a Comment

Your personal information

Your comment