With a little side of applesauce...

Thursday, February 25, 2010

iTunes - control from Terminal

I'm a console junkie, so the following alias and shell script tips are pretty cool:
http://www.macosxhints.com/article.php?story=20011108211802830

I use the shell script myself, since it is more fully-featured.


#!/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

3 comments:

BaTycoon said...

Im lost as to how to set this script up on my computer...

I am barely a novice at using the terminal, no less modifying it.

Could you help me as to how to set this for use on my computer?

Shannon Eric Peevey said...

Sorry, just found this comment! Here is a link that will explain the shell in great detail:
http://developer.apple.com/internet/opensource/opensourcescripting.html

Let me know if you have any other issues.

Joel said...

Great script. Any way of getting it to play different playlists ?

Like $itunes playlist Chillout etc

that would be cool

also a random list like djmix would be really cool.