[Disclaimer: this is a reprint of a previously published article from the TechNet Script Center FunZone. The FunZone was not migrated from our old platform. This article is printed here due to popular demand.]

This is part four of a four part series of articles about using VBScript to work with the Windows Media Player object model. See Part 1, Part 2 and Part 3.

Working with Windows Media Player Playlists

clip_image002

Some of you reading this article might be old enough to have experienced the following scenario. (At least we hope you’re old enough; we’d hate to think that the Scripting Guys are older than everybody in the world.) You go to a friend’s house and he decides he wants you to listen to a few of his favorite songs. And so he hauls out his stereo and his record albums, and he plays the first song. Then he takes that record off the turntable and puts it back in the jacket, and then he finds the next album and takes out the record and plays the next song. And then he takes that record off the turntable and puts it back in the jacket and, well, three hours later you’ve managed to hear 4 or 5 songs. What a…delightful…way to spend an afternoon.

Thank goodness for digital music, and for music playlists. With digital music you can have all your songs stored in one place. And with music playlists your friend can quickly put together a selection of songs and play them one right after another. He gets to show off his music, and you still have enough daylight left to go out and shoot baskets or play Whiffle Ball. It’s the best of both worlds.

In other words, playlists are an integral part of Windows Media Player. And, as we’re about to see, scripts are an excellent way to list, create, delete, and modify your Windows Media Player playlists.

Retrieving a List of All Your Playlists

So how can you get a list of all the playlists in your media collection? Well, how about a script like this one:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set colPlaylists = objPlaylists.getAll()

For i = 0 to colPlaylists.Count - 1

Set objPlaylist = colPlaylists.Item(i)

Wscript.Echo objPlaylist.Name

Next

We start off by creating instances of the WMPlayer.OCX and the PlaylistCollection objects. Next we call the getAll() method to return an array of all the playlists in the playlists collection; because this is an array we can then use a For-Next loop to loop through all the items, and thus all the playlists. Inside the loop we bind to each item in turn, then echo the Name of the playlist. Again, there are other attributes we can echo besides Name; for a complete list, click here.

Listing All the Songs on a Playlist

Being able to retrieve a collection of all your playlists is great; however, most of the time what you really want to see is a list of all the songs on a given playlist. Here’s a sample script that reports back all the songs on the Dire Straits playlist:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objAll = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objAll.Item(0)

For i = 0 to objList.Count - 1

Set objSong = objList.Item(i)

Wscript.Echo objSong.Name

Next

The script begins – as always – by creating an instance of the WMPlayer.OCX object. The script creates an instance of the PlaylistCollection object, then uses the getByName method to return an array consisting of all the playlists with the name Dire Straits Playlist.

For this sample script we’ll assume you have only one playlist named Dire Straits Playlist. As we noted previously, Windows Media Player allows you to have multiple playlists with the same name; before we got tired of fooling around with it, at one point we had 28 playlists with the name Dire Straits Playlist. If you have multiple playlists with the same name, you’ll need to use a For-Each loop to walk through the entire array and look at each playlist individually.

Because in our sample script we’re assuming that there’s only one playlist named Dire Straits Playlist, we can take a shortcut and bind directly to the one playlist (item 0 in the array) using this line of code:

Set objList = objAll.Item(0)

Now, what about the list of songs? Well, because our collection of songs is housed as an array we can walk through the collection using this code:

For i = 0 to objList.Count - 1

Set objSong = objList.Item(i)

Wscript.Echo objSong.Name

Next

We then bind to each individual song within the array and echo back the Name of that song. If we run the script under CScript we’ll get back output similar to this:

Heavy Fuel

Ticket To Heaven

Sultans of Swing

Creating a New Playlist

It can be argued that playlists are the things that make digital music so enticing; after all, playlists enable you to play what you want when you want (and how many times you want). If there’s a disadvantage to playlists, however, it’s this: in order to create each playlist, you have to open Windows Media Player and wade through the media collection, dragging and dropping songs into each playlist.

Or do you? Here’s a script that automatically retrieves all the Dire Straits songs in your media collection and creates a new playlist named Dire Straits Playlist. Let’s take a look at the script and then talk about how it works:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objNewList = objPlaylists.newPlaylist("Dire Straits Playlist")

Set objMediaCollection = objPlayer.MediaCollection

Set objPlaylist = objMediaCollection.getByAuthor("Dire Straits")

Set objNewList = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objNewList.Item(0)

For i = 0 to objPlaylist.Count - 1

Set objSong = objPlaylist.Item(i)

objList.appendItem(objSong)

Next

Wscript.Sleep 10000

Set objNewlist = objMediaCollection.Add _

("D:\Music\My Playlists\Dire Straits Playlist.wpl")

Ok, we know: this looks a little complicated. But that’s because we have a number of things going on here: we need to create a new playlist; we need to retrieve all the Dire Straits songs in the media collection; we need to add those songs to the playlist; and we then need to add the playlist to the media collection. That’s a lot for one little script to do, so let’s break it down into sections and examine it step-by-step.

