Use a remote control with number buttons and iTunes

May 04, '06 07:30:00AM

Contributed by: nick

I've set up a G4 Cube as an iTunes server (VCR, movie player, backup server, etc.) connected to an LCD TV, and I use an ATI Remote Wonder to control iTunes. The problem is that i find it quite uncomfortable to select a playlist with the Remote Wonder's cursor. You have to scroll the list down (move the cursor to the scroll bar), then select a playlist (move the cursor to the playlist).

So I thought I would write a script to ease the task of selecting a playlist. This script can easily be extended to allow the selection of a movie (or any other file).

#!/bin/sh

# get the playlists:
echo `/usr/bin/osascript << END | tr "," "\n" > playlists.tmp
try
	tell application "iTunes"
		set the_playlists to the name of every playlist
	end tell
end try
END
`
# get single characters (without the need to press enter) from std-in:
getc (){
   stty raw
   eval $1=`dd bs=1 count=1 2>/dev/null`
   stty cooked
}

cat playlists.tmp
# convert numbers to characters
while [[ $EXIT != "true" ]]; do
	getc CHARACTER
	clear
	case $CHARACTER in
		1)
			CHARACTER="[.]"
			;;
		2)
			CHARACTER="[2ABCabc]"
			;;
		3)
			CHARACTER="[3DEFdef]"
			;;
		4)
			CHARACTER="[4GHIghi]"
			;;
		5)
			CHARACTER="[5JKLjkl]"
			;;
		6)
			CHARACTER="[6MNOmno]"
			;;
		7)
			CHARACTER="[7PQRSpqrs]"
			;;
		8)
			CHARACTER="[8TUVtuv]"
			;;
		9)
			CHARACTER="[9WXYZwxyz]"
			;;
		0)
			CHARACTER="[0 _-]"
			;;
		q)
			exit
			;;
	esac
	
	SEARCH=${SEARCH}${CHARACTER}
	
	if [[ `grep -i "$SEARCH" playlists.tmp | wc -l` == "       1" ]]; then
		PLAYLIST=`grep -i "$SEARCH" playlists.tmp | perl -pe 's/^+[\t ]//g'`
		EXIT="true"
	elif [[ `grep -i "$SEARCH" playlists.tmp | wc -l` == "       0" ]]; then
		SEARCH=""
	fi
	grep -i "$SEARCH" playlists.tmp | tr '[A-Z]' '[a-z]' | sed s/"\($SEARCH\)"/"[\1]"/

done

echo "--------"
echo $PLAYLIST
echo "--------"

/usr/bin/osascript << END
	tell application "iTunes"
		set the_playlist to item 1 of (every playlist whose name is "$PLAYLIST")
		play track 1 of the_playlist
	end tell
	tell application "iTunes" to activate
END

sh $0


The script does the following:
  1. Queries iTunes for the name of all playlists (I first tried parsing the XML file, but had problems with special characters) and saves the list to a file (when storing it in a variable I lost the line breaks)
  2. Defines the function getc to switch Terminal to raw-input-mode and allow the input of single characters without the need of having to press Return after each character
  3. Runs a loop to get the input characters.
  4. Turn- the numbers of the input characters to RegEx snippets to match the characters printed on the number keys of my remote. Exits, if the input character is a q.
  5. Appends the input character to the search string.
  6. Checks for unique matches.
    1. If yes, it exits the loop.
    2. If no, runs grep with the search string in the playlists so show how good the match is so far.
    3. If there is no match, starts over with an empty search string.
  7. After the loop, the match is displayed and iTunes is told to play the chosen playlist.
  8. Brings iTunes to the front.
  9. Restarts the script.
On my Cube the Terminal isn't used for anything else (I ssh into the machine if I need access) so I can dedicate the remote's keys to Terminal and this script, and run Terminal with a huge font. In iTunes I have a key that brings Terminal (with the infinite running script) to the front.

This script can easily be extended to work with other keys for other actions. It seems to be uncomfortable but works surprisingly well! Have fun!

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2006042803145949