A useful bit of Windows shell scriptrickery

Issue: I have too many songs in my iTunes library.
Solution: Delete songs from the library!

I want to get rid of songs that I don’t like, and, at this point, even just songs I’m a bit ambivalent about.


Because I have several thousand songs I’ve listened to less than twice, I have a Smart Playlist set up that selects songs with a Play Count less than 2. I’ll listen as iTunes randomly shuffles through the playlist (technically, Party Shuffles through, giving me history and clairvoyance), and when I hear a song I don’t like… I run in to an issue. See, iTunes doesn’t let you remove songs in a Smart Playlist from your main library. No, you can remove ‘em from whatever playlist, but not from the main library.

So there’s a workaround: Instead of deleting them, I can append “delete” to the Comment field of the song, and every week or so, search in my main library for songs labeled “delete” — since that’s simply a subset of the main library, all those songs can be deleted in one go.

The only issue is, well, I’m lazy! I run iTunes in the background, and when I’m in the midst of a thicket of code, emerging long enough to switch to iTunes, edit the Comment field, go to the next track, and return to the code can be disruptive to The Zone (yeah, we coders have it too). And I’m a programmer, damnit, so surely I should be able to do something to make the process easier!

And so I did.

Apple was good enough to release the iTunes COM SDK for free; this lets Windows programs ask iTunes to do stuff, like, say, add “delete” to the comment field of the currently-playing track, or delete the currently-playing track from the main library, or advance to the next track in the currently playlist. Windows, meanwhile, architected COM in such a way that it’s language-independent — in short, one can write JavaScript (or JScript, as it were) code on Windows to do the same sort of iTunes-automation tasks on Windows that AppleScript does on the Mac. And, last but not least, completing the trifecta, we have the Microsoft Natural Keyboard Pro that I’m typing on right now, with its extra “media” buttons and accompanying IntelliType Pro driver software. Said software can, conveniently enough, be configured to open or run arbitrary applications or files on your computer at the press of a button. Now, “running” the .JS file alone is likely to do one of two things: either (A) open the file for editing, or (B) execute the file and open a blank console window for about 5 seconds. Neither is what we want, which is execution without any additional windows popping up. This can be achieved by executing the script under WSH (Windows Scripting Host), also known as wscript.exe, instead of cscript.exe, which is the “console-based” script executor.

So the chain looks like this:

  1. I press the rightmost “Media” key on my keyboard, above F12…
  2. …which I’ve configured to make IntelliType Pro execute this COM-aware JScript file under wscript…
  3. …which tells iTunes to append “delete” to the currently playing track’s Comment field, and advance to the next track.


Originally, I had a separate .bat file to execute the JS file, but that would always open (and transfer focus to) a new console window, so that had to go.

And yeah, I know I could set it up to start playing the next track and then delete the original track, but doing it this way lets me see how many dozen songs I’m removing in one go at the end of the week. It’s just less depressing to say “Ooh, I pared 130 songs from my library this time!” than “Hmm, only 4132 songs left! Woohoo!”.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s