Skip to content

Instantly share code, notes, and snippets.

@luckymike
Forked from rkumar/itunes.sh
Created December 8, 2012 18:01
Show Gist options
  • Save luckymike/4241181 to your computer and use it in GitHub Desktop.
Save luckymike/4241181 to your computer and use it in GitHub Desktop.
control iTunes from command line
#!/bin/sh
#
#########################################
# iTunes Command Line Control v1.1
# written by David Schlosnagle
# created 2001.11.08
# edit 2010.06.01 rahul kumar
# edit 2012.12.08 Michael F. Weinberg
#########################################
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.";
echo " rate = Set rating for current track to # [0-5]";
echo " playlist = Show playlists saved in iTunes.";
echo " tracks = Show tracks for current or given playlist.";
}
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'`;
rating=`osascript -e 'tell application "iTunes" to get rating of current track'`;
stars=$(( $rating/20 ))
echo "Current track $artist: $track - $stars stars.";
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 ;;
"rate" ) echo "Setting track rating to $2 stars.";
if [ $2 -ge 0 ] && [ $2 -le 5 ]; then
rating=$(( $2*20))
osascript -e "tell application \"iTunes\" to set rating of current track to $rating";
else
echo "Rating must be 0-5.";
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 ;;
## addition playlist of choice
"playlist" )
if [ -n "$2" ]; then
echo "Changing iTunes playlists to '$2'.";
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell";
break ;
else
# Show available iTunes playlists.
echo "Playlists:";
playlists=`osascript -e 'tell application "iTunes"' -e "set allPlaylists to (get name of every playlist)" -e "end tell"`;
echo $playlists| tr ',' '\n';
break;
fi
break;;
"tracks" )
if [ -n "$2" ]; then
tracks=`osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e " get name of every track in playlist new_playlist" -e "end tell"`;
echo $tracks | tr ',' '\n'
break;
fi
tracks=`osascript -e 'tell application "iTunes" to get name of every track in current playlist'`;
echo $tracks | tr ',' '\n'
break ;;
"help" | * ) echo "help:";
showHelp;
break ;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment