• Home
  • Blog
  • Projects
  • Contact
  • Rss
  • Twitter
  • Github_logo
8bitscraps

Displaying iTunes Album Art and Song Information Using Geektools

Posted on Apr 14
By ghadgraft
Filed in 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:
#!/bin/sh
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 " "
else 
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 "***** "
else
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
fi
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)
					try
						set eof fileRef to 512
						write aTrackArtwork to fileRef starting at 513
						close access fileRef
					on error errorMsg
						try
							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
else
	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.

Creating App Docs When depl... » Blog Home « Displaying Date and Time To...

Comments

rprebel

I tried pasting the first script into the Applescript Editor, but it gave me an error on the second line. It's calling the x in "if ps x" an "identifier" and won't compile. I also tried pasting the code directly into Geektools in Shell->Command, but that didn't work either. Am I doing something wrong?

Love the site.

Posted by rprebel, about 3 years ago

Graham

I took a look at my suggestion and found that wordpress had changed some charachters in the code. Its been updated and now works so please try again. You need to paste this into the script in geektools as it is a bash script and not an applescript.

Im glad you enjoying the site. I have 2 more articles coming up regarding Geektools, the first will cover displaying weather information and the second will be about displaying google analytics information on your desktop.

Posted by Graham, about 3 years ago

rprebel

Works now, looks great! It looks almost exactly like how I have Bowtie configured, which means I'm about ready to get rid of Bowtie. Thanks!

Posted by rprebel, about 3 years ago

rprebel

I've noticed that, when iTunes is playing something that has a special character (an umlaut for example) in the title, the script doesn't display it onscreen. It doesn't update, leaving the info for the previous track on the screen. Thoughts?

Posted by rprebel, about 3 years ago

Graham

Thanks for pointing that out rprebel. It is not something i had noticed, i have very few tracks with special charachters in the titles. Im looking into the cause of it nbot updating and will update the script when i have a solution.

Posted by Graham, about 3 years ago

Graham

rprebel, i have a solution remove the text '| iconv -f utf-8 -t ucs-2-internal' from your script. I suspect an issue with charachter encoding and geektools. removing this text seems to display correctly.

Posted by Graham, about 3 years ago

rprebel

Graham, you're a gentleman and a scholar. I don't care what they say about you.

Posted by rprebel, about 3 years ago

Sm

I can not seem to get the iTunes Album Artwork to display. I have tried pasting the script you have listed here in the script editor to examine it but when I try run it it gives me "Expected end of line, etc. but found unknown token." it is referencing the ";" at the end of this line"set ArtworkFromiTunes to ((path to home folder) as text) &"

I have also tried entering this directly in geektools shell>>console and have no luck there.

Any idea's how to fix this?

I have been able to get the textual information to work though.

Posted by Sm, over 2 years ago

Make a Comment

Other Posts

  • Creating App Docs When deploying with Capistrano
  • Sending Emails When Exceptions Happen using exception_notification
  • Adding README to Gitorious repository page (like github)
  • Serialise a ruby hash into a database field
  • Finding the process ID of a process running on a specific port
  • Using Tinymce with Interchange
  • Interchange Not Starting - No Sockets INTERCHANGE SERVER TERMINATING
  • Displaying Hardware Temperatures With Macbook
  • Creating vcards with ruby on rails
  • Creating Columns Of Equal Heights Using Divs

Categories

  • Ruby On Rails (19)
  • Ruby (3)
  • Titanium Appcelerator (0)
  • Servers (Apache, Mongrel) (3)
  • Interchange (9)
  • Perl (3)
  • SCM (SVN, GIT) (3)
  • Server Administration (6)
  • PHP (1)
  • Regular Expressions (1)
  • SQL (2)
  • Geektools (10)
  • CSS (1)

Archives

  • October 2011 (1)
  • August 2011 (1)
  • 2010 (24)
  • 2009 (2)
  • 2008 (25)

8 bit scraps has been created by Graham Hadgraft a web developer from Ipswich. Graham Hadgraft has 10 Years programming experience in various languages.

The aim of 8 bit scraps is to provide programming tips for Ruby on Rails, PHP, Perl and any other programming info that people may find useful. As well as reviews of programming resources such as books, software and websites.

Why not follow 8-Bit Scraps with

  • RSS
  • Twitter
  • Github
  • Wordpress Blog

Copyright © 2013 Graham Hadgraft