Embedding iTunes track ratings in track comments

December 3, 2008

The iTunes software has served well, but it was time to switch to Amarok.  iTunes held all the rating information for my music collection, and that information needed to be migrated to Amarok.  The easy way to do that would have been be to use some of the existing tools.  As a challenge, I wrote a little Windows VB script to insert the iTunes ratings into the track comments, where they could be found and used by Amarok (or any other good music player).  Here’s it is …

In Windows:

  1. Open Notepad.
  2. Copy and paste the script text below into Notepad.
  3. Save the script somewhere.  Use a .vbs extension.  I called mine c:\temp\iTunes_stars_into_comments.vbs.
  4. Open iTunes
  5. Open a Windows command line.  I use Start / Run then enter ‘cmd’.
  6. Run the script using CScript.  I typed CScript c:\temp\iTunes_stars_into_comments.vbs

Then in Linux:

  1. Rescan the collection with Amarok, and it will notice all the new track comments.  Use Smart Playlists to group the tracks according to their comments (for example, make a smart playlist for the tracks with ‘2Star’ in their comments).  Select all those tracks, and rate them in bulk (for example, with 2 stars).  Repeat this process for all the other ratings.

You can probably guess that I’ve been using a dual-boot environment, with iTunes running under Windows Vista, and Amarok running under Ubuntu.  The music files are stored on an ext3 partition, accessible from Windows through Ext2 IFS For Windows. None of that affects the script, of course.

Here’s the script text.

'iTunes_stars_into_comments.vbs
'Run with: CScript iTunes_stars_into_comments.vbs (from an XP terminal window)
'Inspired by: http://www.microsoft.com/technet/scriptcenter/funzone/tunes.mspx
'License: This script is placed into the public domain.  Use it as you like, at your own risk.
'Purpose: To write iTunes track ratings into the track comments, so the info can be accessed in other music players (such as Amarok)
'Input: Nothing special, just iTunes
'Output: Additional text in track comments. Specifically, terms such as '5Star' or '0Star'.  Progress reported on screen.
'Modifications: Alter the big If..Then..ElseIf..Endif rating text block as required.  You could even handle half-star ratings that way
'Warning: If you use this script, then re-rate a track, then run the script again, you could have tracks with multiple rating comments such as "4Star 5Star".  That would be confusing.
'Run time: Takes a little less than 1 second per track on a Dell Inspiron 1720 laptop, ie, around one hour per 5000 tracks
'Setup
'----------------------------------------------
    'Initialise variables
    Set objApp = CreateObject("iTunes.Application")
    Set objLibrary = objApp.LibraryPlaylist
    Set colTracks = objLibrary.Tracks
    filesUpdated = 0
    filesProcessed = 0
'Walk through all the tracks in  the collection
'----------------------------------------------
    For Each objTrack In colTracks
        WScript.Echo "Processing: " & objTrack.Location
        WScript.Echo " - Existing rating : " & objTrack.Rating
        If Len(objTrack.Location) > 0 Then
            'This is probably a valid track, since there is a non-null filename for the track data
            If objTrack.Rating > 90 then
                'Consider this a 5Star track
                strStar = "5Star"
            ElseIf objTrack.Rating > 70 then
                'Consider this a 4Star track
                strStar = "4Star"
            ElseIf objTrack.Rating > 50 then
                'Consider this a 3Star track
                strStar = "3Star"
            ElseIf objTrack.Rating > 30 then
                'Consider this a 2Star track
                strStar = "2Star"
            ElseIf objTrack.Rating > 0 then
                'Consider this a 1Star track
                strStar = "1Star"
            Else
                'Note that unrated tracks report a rating of zero
                strStar = "0Star"
            End If
            WScript.Echo " - Required comment: " & strStar
            WScript.Echo " - Old comment     : " & strExistingComment
            strExistingComment = objTrack.Comment
            If InStr(strExistingComment, strStar) > 0 Then
                'Track already has the right comment.  No action required.
                WScript.Echo " - No change required"
            Else
                strNewComment = Trim(strExistingComment & " " & strStar)
                WScript.Echo " - New comment     : " & strNewComment
                'Update the comment in the track data
                objTrack.Comment = strNewComment
                filesUpdated = filesUpdated + 1
            End If
            WScript.Echo " "

            'Indicate progress
            filesProcessed = filesProcessed + 1
            If (filesProcessed mod 100 = 0) Then
                WScript.Echo "======================================================="
                WScript.Echo "Processed " & filesProcessed & " files so far."
                Wscript.Echo "Updated " & filesUpdated & " files so far."
                WScript.Echo "======================================================="
                WScript.Echo " "
            End If
        End If

    Next
'Finish
'----------------------------------------------
    Wscript.Echo "Processed " & filesProcessed & " files."
    Wscript.Echo "Updated " & filesUpdated & " files."
    Wscript.Echo "Finished."