Skip to Content

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.

  1. Launch Terminal.
  2. In Terminal, enter: touch nowplaying
  3. In Terminal, enter: open -e nowplaying
  4. 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'
  5. Save the file in TextEdit and close it.
  6. In Terminal, enter: chmod 755 nowplaying
  7. Open iTunes and start playing a song.
  8. 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

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" & ""))'



Have you ever wondered whether there was a command line way to check iTunes and determine which song is currently playing. The command-line...
 

Add a Comment

*0 / 3000 Character Maximum

20 Comments

Filter by:
I,Robot

I've got a much easier way of finding out what song is playing on iTunes -- Try listening to it.

April 04 2007 at 5:05 AM Report abuse rate up rate down Reply
Zachary Hinchliffe

Now. Is there a way to make it so a hotkey will paste that info into the current application?

April 03 2007 at 4:11 PM Report abuse rate up rate down Reply
Dave

Erica, you rock so hard. Thank you, thank you, thank you.

April 02 2007 at 10:55 PM Report abuse rate up rate down Reply
Matt

I stand corrected. This is the greatest thing since the Lisa. ;) Sorry, my geek sensibilities left me for a while there.

April 02 2007 at 8:48 PM Report abuse rate up rate down Reply
kenji

I have many songs that have unicode characters (japanese), and they do now show properly when using this. how do i enable it?

April 02 2007 at 6:12 PM Report abuse rate up rate down Reply
Dr. Drang

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.

April 02 2007 at 5:17 PM Report abuse rate up rate down Reply
Dr. Drang

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.

April 02 2007 at 5:13 PM Report abuse rate up rate down Reply
seth

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/

April 02 2007 at 4:36 PM Report abuse rate up rate down Reply
Josh

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.

April 02 2007 at 4:05 PM Report abuse rate up rate down Reply
Rattboi

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.

April 02 2007 at 4:01 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.