After chatting with my buddy Dave
about how awesome it is to show off using terminal to !1337 computer
users I realized how often I’m sitting at command-line listening to music
in iTunes and wondered how to control iTunes with a shell script.
I searched around an found this
article on the topic but of course wanted many tweaks of my own so I’ve
wrote the following script that should allow anyone to control iTunes in terminal
or via SSH remotely.
Possible Uses
The implications are pretty neat if you think about it.
DJ a party from a laptop, iPhone, or mobile with SSH
Control your home or office music server remotely
Scare the heck out of your roommates
The Code
#!/bin/sh
#
####################################
# iTunes Command Line Control v1.0
# written by David Schlosnagle
# created 2001.11.08
# edited by Jon Todd 2007.11.20
####################################
showHelp () {
echo "-----------------------------";
echo "iTunes Command Line Interface";
echo "-----------------------------";
echo "Usage: `basename $0` “;
echo;
echo “Options:”;
echo ” status = Current state of iTunes.”;
echo ” play = Start playing iTunes.”;
echo ” pause = Pause iTunes.”;
echo ” next = Go to the next track.”;
echo ” prev = Go to the previous track.”;
echo ” mute = Mute iTunes’ volume.”;
echo ” unmute = Unmute iTunes’ volume.”;
echo ” vol up = Increase iTunes’ volume by 10%”;
echo ” vol down = Increase iTunes’ volume by 10%”;
echo ” vol # = Set iTunes’ volume to # [0-100]”;
#echo ” playlist “@” = Play iTunes’ playlist named @”;
echo ” shuf = Shuffle current playlist”;
echo ” nosh = Do not shuffle current playlist”;
echo ” stop = Stop iTunes.”;
echo ” quit = Quit iTunes.”;
}
showError ()
{
echo ;
echo “ERROR:”;
echo $1;
}
if [ $# = 0 ]; then
showHelp;
fi
while [ $# -gt 0 ]; do
action=$1;
case $action in
“status” ) state=`osascript -e ‘tell application “iTunes” to player state as string’`;
# State
echo “State: \t\t$state”;
# Volume
vol=`osascript -e ‘tell application “iTunes” to sound volume as integer’`;
echo “Volume: \t$vol”;
# Playlist
playlist=`osascript -e ‘tell application “iTunes” to name of current playlist as string’`;
echo “Playlist: \t$playlist”;
# Shuffle
shuffle=`osascript -e ‘tell application “iTunes” to shuffle of current playlist as string’`;
echo “Shuffle: \t$shuffle”;
# Current Song
if [ $state = “playing” ]; then
# Artist
artist=`osascript -e ‘tell application “iTunes” to artist of current track as string’`;
echo “Artist: \t$artist”;
# Artist
album=`osascript -e ‘tell application “iTunes” to album of current track as string’`;
echo “Album: \t\t$album”;
# Track
track=`osascript -e ‘tell application “iTunes” to name of current track as string’`;
echo “Track: \t\t$track”;
fi
break ;;
# TODO: Figure out how to get this working
# “playlist” ) echo “Changing iTunes playlist.”;
# osascript -e ‘tell application \”iTunes\” to play playlist “Combo” ‘;
# break ;;
“play” ) echo “Playing iTunes.”;
osascript -e ‘tell application “iTunes” to play’;
break ;;
“pause” ) echo “Pausing iTunes.”;
osascript -e ‘tell application “iTunes” to pause’;
break ;;
“next” ) echo “Going to next track.” ;
osascript -e ‘tell application “iTunes” to next track’;
break ;;
“prev” ) echo “Going to previous track.”;
osascript -e ‘tell application “iTunes” to previous track’;
break ;;
“mute” ) echo “Muting iTunes volume level.”;
osascript -e ‘tell application “iTunes” to set mute to true’;
break ;;
“unmute” ) echo “Unmuting iTunes volume level.”;
osascript -e ‘tell application “iTunes” to set mute to false’;
break ;;
“vol” ) echo “Changing iTunes volume level.”;
if [ “$#” -gt 1 ]; then
vol=`osascript -e ‘tell application “iTunes” to sound volume as integer’`;
if [ $2 = “up” ]; then
newvol=$(( vol+10 ));
elif [ $2 = “down” ]; then
newvol=$(( vol-10 ));
elif [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e “tell application \”iTunes\” to set sound volume to $newvol”;
else
showError “No volume level specified”;
fi
break;;
“stop” ) echo “Stopping iTunes.”;
osascript -e ‘tell application “iTunes” to stop’;
break ;;
“quit” ) echo “Quitting iTunes.”;
osascript -e ‘tell application “iTunes” to quit’;
exit 1 ;;
“shuf” ) echo “Shuffle is ON.”;
osascript -e ‘tell application “iTunes” to set shuffle of current playlist to 1′;
break ;;
“nosh” ) echo “Shuffle is OFF.”;
osascript -e ‘tell application “iTunes” to set shuffle of current playlist to 0′;
break ;;
“help” | * ) echo “help:”;
showHelp;
break ;;
esac
done
Installation
Write this code to a file called “itunes” and put it in your bin directory
mkdir ~/bin
mv pathToFile/itunes.sh ~/bin
chmod u+x ~/bin/itunes
Ensure ~/bin is in your path
Run it
[jtodd@local:~]$ itunes status
State: stopped
Volume: 100
Playlist: Combo
Shuffle: false
[jtodd@local:~]$ itunes play
Playing iTunes.
[jtodd@local:~]$ itunes status
State: playing
Volume: 100
Playlist: Combo
Shuffle: false
Artist: Jamiroquai
Album: synkronized
Track: Canned Heat
[jtodd@local:~]$ itunes vol 50
Changing iTunes volume level.
[jtodd@local:~]$ itunes pause
Pausing iTunes.
[jtodd@local:~]$ itunes vol 100
Changing iTunes volume level.
[jtodd@local:~]$ itunes play
Playing iTunes.
Remote Control
[jtodd@remote:~]$ ssh local
[jtodd@local:~]$ itunes status
State: playing
Volume: 100
Playlist: Combo
Shuffle: false
Artist: Jamiroquai
Album: synkronized
Track: Canned Heat
Tags: command-line , Computers , iTunes , remote , ssh , terminal
This entry was posted
on Monday, November 19th, 2007 at 11:56 am and is filed under Code , Programming .
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response , or trackback from your own site.