My stars

Let me start this post by saying that I hate AppleScript. AppleScript is almost Satanic in how it tempts you. The example scripts you read always seem so clear, but as soon as you start writing your own you find yourself lost in a Hell of inscrutable data types and inadequate documentation. Some of the problem, of course, is that AppleScript is a glue language and each application gets to choose its own commands, elements, and properties. But there’s something wrong with AppleScript itself. I’ve written programs in a dozen or so languages over the years, and it’s this language for non-programmers that has always given me the most trouble.

(An unexpected benefit of moving from BBEdit to TextMate is that TextMate’s AppleScript support is almost non-existent. No temptation!)

Sometimes, however, AppleScript is unavoidable. Today, I decided to revamp one of my iTunes playlists and had to use AppleScript to do it. It went pretty smoothly, which makes me suspicious. Here’s an outline of what I wanted and how I got it.

I have in my iTunes Library many songs that I don’t want to get rid of but typically don’t want to listen to. Usually, these my wife’s, my daughter’s, or my son’s, but sometimes they’re bad tracks of mine. I don’t want to get rid of the tracks because I own them and went to the trouble of ripping them. And I probably do want to hear them if I’m listening to the CD in order. But for everyday shuffle playing? No.

I have a smart playlist that starts with my whole library, but eliminates

Many of my bad tracks show up in this list, and before today I simply skipped past them when they started playing. Unfortunately, this means that their “Last Played” field doesn’t get updated and they can come back around faster than the tracks I do like.

Ratings seem the obvious solution to this problem, but I’ve never rated any of my 6000-7000 songs and can’t imagine going through all of them to do so. What finally realized today was that I didn’t have to give legitimate ratings to all of my songs. Instead, I could:

  1. Select all the tracks and choose a value from the My Rating item in the File menu, thus giving a baseline rating to all the tracks in my library. I chose three stars as the baseline.
  2. Adjust the smart playlist to only include songs with ratings at or above that baseline.
  3. Reduce the rating of the bad tracks as I encountered them in the playlist.

The new form of the smart playlist looks like this

(Click on the screenshot to see it at full size.)

Step 3 is where the AppleScript comes in. Here’s the script that downgrades the rating of the current track and skips to the next:

tell application "iTunes"
  set thisTrack to current track
  set thisRating to rating of thisTrack
  set thisRating to thisRating - 20
  set thisTrack's rating to thisRating
  next track
end tell

I call it “Downgrade and Skip,” and it’s saved in ~/Library/iTunes/Scripts, which means it shows up in the iTunes AppleScript menu. As I said at the beginning of the post, AppleScripts are often very easy to read, so commentary is probably unnecessary except for the line where the rating is reduced by 20. The iTunes ratings system uses a 100-point scale internally, with each 20 points representing one star in displayed rating. So subtracting 20 from the rating is the same as reducing it by one star.

Having this script available from an iTunes menu is all very well and good, but I don’t usually have iTunes running as the top application, so to get at its menus I have to interrupt my work and bring it to the top--certainly not what I want. Here’s where Quicksilver comes to the rescue.

I added ~/Library/iTunes/Scripts to Quicksilver’s catalog, so QS would know about “Downgrade and Skip.” I then made a QS trigger to run “Downgrade and Skip” and bound it to Control-Option-Command-Downarrow. The trigger pane of QS’s preferences looks like this

where you can also see my other iTunes triggers.

The awkwardness of this key combo doesn’t bother me because I have a Logitech keyboard at work with all those multimedia/program launcher keys above the line of function keys. I used the Logitech Preferences Pane to bind one of those keys to Control-Option-Command-Downarrow, and now a single keypress causes the AppleScript to run.

The result of all this is a playlist that will gradually eliminate the songs I don’t want to hear. As I listen, when a song comes on that I don’t like, I hit the button bound to the “Downgrade and Skip” command. The song’s rating is reduced to two stars, removing it from the playlist, and the next song starts up. I’m guessing that all the bad tracks will be downgraded within a month or so.

Update
I added the screenshots on 4/20 using my nifty screenshot utility.

Tags: