I have a large collection of music, all of which is (finally) in iTunes.  I'd like to rate all of it but it's somewhat cumbersome to flip back and forth from whatever app I'm in to iTunes in order to click on the little star icons while I'm listening to music.

I was thinking about this the other day when I noticed that my keyboard has five "My Favorites" buttons at the top of it that I've never, ever used:

 

I started thinking -- there are five star ratings in iTunes and there are five buttons, which seemed like a convenient coincidence.

ITunes can be scripted in Windows via a COM object (see here for an excellent tutorial on it).  I figured I needed to do two things in script -- find the currently playing song and change the rating on it.

To start with, I needed to instantiate the iTunes COM object:

' Connect to iTunes app
Set iTunes = CreateObject("iTunes.Application")

After that, I get the currently playing song from the iTunes object:

' Get the currently playing song
Set PlayingSong = iTunes.CurrentTrack

Finally, set the rating on PlayingSong:

' Set the rating of our song
PlayingSong.Rating = Rating

(Rating is being passed into the script as a command-line parameter)

I found that there were a few more things I needed to do to get this into useable shape.  First of all, if iTunes wasn't running, instantiating the COM object caused iTunes to start -- which was probably not the behavior I wanted.  So, I query WMI to make sure that iTunes is running before proceeding.

Second, when setting the rating, iTunes takes a value of 0-100 that represents unrated (value==0) and the range of one star (value==20) through five starts (value==100).  So, I had to check the command line parameter to make sure it was between 1 and 5, then multiply the value by 20. 

The entire script is at the end of this entry although, as with anything here, this is presented as sample code only.  Use it at your own risk -- I haven't tested it extensively and there are definitely cases that it misses.

Once I had the script done, all I had to do was to go to the settings for these buttons and to set the button to launch the script like this:

Button 1: "c:\scripts\iTunes Rating.vbs" 1
Button 2: "c:\scripts\iTunes Rating.vbs" 2
Button 3: "c:\scripts\iTunes Rating.vbs" 3
Button 4: "c:\scripts\iTunes Rating.vbs" 4
Button 5: "c:\scripts\iTunes Rating.vbs" 5

Now, I can use the buttons to rate my music while I'm working on other things. 

Entire sample script:

' iTunes Rating.vbs
' Sample Script for setting rating
' on currently playing song

' First, need to check if iTunes is running.
' Otherwise, it starts automatically when
' we create the COM object.

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & "." & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery( _
    "Select * from Win32_Process " _
    & "Where Name = 'iTunes.exe'")

If (colProcesses.Count > 0) Then

    ' iTunes is running so let's do this

    Dim iTunes, PlayingSong, Rating

    ' Default to clearing the rating if no parameters
    ' were passed

    Rating = 0

    ' Do simple bounds checking on the parameter and
    ' convert from star rating (0-5) to percentile (0-100)

    If (Wscript.Arguments.Unnamed.Count = 1) _
      And (Wscript.Arguments.Unnamed.Item(0) _
      >= 0) And (Wscript.Arguments.Unnamed.Item(0) _
      <= 5) Then

        Rating = Wscript.Arguments.Unnamed.Item(0) * 20

    End If

    ' Connect to iTunes app
    Set iTunes = CreateObject("iTunes.Application")

    ' Get the currently playing song
    Set PlayingSong = iTunes.CurrentTrack

    ' Set the rating of our song
    PlayingSong.Rating = Rating

    ' Done; release object
    set iTunes = nothing

End If