July 24, 2009 at 11:50 AM by Dr. Drang
This is something of a followup to the post on Monday in which I presented a set of scripts for recording the streams of BBC Radio 2 music shows and putting their track lists into the lyrics fields of the recordings. This makes it easy to see which song is playing as I listen to a show on my iPod1
Oddly enough, it’s not as easy to look up the tracklist when I’m listening to a show on my computer. Since I normally keep iTunes hidden, I have to
- Activate iTunes, through either the Dock or ⌘-Tab
- Make the track playing visible in the main iTunes window through ⌘-L.
- Bring up the Info window for the track through ⌘-I.
- Click on the Lyrics tab of the Info window (if it’s not already selected).
- Close the Info window and hide iTunes again when I’m done looking.
This is a pain. I can eliminate some of the work with this AppleScript:
1: tell application "iTunes" 2: activate 3: tell application "System Events" 4: keystroke "l" using command down 5: keystroke "i" using command down 6: end tell 7: end tell
Saving this in
~/Library/Scripts, I can use FastScripts to invoke it with a single keystroke combination. That reduces the first three steps to one, but it still leaves one mouseclick (maybe) to see the tracklist and a couple of keystroke combos to put everything away. I can do better than that.
The solution is a Python script, called “Current Lyrics,” that’s kept in
~/Library/Scripts. It uses Carsten Blüm’s Pashua for its GUI and appscript to communicate with iTunes. When I invoke it via FastScripts with the F14 keystroke, up pops this window:
When I’m done looking, I dismiss it with ⌘-W. Two keystrokes (and possibly some scrolling, which I’d also have to do with the AppleScript solution) instead of five.
Here’s the code:
1: #!/usr/bin/python 2: 3: import Pashua 4: import appscript 5: 6: # Get the lyrics. 7: track = appscript.app('iTunes').current_track 8: lyrics = track.lyrics.get().replace('\r', '[return]').encode('macroman') 9: title = track.name.get().encode('macroman') 10: artist = track.artist.get().encode('macroman') 11: 12: 13: # Dialog box configuration 14: conf = ''' 15: # Window properties 16: *.title = Current track 17: 18: # Track info. 19: ti.type = text 20: ti.default = %s 21: ti.disabled = 1 22: ti.x = 0 23: ti.y = 15 24: ar.type = text 25: ar.default = by %s 26: ar.x = 0 27: ar.y = 0 28: 29: # Lyrics text box properties 30: pl.type = textbox 31: pl.default = %s 32: pl.width = 400 33: pl.height = 300 34: pl.x = 0 35: pl.y = 40 36: ''' % (title, artist, lyrics) 37: 38: # Open the dialog box and show the lyrics. 39: dialog = Pashua.run(conf)
Everything in Lines 13–36 is window configuration, which is described in the documentation that comes with the Pashua download. The only item worth mentioning is Line 21, which is supposed to make the text box with the lyrics uneditable. Unfortunately, it doesn’t work, which is why I can’t just hit the Return key to dismiss the window and have to use ⌘-W instead. Hitting Return just adds new lines to the bottom of the text in the box.
encode('macroman') calls at the ends of Lines 8–10 surprised me. I originally wrote those lines with
encode('utf-8'), but that resulted in garbage when there were characters outside the ASCII range. Apparently, iTunes still uses the old MacRoman encoding, which is a bit surprising nowadays.
I suppose I really should learn PyObjC if I’m going to keep writing Python scripts with GUI components. Pashua is really easy to use, but it hasn’t been updated in ages, and I’m worried that it’s going stop working in some future OS X version.
Your iPod screen doesn’t look that bright in sunlight? Neither does mine. I photoshopped it, with a screen photographed indoors pasted over the nano photographed outdoors. ↩