Archive for December, 2009


I happen to have an iPhone and an iPod Touch sync’d to my iTunes back in the US, but now I’m in Iraq and don’t have access to my computer with the iTunes library on it. I do have the music, but I don’t want to sync my iPod to my iTunes because all the play counts and ratings will be gone. I had a little free time last night so I wrote a script to update these attributes for me! It is written in JScript/JavaScript and uses the iTunes COM Interface to communicate with your device and the iTunes Library. It is not optimized very well so it will iterate over the entire iTunes Library for each track it encounters on your device, looking for a matching Artist, Album and Track Name to update. It also maintains a log file that lists every track that was updated including it’s old and new values, as well as any tracks on the iPod/iPhone that it could not find a match for in the iTunes Library. I created this script for my own purposes and decided to share it, so I don’t intend to provide much support for it. The zip file contains two files: UpdatePlayCount.bat and UpdatePlayCount.js. Extract the files somewhere, plug in your iPod/iPhone, then open iTunes and click on your device, then check “Manually manage music and movies” and restart iTunes, then double click on UpdatePlayCount.bat to update your library. You can see that progress is being made in iTunes by clicking on your Library’s Music folder then sorting the music by Play Count or Rating. This list is updated in real time.

Download UpdatePlayCount.zip (2KB)

Source Code (JScript/JavaScript)

/**
 * UpdatePlayCount.js
 *
 * Description
 * ---------------------------------------------------------------------------------------------------------
 * This script will update your iTunes library with the play counts and ratings from any iPod or iPhone.
 * It uses the iTunes COM Interface to communicate with your device.  In order to determine which tracks
 * match, the script will search through the iTunes Library looking for a matching Artist, Album and Title;
 * as a result, this process can take a long time.  Updates all the music and movies in the "Music" and
 * "Movies" playlists on your device.  If you need to update more folders you can modify the code.
 *
 * Usage
 * ---------------------------------------------------------------------------------------------------------
 * Double click on UpdatePlayCount.bat
 *
 *
 * @package UpdatePlayCount
 * @author Steve Kamerman, stevekamerman AT gmail.com
 * @version Alpha 1.0 $Date: 2009/12/22 13:19
 * @license http://www.mozilla.org/MPL/ MPL Vesion 1.1
 * @language Microsoft JScript / JavaScript
 *
 */


var logObject, logFile;
var logfileName = "UpdateItunes.log";
logObject = new ActiveXObject("Scripting.FileSystemObject");
logFile = logObject.CreateTextFile(logfileName, true);

var ITTrackKindFile = 1;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var sources = iTunesApp.Sources;
var i;
var updateTracksCount = 0;
var missingTracksCount = 0;
var ipod;
var itunes;
var playlists;
var itunesPlaylists;
var iTunesXML;
iTunesXML = "";

for(i=1;i<sources.Count;i++){
    if(sources.Item(i).Kind == 2){
        ipod = sources.Item(i);
        playlists = ipod.Playlists;
    }
    if(sources.Item(i).Kind == 1){
        itunes = sources.Item(i);
        itunesPlaylists = itunes.Playlists;
    }
}

if(ipod == undefined){
    WScript.Echo("Error: No iPod or iPhone found.  Please make sure your device is listed in iTunes, then click on it's name and check "Manually manage music and videos".  Restart iTunes and rerun this script.");
    WScript.Quit(0);
}
WScript.Echo("iPod found, press OK to update iTunes with the play counts and ratings from your iPod.");
WScript.Echo("This process can take a long time to complete.  To monitor the progress,\nopen iTunes and click on your iTunes Music folder\nthen sort by play count or rating and watch them change.\nPress OK to continue");
var currentPlaylist;
var currentTracks;
var currentTrack;
for(i=1;i<playlists.Count;i++){
    currentPlaylist = playlists.Item(i);
    if(currentPlaylist.Name != "Music" && currentPlaylist.Name != "Movies")continue;
    currentTracks = currentPlaylist.Tracks;
    for(a=1;a<currentTracks.Count;a++){
        currentTrack = currentTracks.Item(a);
        if(currentTrack.PlayedCount > 0){
            updateITunesEntry(currentPlaylist.Name,currentTrack.Artist,currentTrack.Album,currentTrack.Name,currentTrack.PlayedCount,currentTrack.Rating);
        }
    }
}

WScript.Echo("Finished processing "+(updateTracksCount+missingTracksCount)+" tracks.\nUpdated Tracks: "+updateTracksCount+"\nMissing Tracks: "+missingTracksCount+"\nSee the logfile ("+logfileName+") for more details.");

function updateITunesEntry(playlist, artist, album, song, playCount, rating){
    var itunesPlaylist;
    var itunesTracks;
    var itunesTrack;
    var i;
    var a;
    for(i=1;i<itunesPlaylists.Count;i++){
        itunesPlaylist = itunesPlaylists.Item(i);
        if(itunesPlaylist.Name != playlist)continue;
        itunesTracks = itunesPlaylist.Tracks;
        for(a=1;a<itunesTracks.Count;a++){
            itunesTrack = itunesTracks.Item(a);
            if(itunesTrack.Artist == artist && itunesTrack.Album == album && itunesTrack.Name == song){
                logFile.WriteLine(itunesTrack.Name+": count: "+itunesTrack.PlayedCount+"->"+playCount+", rating: "+itunesTrack.Rating+"->"+rating);
                itunesTrack.PlayedCount = playCount;
                itunesTrack.Rating = rating;
                updateTracksCount++;
                return(1);
            }
        }
    }
    missingTracksCount++;
    logFile.WriteLine("WARNING: Track not found in iTunes Library: "+artist+" - "+song);
}

Just wanted to let you know that I'm here in Iraq and beginning my mission. I may provide more details about where I am and what I'm doing here in the future. It looks like I won't have as much time as I thought, but I still plan to get some work done on Tera-WURFL while I'm here!