iTunes Web Remote
March 23rd, 2004
This little tutorial will show you how to create a web based remote for Apple iTunes. It's only for Mac OS X. It requires some modifications to the Apache Web Server that make it less secure, so I recommend you do this on a computer that's safely hidden behind a router, and preferably a computer that isn't using Apache for anything else.
Mac OS X Upgrade Note:
Upgrading from one major version of OS X to another will likely replace your apache config file with a fresh default one. So you will probably have to make these changes again to get the remote working.
Basic Idea
- Modify your Apache Config
- Create the controlling PHP file
- Call the PHP file to control iTunes
Step By Step
You must make a few modifications to the Apache web server. The path to your Apache Configuration file is /etc/httpd/httpd.conf
You must edit it as root
. I use pico
, so in the terminal, simply type:
sudo pico /etc/httpd/httpd.conf
Now, assuming you have a default (or nearly so) Apache configuration, you must make the following modifications. First, you must enable PHP within Apache. Search for the section that starts with "Dynamic Shared Object (DSO) Support
" In the first group of items, uncomment the following line:
#LoadModule php4_module libexec/httpd/libphp4.so
In the group of items immediately following that section, uncomment the following line:
#AddModule mod_php4.c
Next, you want to make apache run as the same user as YOU. Normally Apache runs as its own user. This is the part that has some potential security risks. You need to know your short name on the system. If you don't know it, go to the Accounts section of System Preferences, and choose your account. You will see your short name. Now, back to the Apache Config file. Scroll down until you see the following lines:
#
User www
Group www
You want to change the first www
to your short name, and change the second www
to staff
.
The last thing you want to do is add index.php
as a default file name to make things run a little easier. Scroll down to the following line:
<IfModule mod_dir.c>
DirectoryIndex index.html
</IfModule>
And after index.html
, add a space, and index.php
.
Now you need to save your file (Control O
if you are using pico
). Exit out of your editor and when you are back at the command line, type the following to restart the Apache web server:
sudo apachectl graceful
The next step is to create a php file. You are going to link to this page from another page (whichever page you want) to control iTunes. If you are confused, keep reading and it will be explained. First off, within the HEAD
tag of this file, insert the following code so that when you call this file, it will perform the function on iTunes and then return you to linking page:
<? $url = $_SERVER['HTTP_REFERER'];
echo "<META HTTP-EQUIV=refresh content=0;URL=$url>" ?>
Now you want to insert the following code in the body of the php document. This code takes the commands you send it and then sends AppleEvents out to iTunes through the command line.
<? $q = $_GET['q']; switch ($q) { case "": echo "You need to send me a command, then I shall execute it"; break; case "play": exec("osascript -e 'tell app \"iTunes\" to play'"); echo "Playing"; break; case "pause": exec("osascript -e 'tell app \"iTunes\" to pause'"); echo "Pausing"; break; case "playpause": exec("osascript -e 'tell app \"iTunes\" to playpause'"); echo "Toggling Play"; break; case "next": exec("osascript -e 'tell app \"iTunes\" to next track'"); echo "Next Track"; break; case "prev": exec("osascript -e 'tell app \"iTunes\" to previous track'"); echo "Previous Track"; break; case "louder": exec("osascript -e 'tell app \"iTunes\" to set sound volume to sound volume + 5'"); echo "Turning Up the Volume"; break; case "quieter": exec("osascript -e 'tell app \"iTunes\" to set sound volume to sound volume - 5'"); echo "Turning Down the Volume"; break; case "mute": mutev(); echo "Muting the Volume"; break; } function mutev() { echo "start mute function"; $data = file_get_contents("/webfolder/volume.txt"); $logfile = fopen("/webfolder/volume.txt",'w'); $oldvolume = exec("osascript -e 'tell app \"iTunes\" to sound volume'"); echo "volume data:$data:"; if ($data == "x") { fwrite($logfile,$oldvolume); exec("osascript -e 'tell app \"iTunes\" to set sound volume to 0'"); } else { fwrite($logfile,"x"); exec("osascript -e 'tell app \"iTunes\" to set sound volume to $data'"); } fclose($logfile); } ?>
Now, in the code you just inserted into the control.php file. You must go back and find the two lines near the end that call the file /webfolder/volume.txt
In both of those lines, you must change the 'webfolder' to the actual path to your web folder. The default location for this file would be
/Library/WebServer/Documents/volume.txt
After you get both of the pathes right, you must now create the volume.txt file, and make it writeable to all. There are many ways to do this, but if you don't know any, use these commands in the Terminal.
cd /Library/WebServer/Documents/
(change this line if your web folder is in a custom location)
pico volume.txt
Hit
'Control O
' to exit pico, hit return to save, then hit 'Control X
'
sudo chmod 777 volume.txt
That last line makes the volume.txt file writeable by all. That concludes this step. The final step is to link to the control.php file. From your bookmark page or wherever you want to control iTunes from, just insert links to control.php into the page. Like so:
http://192.168.0.8/control.php?q=play
Notice the 'q=play
' at the end. Change play
to whatever you want the script to do. Here is a list of all the commands that the script accepts:
play
pause
playpause
next
prev
louder
quieter
mute
You can easily add your own commands as well, if you are familiar with php and AppleScript.
The End
Now you should be able to use URL's to control iTunes from anywhere in your house, or if you wanted to, you could set it up so it's controlable from the internet too. You can also use the same technique to GET info on the current song too, but that's much slower to execute.
Uncomment - In an Apache config (among other places), line that begin with a # symbol are comment out, when the config file is read in, those lines are ignored. To uncomment a line, you must delete that # symbol at the beginning of the line.