Automatically Set Album Artist in iTunes

I'm working on getting my Windows Media Center to play my iTunes files (by installing codecs and tag readers) and one of the things I'm doing is setting the album artist on all of the tracks because that's something Windows Media Center needs set to get album art properly. While doing it, I found there were basically two classes of track that didn't already have the album artist tag set - those that fell into "Various Artists" (like soundtracks and such) and those where the artist should also be set as the album artist.

To make it easy and less tedious, I wrote a script to set the album artist field to the artist. I figured I'd pass it along for other folks to use at their own risk. No promises this won't corrupt your library or anything. Keep backups.

First, make sure you've set all of the tracks that are "Various Artists" as such. This script doesn't take care of that for you. It's not smart, it just sets "artist = album artist" for tracks missing album artist. Once you've taken care of all of your "Various Artists" tracks, run this script:

var ITTrackKindFile  = 1;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var numTracksWithoutAlbumArtist = 0;
var mainLibrary = iTunesApp.LibraryPlaylist;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var foundTracks = new Array();

WScript.Echo("Checking " + numTracks + " tracks for missing album artist...");
while (numTracks != 0)
{
  var  currTrack = tracks.Item(numTracks);
  
  if (currTrack.Kind == ITTrackKindFile && !currTrack.Podcast)
  {
    if(currTrack.AlbumArtist == null || currTrack.AlbumArtist.length == 0)
    {
      numTracksWithoutAlbumArtist++;
      foundTracks.push(currTrack);
    }
  }
  
  numTracks--;
  if(numTracks % 1000 == 0)
  {
    WScript.Echo(numTracks + " tracks left to check...");
  }
}

if (numTracksWithoutAlbumArtist > 0)
{
  WScript.Echo("Found " + numTracksWithoutAlbumArtist + " tracks missing album artist. Creating playlist and updating artists...");
  
  var playList = iTunesApp.CreatePlaylist("Fixed Album Artists");
  for(var trackIndex in foundTracks)
  {
    var currTrack = foundTracks[trackIndex];
    currTrack.AlbumArtist = currTrack.Artist;
    playList.AddTrack(currTrack);
  }
  WScript.Echo("Playlist created.");
}
else
{
  WScript.Echo("No tracks missing album artist were found.");
}

What it does is:

  • Iterate through all of the tracks in your library and find the ones that aren't Podcasts that have no album artist.
  • Creates a playlist.
  • Sets the artist as the album artist on the tracks it found.
  • Adds those tracks to the playlist so you know which tracks were updated.

In the event it does something wrong, you can always select all the tracks in the created playlist and set the album artist back to empty.

Again, YMMV, use at your own risk... but it worked great for me.

posted on Saturday, May 09, 2009 10:38 AM | Filed Under [ Media Code Snippets ]

Comments

Gravatar # re: Automatically Set Album Artist in iTunes
by Matt at 5/19/2009 12:17 PM
I know enough about coding to follow the logic, but how do I take your code and use it in iTunes? Basically, what do I do to execute the script?
Gravatar # re: Automatically Set Album Artist in iTunes
by Travis Illig at 5/19/2009 12:44 PM
With this, or any, script for iTunes, you'll first save it as a text file. In this case, it's a JavaScript file, so make it "SetAlbumArtist.js" or something. Then you can execute it by EITHER double-clicking it OR dropping to a command prompt and running "cscript SetAlbumArtist.js" (without the quotes). Done and done.

(This particular script is for Windows using the iTunes COM interface; on Mac you'd use AppleScript and interface slightly differently. I don't know how to do that.)
Gravatar # re: Automatically Set Album Artist in iTunes
by Matt at 5/20/2009 10:09 AM
Thanks! I just wasn't sure what extension to save it as in notepad. However, it's not working for me. It works all the way up to line 38:

currTrack.AlbumArtist = currTrack.Artist;

Then it gives me an invalid pointer error. I know it's found the files b/c it gives me a count of all the files that don't have the album artist field filled, creates the playlist in iTunes, but then the playlist is empty, no files fixed. Do you know of a way I can debug it?
Gravatar # re: Automatically Set Album Artist in iTunes
by Travis Illig at 5/20/2009 10:17 AM
Chances are that's because you have a track that doesn't have Artist set.

There is no good way to debug this. It's old-school "printf" style debugging where you'll have to add WScript.Echo messages to dump data to the console.

I would also recommend you download the iTunes COM SDK so you have some documentation explaining what the script is doing and what the object types are.

Unfortunately, I don't think I can help you much beyond that. Like I said, it's sort of a "works on my box" script.
Gravatar # re: Automatically Set Album Artist in iTunes
by Travis Illig at 5/20/2009 12:49 PM
Note: The iTunes SDK requires you to have an Apple Developer Center (ADC) membership. It's free, but you do need it. Once you log into the Apple Developer Center, the iTunes SDK is under Downloads - Developer Tools.
Comments have been closed on this topic.