2008/10/29
Automatic iTunes Lyric Fetching Script

I’ve looked around the web for an automated way of getting lyrics for the songs in my iTunes library. There are some widgets and some programs fetching lyrics, but they all seem to just do the currently playing song. I wanted something to take care of all my songs at once, so I whipped together a little Ruby script to do the work.

Thanks to valuable feedback from commenters bep, WZot and skeldoy, I’ve created an updated script. Check it out for better error handling and some more verbosity.

Note: This works only for OSX, the Windows crowd is unfortunately left in the cold on this one.

The script works thanks to the magnificent ruby gem rb-appscript, which takes care of the bindings to iTunes, and also the soap api at lyricwiki.org.

Just paste the script it into a text editor and save it as a rb file. Then select the songs you want to lyricify (probably not a word) in iTunes, and run the script in the terminal. It skips songs that already has lyrics attached.

Remember to install the gem first by entering the following command in Terminal:

sudo gem install rb-appscript

Here’s the script:

require 'rubygems'
require 'appscript'
require 'soap/wsdlDriver'
include Appscript
driver = SOAP::WSDLDriverFactory.new("http://lyricwiki.org/server.php?wsdl").create_rpc_driver

it = app('iTunes')

it.selection.get.each do |track|
  next unless track.lyrics.get == "Not found" or track.lyrics.get.empty?
  lyrics = driver.getSong(track.artist.get,track.name.get).lyrics
  if lyrics == "Not found"
    puts "Found nothing for: %s" % track.name.get
    next
  end
  track.lyrics.set(lyrics)
  track.refresh
  puts "Updated: %s" % track.name.get
end

That should do the trick. Feel free to alter and improve the program, and if you find it useful I’d be very grateful if you said so in the comments.

EDIT:

I’ve realized from some of the comments that I haven’t been completely clear on how to actually execute the script. A reader of mac1.no, KDN, posted this guide (loosely translated from Norwegian):

  1. Write “sudo gem install rb-appscript” i Terminal
  2. Copy the code into a text editor (Textedit works fine, or Textmate, or Coda) and save it as lyrics.rb on your desktop
  3. Type “cd desktop” in Terminal.
  4. Select the songs you want to find lyrics for in iTunes.
  5. Type “ruby lyrics.rb”. Wait for it to fetch lyrics.

EDIT 2 – A few more mentions (and also shortened the script a little bit):

