The Scripting Guys Really Do Rock, Part 3 of 4

ScriptingGuy1

[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 three of a four part series of articles about using VBScript to work with the Windows Media Player object model. See Part 1 and Part 2.

Bonus Script: Copying Media Collection Information to an Excel Spreadsheet

To illustrate how useful it can be to retrieve media collection information we’ve decided to toss in a bonus script: this script retrieves information about all your audio files and then copies that information to an Excel spreadsheet. Note that explaining the Excel portion of this script lies a bit outside the scope of this article; if you’re interested in learning more about scripting Microsoft Excel take a look at the Scripting For Microsoft Office center, and check out the Office Space archive in the TechNet Script Center Library.

In the meantime, here’s a script that retrieves all the audio files found in your media collection and writes the following attributes for each song to an Excel spreadsheet:

Name

WM/AlbumArtist

Album

durationString

UserPlayCount

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set colSongList = objMediaCollection.getByAttribute(“MediaType”, “Audio”)

Set objExcel = CreateObject(“Excel.Application”)

objExcel.Visible = True

Set objWorkbook = objExcel.Workbooks.Add()

Set objWorksheet = objWorkbook.Worksheets(1)

objWorksheet.Cells(1,1) = “Song”

objWorksheet.Cells(1,2) = “Artist”

objWorksheet.Cells(1,3) = “Album”

objWorksheet.Cells(1,4) = “Length”

objWorksheet.Cells(1,5) = “Times Played”

j = 2

For i = 0 to colSongList.Count – 1

Set objSong = colSongList.Item(i)

objWorksheet.Cells(j,1) = objSong.Name

objWorksheet.Cells(j,2) = objSong.getItemInfo(“WM/AlbumArtist”)

objWorksheet.Cells(j,3) = objSong.getItemInfo(“Album”)

objWorksheet.Cells(j,4) = objSong.durationString

objWorksheet.Cells(j,5) = objSong.getItemInfo(“UserPlayCount”)

j = j + 1

Next

Set objRange = objWorksheet.UsedRange

objRange.Columns.AutoFit()

When you run the script you should get back a spreadsheet that looks something like this:

clip_image002

Very cool.

Retrieving a Targeted List of Items

If you want a database containing information about all your media files then you’ll definitely appreciate the fact that Windows Media Player enables you to easily retrieve a complete set of media files. On the other hand, there will often be times when you want to work with only a subset of those files; for example, you might want to see only songs by a particular artist, that came from a particular album, or that fit into a specified genre. In fact, there might even be times when you want information about only one song in particular. Sounds great, but how complicated is it going to be to retrieve that kind of information?

As it turns out, it isn’t complicated at all. You don’t have to write an intricate SQL-like query in order to return a targeted set of items; instead, Windows Media Player has a number of built-in methods that make it easy to retrieve these items.

Don’t believe us? Then take a look at the next four scripts.

Retrieving Songs by Artist Name

You can use the getByAuthor method to retrieve an array consisting of all the songs by a particular group or artist. After creating an instance of the MediaCollection object, call the getByAuthor method, passing as the sole parameter the artist name. For example, this script returns an array of all the songs by the group Dire Straits:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set colSongList = objMediaCollection.getByAuthor(“Dire Straits”)

For i = 0 to colSongList.Count – 1

Set objSong = colSongList.Item(i)

Wscript.Echo objSong.Name

Next

As you can see, there’s nothing too terribly complicated here. We create instances of the WMPlayer.OCX and MediaCollection objects. We then call the getByAuthor method, passing the method the name of the artist. This returns an array, in this example, of all the songs by Dire Straits. We can then use a For-Next loop to loop through this array; inside that loop we bind to each individual song and then echo the Name property. The net result? Output that looks like this:

Heavy Fuel

Ticket To Heaven

Sultans of Swing

Retrieving Songs by Album Name

Yes, we know: albums are passé, no one cares about albums any more, it’s all about individual songs and individual downloads. For you old-fashioned types, though, Windows Media Player does allow you to retrieve a list of songs by album name; that is, all the songs in your collection derived from a particular album. The process is very similar to retrieving a collection of songs by artist name; the only difference is that you use the getByAlbum method and pass the album name as the sole parameter.

For example, here’s a script that returns a list of songs from the album In Utero. And, yes, that’s Nirvana’s In Utero. Contrary to the rolled-eyed utterances of at least one Scripting Son, occasionally the Scripting Guys do listen to music that has been released in the last 100 years.

Here’s the script:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set colSongList = objMediaCollection.getByAlbum(“In Utero”)

For i = 0 to colSongList.Count – 1

Set objSong = colSongList.Item(i)

Wscript.Echo objSong.Name

Next

So why would you want to retrieve a list of songs by album? Well, later on in this article we’ll show you how to retrieve a set of songs and then turn that collection into a playlist. Retrieving songs by album is an easy way to create a playlist based on that album.

Retrieving Songs by Genre

One of the hallmarks of digitized music is categorization by genre; inside Windows Media Player – depending on the songs you own – you’ll find categories such as Brit Pop; Classical; Grunge; Jazz; Pop; and Rock. Windows Media Player enables you to use existing categories or create categories all your own; regardless, you can use the getByGenre method to retrieve a collection of all the songs in a particular category. For example, this script returns an array of all the General Rock songs:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set colSongList = objMediaCollection.getByGenre(“General Rock”)

For i = 0 to colSongList.Count – 1

Set objSong = colSongList.Item(i)

Wscript.Echo objSong.Name

Next

Retrieving – and Playing – an Individual Song

You know how it is: sometimes you’re sitting at your computer and you think, “Man, I have to hear Eurotrash Girl one more time or I’ll just die!” (Ok, fine: substitute the name of a song you’ve actually heard of for Eurotrash Girl.) Which brings up an interesting point: is it possible to bind to, and then play, a single song?

You bet it is. In fact, here’s a script that retrieves the song I Hate Myself for Loving You, loads it into Windows Media Player, and then plays it:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set objTempList = objMediaCollection.getByName _

(“I Hate Myself for Loving You”)

Set objSong = objTempList.Item(0)

objPlayer.OpenPlayer(objSong.sourceURL)

The script begins by creating instances of the WMPlayer.OCX and MediaCollection objects; it then uses the getByName method to retrieve all instances of the song I Hate Myself for Loving You. As usual, getByName retrieves an array, even if there’s only one such song named I Hate Myself for Loving You. Therefore, we need to bind to the first song in the array (item 0) using this line of code:

Set objSong = objTempList.Item(0)

After binding to the song we then call the OpenPlayer method, passing as the sole parameter the value of the sourceURL property. Don’t let the name fool you; sourceURL is just the path to the song; while it could be an actual Internet URL, most of the time it will be the path to a local media file.

Of course, having to edit the script each time you want to play a different song is a bit of a hassle, to say the least. Here’s a modified script that accepts a song name as a command-line argument and then opens Windows Media Player and plays the specified song:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

strSong = Wscript.Arguments.Item(0)

Set objMediaCollection = objPlayer.MediaCollection

Set objTempList = objMediaCollection.getByName _

(strSong)

Set objSong = objTempList.Item(0)

objPlayer.OpenPlayer(objSong.sourceURL)

To run this script, type something like this at the command line:

cscript play_song.vbs “Sunny Afternoon”

In the preceding example, the script is named play_song.vbs and the song being played is Sunny Afternoon. If your song name includes blank spaces be sure to surround the name with double quotes; if you don’t, the script will assume that Sunny is the first argument and Afternoon the second. The nice thing about this? Because we’re using the media collection all you have to know is the name of the song; you don’t have to know its location.

Adding a New Item to the Media Collection

If there’s a drawback to using Windows Media Player to manage your digital music it’s the fact that items have to be specifically added to the media collection; Windows Media Player won’t automatically search out all the music files on your computer (although you can ask it to do this). A bit of a nuisance, perhaps, but in reality this shouldn’t cause too many problems. For one thing, you can configure the Player to monitor specific folders and automatically add any new items placed in those folders to the media collection. (For more information, see the Windows Media Player help file.)

Alternatively, you can also use scripts to add new items to the media collection. For example, this script creates instances of the WMPlayer.OCX and MediaCollection objects, then uses the Add method to add the file D:\Music\White_Rabbit.wma to the collection. As you can see, the Add method requires only a single parameter: the path to the media object to be added to the collection.

Here’s the script:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set colMediaCollection = objPlayer.MediaCollection

Set objSong = colMediaCollection.Add _

(“D:\Music\White_Rabbit.wma”)

Of course, this script can easily be modified to add all the items in a specified folder to the collection. Here’s a script that adds all the MP3 and WMA files found in the folder D:\Music to the media collection:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set colMediaCollection = objPlayer.mediaCollection

Set FileList = objWMIService.ExecQuery _

(“ASSOCIATORS OF {Win32_Directory.Name=’D:\Music’} Where ” _

& “ResultClass = CIM_DataFile”)

For Each objFile In FileList

If objFile.Extension = “mp3” or objFile.Extension = “wma” Then

Set objSong = colMediaCollection.Add(objFile.Name)

End If

Next

Deleting an Item from the Media Collection

When it comes to music sooner or later you find yourself in possession of something terribly embarrassing. The Captain and Tennille? What the heck were you thinking? But hey, don’t sweat it: one of the great benefits of the digital age is the fact that you can quickly and easily erase your mistakes. Suppose that, for reasons known only to yourself, you downloaded a karaoke version of That’s Amore. Relax; just run this script and no one will ever know:

Set objPlayer = CreateObject(“WMPlayer.OCX” )

Set objMediaCollection = objPlayer.MediaCollection

Set objTempList = objMediaCollection.getByName _

(“Karaoke – That’s Amore – Dean Martin”)

Set objSong = objTempList.Item(0)

objMediaCollection.Remove objSong, TRUE

This script creates instances of the WMPlayer.OCX and MediaCollection objects, then uses the getByName method to retrieve all instances of the song Karaoke – That’s Amore – Dean Martin (and let’s hope there’s only one such instance). The script binds to the first item in the array and then calls the Remove method, passing the method two arguments: objSong (the object reference to the song) and TRUE, which simply means, yes, I want to delete this file. That’s all you have to do.

Note that this removes the song only from the Media Collection; it doesn’t actually remove the file from your hard drive. Later, after everyone else has gone, you can restore the file to your media collection and karaoke the night away.

That is all there is to using VBScript to work with Windows Media Player. Join me tomorrow as this four part series of Windows Media Player articles draws to a conclusion. 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

0 comments

Discussion is closed.

Feedback usabilla icon