We begin by creating instances of the WMPlayer.OCX and PlaylistCollection objects. We then create the new playlist by – here’s a shocker – calling the newPlaylist method, passing it the name of our new playlist. That all happens in these three lines:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objNewList = objPlaylists.newPlaylist("Dire Straits Playlist")

Next we retrieve an array of all the Dire Straits songs in the media collection. That takes two lines of code:

Set objMediaCollection = objPlayer.MediaCollection

Set objPlaylist = objMediaCollection.getByAuthor("Dire Straits")

Having created the new playlist we now need to create an object reference to the new list; that happens here:

Set objNewList = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objNewList.Item(0)

Whew. Now – at last – we’re ready to add some songs to the playlist. Here’s the code that does that:

For i = 0 to objPlaylist.Count - 1

Set objSong = objPlaylist.Item(i)

objList.appendItem(objSong)

Next

What we do here is create a For-Next loop that loops through the array of Dire Straits songs we retrieved from the media collection. Inside the loop we bind to the individual song and then use the appendItem method to add the song to the playlist. The appendItem method takes a single parameter: the object reference to the song being added. The code looks like this:

objList.appendItem(objSong)

Note that creating a playlist does not automatically add that playlist to the media collection; in fact, if you create a playlist and do not expressly add the new list to the media collection…well, let’s just say you’ll be disappointed the next time you open Windows Media Player. Therefore, we tack these lines of code onto the end of the script:

Wscript.Sleep 10000

Set objNewlist = objMediaCollection.Add _

("D:\Music\My Playlists\Dire Straits Playlist.wpl")

The first line – Wscript.Sleep 10000 – simply pauses the script for 10 seconds. We do that to allow enough time for the new playlist to be written to the file system. When you create a playlist a file with the file extension .wpl is created in your default My Playlists folder; we have to make sure that this file exists before it can be added to the media collection.

Incidentally, the 10 seconds is just a guideline. Depending on your computer and on the number of songs in the playlist you might have to adjust this value. If you create a playlist and it fails to show up in your Windows Media Player library, then try pausing the script for a little longer. That will usually take care of the problem.

Note. How do you know which is the default My Playlists folder? In Media Player, click Tools, then click Options. On the Rip Music tab look at the folder name under Rip music to this location. For example, if your default music folder is C:\Documents and Settings\All Users\Documents\My Music, My Playlists will be a subfolder of this folder: C:\Documents and Settings\All Users\Documents\My Music\My Playlists.

To actually add the playlist to the media collection simply call the Add method, passing to the method the path to the new .wpl file.

Bonus Script: Creating a Random Playlist

Just for the heck of it, here’s another bonus script. This one retrieves all the songs in the media collection and then – using VBScript’s random number generator – randomly chooses 10 songs and creates a new playlist called Random Playlist. We won’t discuss the code in this article; for more information about generating random numbers see this portion of the VBScript documentation on MSDN.

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objNewList = objPlaylists.newPlaylist("Random Playlist")

Set objMediaCollection = objPlayer.MediaCollection

Set objPlaylist = objMediaCollection.getByAttribute("MediaType", "Audio")

intCount = objPlaylist.Count - 1

Set objNewList = objPlaylists.getByName("Random Playlist")

Set objList = objNewList.Item(0)

For i = 0 to 10

Randomize

intNumber = Int((intCount - 1 + 1) * Rnd + 1)

Set objSong = objPlaylist.Item(intNumber)

objList.appendItem(objSong)

Next

Wscript.Sleep 10000

Set objNewlist = objMediaCollection.Add _

("D:\Music\My Playlists\Random Playlist.wpl")

Each time you run this script you’ll generate a different playlist.

Removing an Existing Playlist

As we’ve already seen, you can use a script to create (or recreate) a playlist in a matter of seconds. Why should you care? Well, for one thing, you don’t have to worry about cluttering up Windows Media Player with a bunch of playlists you rarely use. Instead, you can simply delete those playlists, remaining confident that the lists can easily be recreated should you decide you want to use them after all.

So how we do remove an existing playlist? Well, here’s one way:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objOldList = objPlaylists.getByName("Dire Straits Playlist")

Set objRemoveList = objOldList.Item(0)

objPlaylists.Remove(objRemoveList)

The script starts off by creating instances of the WMPlayer.OCX and PlaylistCollection objects; it then uses the getByName method to retrieve an array consisting of all the playlists named Dire Straits Playlist. Because we’re assuming there’s only one such playlist, the script then creates an object reference (objRemoveList) by binding to item 0 in the array:

Set objRemoveList = objOldList.Item(0)

After that we simply call the PlaylistCollection’s Remove method, passing as the only parameter the object reference to Dire Straits Playlist. Bang! Just like that, the playlist is gone.

Adding a Song to a Playlist