16 Responses to “Automatic iTunes Lyric Fetching Script”

  1. WZot 2008/08/10 at 09:35

    Only problem I have encountered is when theres non-ascii chars in the song text (such as accented chars and the norwegian æøå. Also it doesn't fail very gracefully when it gets an error from that lyrics wiki…

    But hey, it's just a few lines of code. Great job anyways. Thanks a lot!:-)

    • Even 2008/08/10 at 12:32

      @WZot: Yeah, it seems LyricWiki is far from consistent when it comes to encodings used for their lyrics, so you'll find that while some of the norwegian lyrics work fine, others will have that characteristic å.

      I guess that wouldn't be too hard to add code for, but you're right in that this is only a small script I cooked up for myself and realized others might find useful. The code is pretty simple, and graceful failing would require way more time for testing etc. So I guess what I'm saying is caveat emptor – it's not foolproof but should be easy to fix if anyone runs into trouble.

      Thanks by the way!

    • Even 2008/08/10 at 12:52

      @WZot: According to the LyricWiki SOAP Talk page, people are having problems with the Ruby SOAP client in general when it comes to international and accentuated characters. Still, it could probably be sensed with a few lines of code, but now their server is down so there's nothing to do about it at the moment :)

  2. skeldoy 2008/08/10 at 21:08

    Routine crashes on reaching radio-stations/feeds, just so you know. Nice job and nice, concise code, man! :)

    • Even 2008/08/10 at 22:57

      @skeldoy: Yeah, the script is pretty unforgiving when it comes to unforeseen content. That's the main reason I chose to not do the whole library, but only the selected songs. Thanks for the feedback!

  3. skeldoy 2008/08/10 at 23:25

    Trying to peg a lyric to a n mpeg-4 video file also failes, just like the streams:
    lyrics.rb:10: undefined method `empty?' for :missing_value:Symbol (NoMethodError)
    from lyrics.rb:9:in `each'
    from lyrics.rb:9

    Is there a way of establishing whether there is an actual lyric property on a given track? If so, you could just pop that test before the other tests and movies and streams would automagically be skipped.

    Even so: 540 songs (of 1570) automagically tagged with the proper lyrics is not a bad feat.

  4. bep 2008/08/11 at 01:20

    About error handling.

    http://mac1.no/artikkel/7036/automatisk-henting-a...
    This is a discussion of this script (in Norwegian). I posted a Win-version of this one that goes through the complete library.

    I do not know too much about Ruby, but what I did to avoid these was:

    1. Check for "Kind". I assume there is a Kind-property also in the OSX interface, so in the loop i put:
    next if track.Kind == 3 or track.KindAsString.rindex("video")

    2. Wrap the problematic parts in a begin/rescue/end-block.

    Thanks for this inspiring script. It was really easy to port to Windows.

    • skeldoy 2008/08/11 at 04:32

      Thanks bep! Got it working properly: Just added

      next if track.kind.get != "MPEG audio file"

      ..Before trying to access the lyrics-property.
      Only problem now is to get the UTF-8 characters
      to work on the mac.

  5. Even 2008/08/11 at 06:38

    Thanks for all your feedback guys. I've created an updated version of the script incorporating your pointers.

    You'll notice I've used track.lyrics.get == :missing_value instead of track.kind.get != “MPEG audio file” so that the script works on AAC music from the iTunes Store too.

    Again thanks!

  6. Kjetil 2008/10/04 at 23:36

    Hello,

    When I write in "ruby lyrics.rb" I get the following message when using the script in "take 2" :

    MacBook:desktop kjetilstre$ ruby lyrics.rb
    lyrics.rb:1: syntax error, unexpected $undefined, expecting '}'
    {
    tf1ansiansicpg1252cocoartf949cocoasubrtf350

    Can you please help me solve this. Am a novice…

    Thanks.

  7. AJ 2008/12/29 at 12:52

    When i put the sudo gem install rb-appscript into Terminal he asks me for my password, i try to write it in but i cant write any thing so i press ENTER and he says password incorrect… Some help ? I use an 14" iBook G4 with Tiger 10.4.11.

    • Even 2008/12/29 at 17:56

      Hey AJ,

      That's how you type in passwords in Terminal – you don't get a visual cue at all so just type your password and then press Enter. A small caveat though: If you're completely unfamiliar with the Terminal I'd be a little bit cautious as you can do a lot of damage from there if you do something wrong. If you just follow the recipe you should be fine.

  8. Bob 2009/02/18 at 23:40

    This seems to work great, although I did have errors at first that looked like:
    lyrics.rb:5: Invalid char `302' in expression
    lyrics.rb:5: Invalid char `240' in expression

    I noticed that the line numbers in the error were all blank lines and found extra blank characters on each one. Just deleted the blanks and away it went.

  9. torf 2009/04/16 at 14:02

    I get the same error message when I have selected the songs in itunes and executed the last command: lyrics.rb:1: syntax error, unexpected $undefined, expecting '}' {
    tf1ansiansicpg1252cocoartf949cocoasubrtf430 ^ I'm a newbie on terminal and scripts, so I don't have a clue on what to do.. can someone help, please?

  10. Torf 2009/04/16 at 20:21

    problem solved! I had pasted the script in textedit, and it seems that this was the problem. when I pasted it in xcode it worked fine. had to restart the lyrics fetching-process a couple of times, but this might have been because of bad file or wrong types of files.. thanks for sharing the script!

  11. Lars 2010/04/25 at 16:58

    lyrics.rb:9: Could not connect to LyricWiki (RuntimeError)

Leave a Reply