iTunes Remote

Hi guys, I managed to make a nifty little website site with which to control my iTunes using php and applescript. The way it’s setup I can access this site from my blackberry and control my music from anywhere in my house / the world.

As of right now the script serves only to play / pause the track, previous track, next track, volume up or down, and display the song name and artist. I have been unsuccessful as of yet to find anymore commands for iTunes which can be executed to increase the functionality. My ultimate goal would be to be able to search for and choose songs, choose different playlists, etc.

If anybody happens to know a whole bunch of commands which can be executed using osascript -e which could help me increase functionality that would be fantastic. For those interested here is the current php script I am using.

[code]

iTunes Controller
	<?php
		$q = $_GET['q'];
		
		switch ($q)
		{
		case "play";
		exec("osascript -e 'tell app \"iTunes\" to play'");
		break;
		
		case "pause";
		exec("osascript -e 'tell app \"iTunes\" to pause'");
		break;
		
		case "playpause";
		exec("osascript -e 'tell app \"iTunes\" to playpause'");
		break;
		
		case "next";
		exec("osascript -e 'tell app \"iTunes\" to next track'");
		break;
		
		case "prev";
		exec("osascript -e 'tell app \"iTunes\" to 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;
		}
	?>

	<img src="logo.png" alt="iTunes Controller" /><br />
	<div id="center">
		<hr />
		<a href="index.php?q=prev"><img src="prev.png" alt="Previous" /></a> 
		<a href="index.php?q=playpause"><img src="play.png" alt="Play / Pause" /></a> 
		<a href="index.php?q=next"><img src="next.png" alt="Next" /></a>
		<?php
		$song = exec("osascript -e 'tell app \"iTunes\" to name of current track as string'");
		$artist = exec("osascript -e 'tell app \"itunes\" to artist of current track as string'");
		if ($song != "" || $artist != "") {
			echo '<p><strong>' . $song . '</strong><br />' . $artist . '</p>';
		} else {
			echo '<p><em>No Song Playing</em></p>';
		}
		?>
	</div>
</body>
[/code] Model: Macbook Browser: Firefox 3.0.3 Operating System: Mac OS X (10.5)

This is how to select a song by name:

tell application "iTunes" to tell playlist "Music" to play (first track whose name is songname)

I use it in a lot of my scripts, hope it helps.

Thanks man I’ll work that into it.

Does anyone know if it’s possible to get it to return a list of my playlists? I can figure out how to get it to play a certain playlist but to be able to choose one of them from a list would be fantastic.

Maybe you could use code as follows:


tell application "iTunes"
	set playlistnames to name of every playlist
end tell

set output to ""
set countplaylistnames to length of playlistnames
repeat with i from 1 to countplaylistnames
	set playlistname to item i of playlistnames
	if i is not equal to countplaylistnames then
		set output to output & playlistname & return
	else
		set output to output & playlistname
	end if
end repeat

return output

you can choose a playlist from a list of all playlists with this AppleScript code
but I have no idea to use this in a php environment


tell application "iTunes"
	set allPlaylists to name of playlists
	set chosenList to choose from list allPlaylists
end tell
if chosenList is false then return
set chosenList to item 1 of chosenList