Controlling iTunes from the Terminal

Nov 08, '01 09:18:02PM

Contributed by: sekalreed

[Editor's addition: I received two different 'iTunes command line' hint submissions at nearly the same time. In the interest of ease of use in the future, I'm combining them in this one posting.

The first 'iTunes terminal script' tip, submitted by Stu G., reads as follows:

"I come from a more unix background and have always controlled xmms (a mp3 player) from the command line, usually remotely. To my knowledge, iTunes alone does not do this. So using the new scriptability of version 2.01 and the 'osascript' command, I tossed together some quick aliases I use with my tcsh:

alias next      osascript -e 'tell application "iTunes"'[space]
-e "next track" -e "end tell"
alias prev osascript -e 'tell application "iTunes"'[space]
-e "previous track" -e "end tell"
alias pause osascript -e 'tell application "iTunes"'[space]
-e "pause" -e "end tell"
[NOTE: Enter as one line each, replace [space] with the actual space character]. Stop and play, etc are just as easy to create. What I think would be nice, is if some one takes the scripting of iTunes and smashes these controls into say the Services menu, then there would be semi-global keyboard control of iTunes."

The second tip, submitted by the author of this article, David S., is more of a full command line interface for iTunes. I've installed it on my machine, and it's very nicely done. The rest of this tip is his text... -rob.]



Now that iTunes has built-in AppleScript support, everyone is making AppleScripts to control it. I felt left out since I haven't found a script to control it from the command line. This little shell script I made should fill that void. I have put some of iTunes features I use most into it.

If you're interested in controlling iTunes from the Terminal, read the rest of the article...

Create the following script using either a Terminal text editor such as Pico or vi, or use TextEdit/BBEdit and make sure you save the fle as pure text.

#!/bin/sh
#
####################################
# iTunes Command Line Control v1.0
# written by David Schlosnagle
# created 2001.11.08
####################################

showHelp () {
echo "-----------------------------";
echo "iTunes Command Line Interface";
echo "-----------------------------";
echo "Usage: `basename $0` <option>";
echo;
echo "Options:";
echo " status = Shows iTunes' status, current artist and track.";
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 " stop = Stop iTunes.";
echo " quit = Quit iTunes.";
}

if [ $# = 0 ]; then
showHelp;
fi

while [ $# -gt 0 ]; do
arg=$1;
case $arg in
"status" ) state=`osascript -e 'tell application "iTunes" to player state as string'`;
echo "iTunes is currently $state.";
if [ $state = "playing" ]; then
artist=`osascript -e 'tell application "iTunes" to artist of current track as string'`;
track=`osascript -e 'tell application "iTunes" to name of current track as string'`;
echo "Current track $artist: $track";
fi
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.";
vol=`osascript -e 'tell application "iTunes" to sound volume as integer'`;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
fi

if [ $2 = "down" ]; then
newvol=$(( vol-10 ));
fi

if [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
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 ;;

"help" | * ) echo "help:";
showHelp;
break ;;
esac
done
To use the file, save it somewhere in your $path. Then 'chmod 755 filename' to make it executable. Type 'rehash'. Then you can start using it. Personally mine is named 'itunes', so to start playing I use the command:
itunes play
If you just type "itunes" it will list the options. Currently the options are:
status   = Shows iTunes' status, current artist and track.
play = Start playing iTunes.
pause = Pause iTunes.
next = Go to the next track.
prev = Go to the previous track.
mute = Mute iTunes' volume.
unmute = Unmute iTunes' volume.
vol up = Increase iTunes' volume by 10%
vol down = Increase iTunes' volume by 10%
vol # = Set iTunes' volume to # [0-100]
stop = Stop iTunes.
quit = Quit iTunes.
If anyone has any suggestions, I'd love to hear them. Especially if there is a way to speed these AppleScript commands up.

-David Schlosnagle
http://www.david-s.net

Comments (35)


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