For many people, listening to music has always consisted of hearing a song on the radio, rushing out to buy the album, and only then discovering that there was a good reason why that was the only song on the album ever played on the radio. Today, of course, you don’t have to buy an entire album just to get one song; instead, you can go to an online music store and – for less than a dollar – purchase just that one song. (Well, unless it’s a Beatles song you’re after. Paul, Ringo, we know you’re reading this: what’s up with that?)

After you’ve downloaded your new song you’ll likely want to add it to the media collection and then add it to a playlist. We’ve already shown you how to add a song to the media collection, but what about adding a song to a playlist? Well, here’s one way to do that:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objMediaCollection = objPlayer.MediaCollection

Set objTempList = objMediaCollection.getByName("So Far Away")

Set objSong = objTempList.Item(0)

Set objPlaylists = objPlayer.PlaylistCollection

Set objSongList = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objSongList.Item(0)

objList.appendItem(objSong)

After creating an instance of the WMPlayer.OCX object we create an instance of the MediaCollection object; that enables us to use the getByName method to retrieve a collection of all the songs named So Far Away. Note that we’re assuming you’ve already added the new song to the media collection, either by having it placed in a monitored folder or by using a script. We’re also assuming that you have only one song named So Far Away; if that’s true, then the array of songs you get back from the getByName method will consist of just one item. Therefore, we can create an object reference to the song by binding to item 0 in the array:

Set objSong = objTempList.Item(0)

We then use a similar process to create a second object reference, this one binding us to the Dire Straits Playlist. We now have an object reference – objSong – pointing to the new song and an object reference – objList – pointing to the playlist we want to add the song to. All that’s left is to call the appendItem method to add the new song to the playlist:

objList.appendItem(objSong)

Deleting a Song from a Playlist

Inevitably you’ll put together a playlist and then change your mind or have second thoughts about a particular song. For example, Teacher Teacher is a really good song, but it’s by the group Rockpile; why in the world did you put it on your Dire Straits playlist?

Listen, don’t beat yourself up about it; if Teacher Teacher shouldn’t be on the Dire Straits playlist then just remove it. For example here’s a script that does that very thing. And don’t worry; this only removes the song from the playlist; it does not delete the song from the hard drive or remove it from the media collection.

Here’s the script:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.PlaylistCollection

Set objSongList = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objSongList.Item(0)

For i = 0 to objList.Count - 1

Set objSong = objList.Item(i)

If objSong.Name = "Teacher Teacher" Then

objList.removeItem(objSong)

End If

Next

We start off by creating instances of the WMPlayer.OCX and PlaylistCollection objects, and then use the getByName method to return an array of all the playlists with the name Dire Straits Playlist. We then use this line of code to create an object reference named objList that points to the first (and likely only) item in the array:

Set objList = objSongList.Item(0)

What we need to do now is loop through the playlist and bind to each song; that’s the only way we can determine whether we’ve found Teacher Teacher or not. In this block of code we bind to an item in the array and check to see if the name is equal to Teacher Teacher. If it is, we call the removeItem method, passing the object reference of the song to be deleted:

Set objSong = objList.Item(i)

If objSong.Name = "Teacher Teacher" Then

objList.removeItem(objSong)

End If

A little clumsy and cumbersome maybe (it would be much nicer if you could just bind to the playlist and remove a song by name). But, hey, it works.

Playing a Playlist

There’s not much sense in owning a Ferrari if you never drive it; likewise there’s not much sense in owning a playlist if you never play it. If you’d like to start a playlist from a script, well, here you go:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objPlaylists = objPlayer.MediaCollection

Set objAll = objPlaylists.getByName("Dire Straits Playlist")

Set objList = objAll.Item(0)

objPlayer.OpenPlayer(objList.sourceURL)

This script starts off using code that ought to be familiar to you by now: it creates instances of the WMPlayer.OCX and MediaCollection objects, uses the getByName method to retrieve an array consisting of all the playlists named Dire Straits Playlist, and then creates an object reference to the first playlist (item 0) in the array.

Just a second; we need to catch our breath. If you want to go get a sandwich or something, this might be a good time to do that.

After binding to the playlist we then call the OpenPlayer method, passing as the sole parameter the value of the sourceURL property. That will open Windows Media Player, load in the playlist, and begin playing the first song in the list.

Here’s a modified version of the script that takes a playlist name as a command-line argument and then plays that playlist using Windows Media Player:

Set objPlayer = CreateObject("WMPlayer.OCX" )

strPlaylist = Wscript.Arguments.Item(0)

Set objPlaylists = objPlayer.MediaCollection

Set objAll = objPlaylists.getByName(strPlaylist)

Set objList = objAll.Item(0)

strLocation = objList.sourceURL

objPlayer.OpenPlayer(strLocation)

That is all there is to using VBScript to work with Windows Media Player. Join me tomorrow as I begin a new week on the TechNet Script Center. I invite you to follow me on Twitter or Facebook. If you have any questions, send e-mail to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy