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
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" & ""))'
Share
Categories
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
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 Permalink rate up rate down ReplyNow. 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 Permalink rate up rate down ReplyErica, you rock so hard. Thank you, thank you, thank you.
April 02 2007 at 10:55 PM Report abuse Permalink rate up rate down ReplyI 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 Permalink rate up rate down ReplyI 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 Permalink rate up rate down ReplyOK, 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.
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.
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/
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 Permalink rate up rate down Replythat 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 Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Altec Lansing Octiv Duo iDock for $48 + free shipping
- Used Apple iMac 17" Core 2 Duo 1.83GHz for $430 + $28 s&h
- Lounge Deluxe Stand for iPhone / iPod touch for $28 + $8 s&h
- Brookstone Surround-Sound Earbuds for $14 + $7 s&h
- Refurbished Skullcandy Tokidoki Smokin' Buds Mic'd Headset for $5 + $2 s&h
- Stitchway Backup Battery for iPod / iPhone for $5 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



20 Comments