Displaying iTunes Album Art and Song Information Using Geektools

In the third article on our series about geektools we will look into how we can display iTunes song information directly on our desktop. This article will have two scripts one which displays written track information and a second script which will display the album art for the currently playing track.

Track Information

Paste the following script into geektools:
if ps x | grep iTunes | grep -q -v grep;   then 
  osascript -e 'tell application "iTunes"
set trackname to name of current track
set artistname to artist of current track
set albumname to album of current track
if albumname is null then
set albumshow to " "
else if albumname is "" then
set albumshow to " "
set albumshow to "" & albumname & "" 
end if
set trackduration to duration of current track
set trackposition to player position
set elapsed to round (trackposition / trackduration * 100)
set myRating to round ((rating of current track) / 20)
if myRating is 1 then
set myRating to "* "
else if myRating is 2 then
set myRating to "** "
else if myRating is 3 then
set myRating to "*** "
else if myRating is 4 then
set myRating to "**** "
else if myRating is 5 then
set myRating to "***** "
set myRating to ""
end if
set myRating to myRating
set output to "" & trackname & "n" & artistname  & "n" & albumshow & "n" & myRating 
  end tell' | iconv -f utf-8 -t ucs-2-internal
This will show song title, artist name album name and the rating of the track that is currently playing. Set this to refresh every couple of seconds to keep it up to date with iTunes.

Album Art

Displaying album art is a little more complicated than displaying textual information. To do this we will update an image file which we will use in geektools then we will run a script periodically which changes the image to the current albumart.

The script:

-- Paths and stuff
set ArtworkFromiTunes to ((path to home folder) as text) & ¬
	"Pictures:iTunes Artwork:From iTunes:albumArt.pict" as alias
set iTunesArtwork to ((path to home folder) as text) & ¬
	"Pictures:iTunes Artwork:From iTunes:albumArt.pict"
set DefaultArtwork to ((path to home folder) as text) & ¬
	"Pictures:iTunes Artwork:Default:albumArt.pict"
set displayArtwork to ((path to home folder) as text) & ¬
	"Pictures:iTunes Artwork:albumArt.pict"

-- Unix versions of the above path strings
set unixITunesArtwork to the quoted form of POSIX path of iTunesArtwork
set unixDefaultArtwork to the quoted form of POSIX path of DefaultArtwork
set unixDisplayArtwork to the quoted form of POSIX path of displayArtwork

set whichArt to "blank"
tell application "System Events"
	if exists process "iTunes" then -- iTunes is running
		tell application "iTunes"
			if player state is playing then -- iTunes is playing
				set aLibrary to name of current playlist -- Name of Current Playlist
				set aTrack to current track
				set aTrackArtwork to null
				if (count of artwork of aTrack) ≥ 1 then -- there's an album cover
					"Running and playing and art"
					set aTrackArtwork to data of artwork 1 of aTrack
					set fileRef to ¬
						(open for access ArtworkFromiTunes with write permission)
						set eof fileRef to 512
						write aTrackArtwork to fileRef starting at 513
						close access fileRef
					on error errorMsg
							close access fileRef
						end try
						error errorMsg
					end try

					tell application "Finder" to ¬
						set creator type of ArtworkFromiTunes to "????"
					set whichArt to "iTunes"
				end if
			end if
		end tell
	end if
end tell

if whichArt is "iTunes" then
	do shell script "ditto -rsrc " & unixITunesArtwork & space & unixDisplayArtwork
	do shell script "ditto -rsrc " & unixDefaultArtwork & space & unixDisplayArtwork
end if
You will need to change the paths at the top of the file to where you want your images saved to. The next step is to create a geektool script widget which runs the following command:
osascript ~/Projects/Scripts/iTuntesArtwork2.scpt
I set this script to run every 10 seconds. The final stage is to add a geektools image widget thich points to the image file "albumArt.tif" that the script creates and of course get this to update regularly.

Next time we will discuss commands for displaying various system information.