Filed under: How-tos, iTunes, Terminal Tips
Terminal Tip: "Now Playing" info from the command line
Have you ever wondered whether there was a command line way to check iTunes and determine which song is currently playing. The command-line osascript utility makes this an easy problem to solve. Osascript executes AppleScript commands from Terminal's command line and can run many of the same kinds of simple scripts you'd normally run in Script Editor. Here's a quick how-to to build a Now Playing command line utility. - Launch Terminal.
- In Terminal, enter:
touch nowplaying - In Terminal, enter:
open -e nowplaying - In TextEdit, paste the following into nowplaying. The second line wraps here but should copy and paste correctly--at least it did when I tried it out.
#! /bin/bash
osascript -e 'tell application "iTunes" to if player state is playing then "Now Playing: " & name of current track & " - " & artist of current track' - Save the file in TextEdit and close it.
- In Terminal, enter:
chmod 755 nowplaying - Open iTunes and start playing a song.
- In Terminal, enter:
./nowplaying
The script, when run, outputs the track name and artist.
% ./nowplaying
Now Playing: Everything I've Got - Iain Archer
%
Thanks Dave M
Gallery: Adding "Now Playing" to your desktop
Update: For those of you who don't want to keep iTunes open:
#! /bin/bash
osascript -e 'tell application "System Events" to if ((name of processes) contains "iTunes") then do shell script ("osascript -e " & quoted form of ("tell application \"iTunes\" to if player state is playing then \"Now Playing: \" & name of current track & \" - \" & artist of current track" & ""))'

Reader Comments (Page 1 of 1)
John from Buffalo said 2:52PM on 4-02-2007
Now THAT is a sweet damn hack.
Reply
Nik said 3:09PM on 4-02-2007
Matt,
I promise to use it more than twice, if you promise to pay me cash money.
Reply
Matt said 3:12PM on 4-02-2007
I understand the allure of the command line, but I will pay cash money to the first person who can explain why this is superior to pressing Command-Tab and looking for yourself. Or even to anyone who promises to use it more than twice.
Still, yeah, kind of neat.
Reply
Erica Sadun said 3:14PM on 4-02-2007
I used it more than twice while writing and debugging it. Does that mean I win something?
Anyway, Dave M who asked how to do this wants to use it with Geek Tool.
Reply
Rob said 3:15PM on 4-02-2007
I thought this wouldn't be useful at first but I have actually been using quite a bit, just click to terminal and press up arrow, enter
Reply
Sovok said 3:16PM on 4-02-2007
Matt: You can for example take just the osascript... line, put it into GeekTool and have your current tracks name on your desktop. Then quickly push your windows by side via Exposé and you see whats playing. No unnecessary pointing and clicking :>
Reply
dojan said 3:20PM on 4-02-2007
can't get this to work :( I'm looking for something similar to this, for use with irssi, anyone who can point me in the right direction?
Reply
Hannes Petri said 3:27PM on 4-02-2007
No, that script isn't too useful, but it demonstrates the capabilities in scripting applications from command line. A maybe more useful example would be:
#!/bin/bash
r=$1*20;
osascript -e "tell application \"iTunes\" to if player state is playing then set rating of current track to $r;
which sets the rating of the current track, using for example './setrating 4'.
You could even create a directory, in which you stores useful scripts, such as these. You can then put that directory in you path, so that you can call it from anywhere.
Reply
ossiejnr said 3:41PM on 4-02-2007
Matt,
This is useful if for example you're not near your machine that's playing the music and you're accessing it remotely.
I use this script which was written by David Schlosnagle. It provides a more full command line utility for iTunes.
#!/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` ";
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
Reply
bruce robertso said 3:59PM on 4-02-2007
Can't get it to work - no result. If osascript portion attempted from command line I get:
[iMac24:~] bruce% osascript -e 'tell application \"iTunes\" to name of current track'
2007-04-02 12:47:37.546 osascript[23574] CFLog (21): dyld returns 2 when trying to load /Library/ScriptingAdditions/ConvexPoly.osax/Contents/MacOS/ConvexPoly
2007-04-02 12:47:37.549 osascript[23574] CFLog (21): dyld returns 2 when trying to load /Library/ScriptingAdditions/dialModemOSAX.osax/Contents/MacOS/dialModemOSAX
2007-04-02 12:47:37.564 osascript[23574] CFLog (21): dyld returns 2 when trying to load /Library/ScriptingAdditions/Resampler.osax/Contents/MacOS/Resampler
etc
Reply
Rattboi said 4:04PM on 4-02-2007
that tunes script mentioned above is what I based my script off of. I used Nicecast to stream radio and SSH to control it. I don't have a website, but if someone has a place to recommend, I'll package it up. My script(s) supports basic playlist creation based on keywords too, and it's really easy to use.
Reply
Josh said 4:06PM on 4-02-2007
Also, put any of these in the /usr/local/bin directory and you won't have to type "./nowplaying" (or be in a certain directory) to use it.
Reply
seth said 4:37PM on 4-02-2007
I use something similar with GeekTool, I run a rather minimalistic desktop relying souly on GeekTool and Quicksilver for input and output of data. Here's a screenshot of my current setup:
http://www.flickr.com/photos/smesser/437188260/
Reply
Dr. Drang said 5:14PM on 4-02-2007
I’m going to cross my fingers that TUAW comments will accept HTML…
As Sovok said in comment #6, this sort of utility works very well when invoked by GeekTool. One problem with using it from GeekTool is that if iTunes isn’t running, this script will start it—probably not what you want.
The way to avoid this is to first check whether iTunes is running using the ‘ps’ command. Last year I wrote a pair of scripts (one AppleScript, one shell) that work together to do this checking and print the appropriate info. The output is either
“iTunes off,”
“Not playing,” or
the track name, artist, and album
depending on what’s going on. The AppleScript (~/bin/itunes-playing.scpt) is
set notify to "Not playing"
tell application "iTunes"
if player state is playing then
set who to artist of current track as string
set what to name of current track as string
set onwhat to album of current track as string
set notify to "\"" & what & "\" by " & who & " from " & onwhat
end if
end tell
notify
The shell script (~/bin/playing) is
#!/bin/bash
if [[ -n `ps x | grep "iTunes -psn" | grep -v grep` ]]; then
osascript ~/bin/itunes-playing.scpt
else
echo "iTunes off"
fi
I wrote a more complete description, including the GeekTool setup, here.
Reply
Dr. Drang said 5:19PM on 4-02-2007
OK, the HTML was only a partial success. The formatting of the source code was messed up an the URLs for GeekTool:
http://projects.tynsoe.org/en/geektool/
and my writeup
http://www.leancrew.com/all-this/2006/07/off_track.html
were lost. Now I know to go text-only at TUAW.
Reply
kenji said 6:44PM on 4-02-2007
I have many songs that have unicode characters (japanese), and they do now show properly when using this. how do i enable it?
Reply
Matt said 8:49PM on 4-02-2007
I stand corrected. This is the greatest thing since the Lisa. ;) Sorry, my geek sensibilities left me for a while there.
Reply
Dave McAuley said 11:55PM on 4-02-2007
Erica, you rock so hard. Thank you, thank you, thank you.
Reply
Zachary Hinchliffe said 4:11PM on 4-03-2007
Now. Is there a way to make it so a hotkey will paste that info into the current application?
Reply
c.Lake said 5:06AM on 4-04-2007
I've got a much easier way of finding out what song is playing on iTunes -- Try listening to it.
Reply