<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon Todd &#187; terminal</title>
	<atom:link href="http://www.jontodd.com/tag/terminal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jontodd.com</link>
	<description>Optimal</description>
	<lastBuildDate>Tue, 03 Nov 2009 22:33:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How To Control iTunes remotely with SSH</title>
		<link>http://www.jontodd.com/2007/11/19/how-to-control-itunes-remotely-with-ssh/</link>
		<comments>http://www.jontodd.com/2007/11/19/how-to-control-itunes-remotely-with-ssh/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 15:56:00 +0000</pubDate>
		<dc:creator>Jon Todd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[iTunes]]></category>
		<category><![CDATA[remote]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://www.jontodd.com/2007/11/20/how-to-control-itunes-remotely-with-ssh/</guid>
		<description><![CDATA[After chatting with [ my buddy Dave ](http://mortysworld.blogspot.com) about how awesome it is to show off using terminal to !1337 computer users I realized how often I&#8217;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 ](http://www.macosxhints.com/article.php?story=20011108211802830) article on [...]]]></description>
			<content:encoded><![CDATA[<p>After chatting with [ my buddy Dave ](http://mortysworld.blogspot.com)<br />
about how awesome it is to show off using terminal to !1337 computer<br />
users I realized how often I&#8217;m sitting at command-line listening to music<br />
in iTunes and wondered how to control iTunes with a shell script.</p>
<p>I searched around an found [ this ](http://www.macosxhints.com/article.php?story=20011108211802830)<br />
article on the topic but of course wanted many tweaks of my own so I&#8217;ve<br />
wrote the following script that should allow anyone to control iTunes in terminal<br />
or via SSH remotely.</p>
<p>### Possible Uses ###<br />
The implications are pretty neat if you think about it. </p>
<ul>
<li>DJ a party from a laptop, iPhone, or mobile with SSH</li>
<li>Control your home or office music server remotely</li>
<li>Scare the heck out of your roommates</li>
</ul>
<p><span id="more-123"></span></p>
<p>### The Code ###</p>
<pre name="code" class="php">
#!/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`
<option>";
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
</pre>
<p>### Installation ###</p>
<p>Write this code to a file called &#8220;itunes&#8221; and put it in your bin directory</p>
<pre name="code" class='shell'>
mkdir ~/bin
mv pathToFile/itunes.sh ~/bin
chmod u+x ~/bin/itunes
</pre>
<p>Ensure ~/bin is in your path</p>
<p>### Run it ###</p>
<pre name="code" class='shell'>
[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.
</pre>
<p>### Remote Control ###</p>
<pre name="code" class='shell'>
[jtodd@remote:~]$ ssh local
[jtodd@local:~]$ itunes status
State: 		playing
Volume: 	100
Playlist: 	Combo
Shuffle: 	false
Artist: 	Jamiroquai
Album: 		synkronized
Track: 		Canned Heat
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jontodd.com/2007/11/19/how-to-control-itunes-remotely-with-ssh/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

