 |
 |
PHP > Shell Command > AppleScript... Ain't Working
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
So what I'm trying to do is whip together a web interface for iTunes, because I have a mac set up as sort of a 'media machine' plugged into my stereo, and I wanna be able to control it via a browser from any mac in the house. So what I came up with (with help) is this:
Code:
<? exec("osascript 'tell app \"iTunes\" to playpause'"); ?>
Run as a php, it should run the command line osascript command that should run that short AppleScript, and pause iTunes. Now if I add a simple 'echo' line in there, it works, so I know php is functioning on this machine. And I can SSH into the machine and run the osascript line directly, and it works, and pauses iTunes. But run through php, it doesn't wanna work. I've tried all of the following variations, with no luck...
Code:
<? exec("osascript 'tell app "iTunes" to playpause'"); ?>
<? exec("osascript 'tell app \"iTunes\" to playpause'"); ?>
<? exec("osascript 'tell app \\"iTunes\\" to playpause'"); ?>
<? exec("osascript 'tell app \\\"iTunes\\\" to playpause'"); ?>
It just won't budge. There must be something be I'm totally missing, right?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status:
Offline
|
|
Originally posted by l008com:
So what I'm trying to do is whip together a web interface for iTunes, because I have a mac set up as sort of a 'media machine' plugged into my stereo, and I wanna be able to control it via a browser from any mac in the house. So what I came up with (with help) is this:
Code:
<? exec("osascript 'tell app \"iTunes\" to playpause'"); ?>
I think you need to at least put a -e option in the command. I haven't tried it from within php, but the following works in a shell script with the -e, and doesn't without it.
osascript -e "tell application \"iTunes\" to playpause"
Your quoting may not be exactly right for php. That has always been the hardest part of embedding AS for me.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
I forgot the '-e' in this post, but not in my script. I too have gotten it to work flawlessly form the command line, it just won't jive with php for some reason.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2002
Location: Left Coast
Status:
Offline
|
|
Have you tried this:
<? exec("osascript -e \"tell app \"iTunes\" to playpause\""); ?>
If I use single quotes around the tell string in a sh script it doesn't work. I think osascript expects to see double quotes around the tell string. I don't know if that will be a problem for embedding it in php or not, but there must be some way to do that.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Another option is to put the AppleScript command in a file and run it via:
osascript filename
That way, you don't have all the weird escaping issues:
<?php
exec("osascript playpause.scpt");
?>
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
I tried that too, my problem doesn't seem to be with escaping at all.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by l008com:
I tried that too, my problem doesn't seem to be with escaping at all.
I just tried it myself. I had the AppleScript that worked. It worked when run via CLI and osascript. It worked when called on the command line via "php test.php", where test.php looks like this:
[php]
<?php
exec('/usr/bin/osascript /Library/WebServer/Documents/playpause.scpt');
?>
[/php]
It does NOT work when called via the web browser.
I wonder if there's an issue with permissions -- the web server is running as "www" but iTunes is running as my user. Maybe that is the problem.
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Is there a way to get around this? Maybe run apache is me? The computer behind a router so its not really a security risk.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Originally posted by l008com:
Is there a way to get around this? Maybe run apache is me? The computer behind a router so its not really a security risk.
So the problem is in the apache error log:
INIT_Processeses(), could not establish the default connection to the WindowServer.
So the issue is that the WindowServer process is not accessible by "nobody".
You could run the web server as yourself. I just verified that it does work when the web server is running as me.
To do that, edit /etc/httpd/httpd.conf. Change the lines:
Code:
# User/Group: The name (or #number) of the user/group to run httpd as.
# . On SCO (ODT 3) use "User nouser" and "Group nogroup".
# . On HPUX you may not be able to use shared memory as nobody, and the
# suggested workaround is to create a user www and use that user.
# NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET)
# when the value of (unsigned)Group is above 60000;
# don't use Group "#-1" on these systems!
#
User www
Group www
Change the value for "User" to your short username, and restart apache (sudo apachectl restart).
|
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: May 2001
Status:
Offline
|
|
Always check what you get back:
echo exec("osascript -e 'tell application \"iTunes\" to playpause' 2>&1") . "<br>\n";
Your httpd user might fail to connect to the WindowServer.
-
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
Arkham nailed it. Or, you can copy httpd.conf somewhere (say, ~/Library/), change it to run as you -- and also change the port from 80 to, say, 8000, and start it up with httpd -f ~/Library/httpd.conf.
Then you can have the regular Apache, and also the 'you' Apache accessed with hostname:8000. Drop a password protection (htdigest) into the 'you' Apache to keep things safe.
This also lets you link into nifty things, like reading your mail, your iPhotos, on-the-fly screenshots, or connecting via WebDAV to access your files...
good luck!
last tip: you will better performance if you do what you did above -- precompile the Applescript and use osascript to run that script.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Jan 2000
Location: Stoneham, MA, USA
Status:
Offline
|
|
Yeah running apache as me worked, now I have a full set of iTunes controls built right into my custom browser startup page. It works awesome!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|