{"id":16311,"date":"2010-12-05T00:01:00","date_gmt":"2010-12-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/12\/05\/the-scripting-guys-really-do-rock-part-2-of-4\/"},"modified":"2010-12-05T00:01:00","modified_gmt":"2010-12-05T00:01:00","slug":"the-scripting-guys-really-do-rock-part-2-of-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/the-scripting-guys-really-do-rock-part-2-of-4\/","title":{"rendered":"The Scripting Guys Really Do Rock, Part 2 of 4"},"content":{"rendered":"<p>&nbsp;\n[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.]\n<em>This is part two of a four part series of articles about using VBScript to work with the Windows Media Player object model. Part one began the introduction to the object model.<\/em>\nContinuing from <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/12\/04\/the-scripting-guys-really-do-rock-part-1-of-4.aspx\">yesterday<\/a>. For now, let&rsquo;s see how scripts can help us manage our music files and playlists.\n<b>Retrieving All the Items in Your Media Collection<\/b>\nIn old Disney cartoons Scrooge McDuck used to sit in his private bank vault, surrounded by every last dollar he owned. As a digital media aficionado you&rsquo;ll often want to do the same thing: you&rsquo;ll want to be able to retrieve a list of every last piece of digital media you own. Here&rsquo;s a script that will give you a Scrooge McDuck experience (though, unfortunately, it&rsquo;ll just be with your digital media files and not with a bank vault full of money):\nSet objPlayer = CreateObject(&#8220;WMPlayer.OCX&#8221; )\nSet colMediaCollection = objPlayer.mediaCollection\nSet colMedia = colMediaCollection.getAll()\nFor i = 0 to colMedia.Count &#8211; 1\nSet objItem = colMedia.Item(i)\nWscript.Echo objItem.Name\nNext\nAs you can see, it&rsquo;s actually a very simple script. We begin by creating an instance of the WMPlayer.OCX object. This is the parent object for all Windows Media Player scripts; all the other objects, collections, and arrays we&rsquo;ll use are derived from this object. After we create the Windows Media Player object we can then create an instance of the MediaCollection object by using this line of code:\nSet colMediaCollection = objPlayer.mediaCollection\nNow, how do we get at all the items stored in the media collection? The easiest way to do that is to use the <b>getAll()<\/b> method; as the name implies, this returns an array consisting of all the items in the collection. And because this <i>is<\/i> an array, we can use a For-Next loop to loop through all the items in the array (and thus through all the media items in our collection):\nFor i = 0 to colMedia.Count &#8211; 1\nSet objItem = colMedia.Item(i)\nWscript.Echo objItem.Name\nNext<\/p>\n<p>Why does our loop run from 0 to the number of items in the list (<b>colMedia.Count<\/b>) minus 1? Because arrays begin with item 0 and continue through the number of items minus 1. For example, a 5-item array would be numbered like this: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\">  <b>Item<\/b><\/p>\n<\/td>\n<td valign=\"top\">\n<p><b>Item Number<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>First item<\/p>\n<\/td>\n<td valign=\"top\">\n<p>0<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Second item<\/p>\n<\/td>\n<td valign=\"top\">\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Third item<\/p>\n<\/td>\n<td valign=\"top\">\n<p>2<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Fourth item<\/p>\n<\/td>\n<td valign=\"top\">\n<p>3<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>Fifth item<\/p>\n<\/td>\n<td valign=\"top\">\n<p>4<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Inside the For-Next loop we create an object reference to the individual item and then simply echo back the <b>Name<\/b> of that item. Are there other properties we could retrieve besides the Name? You bet there are, and we&rsquo;ll discuss those momentarily. (If you just can&rsquo;t wait, then skip ahead to the section titled <i>Working with Media Object Attributes<\/i>.)\n<b>Retrieving Items by Item Type<\/b><\/p>\n<p>The preceding script shows you all the items in your Media Collection, including songs, playlists, videos, and what-have-you. There are times when you might find it useful to retrieve all these items; at other times, however, you might want to return only a subset of items, maybe all the music files or all the video files. Using the Windows Media Player object model you can write a script that returns only: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">  &bull;<\/p>\n<\/td>\n<td>\n<p>Audio items<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Video items<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Photo items<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Playlists<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Other items that might be found in the collection<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For example, the following script returns only the audio items found in the media collection. The script begins by creating instances of the WMPlayer.OCX and MediaCollection objects, then uses the <b>getByAttribute<\/b> method to return all the items with the <b>MediaType<\/b> of Audio. The Audio items will be returned as an array, so the script uses a For-Next loop to walk through all the items in that array, binding to each song and then echoing the song <b>Name<\/b>.\nThe script itself looks like this:\nSet objPlayer = CreateObject(&#8220;WMPlayer.OCX&#8221; )\nSet objMediaCollection = objPlayer.MediaCollection\nSet colSongList = objMediaCollection.getByAttribute(&#8220;MediaType&#8221;, &#8220;Audio&#8221;)\nFor i = 0 to colSongList.Count &#8211; 1\nSet objSong = colSongList.Item(i)\nWscript.Echo objSong.Name\nNext\nThe focus of this article is music files; we aren&rsquo;t going to talk in any detail about other media items. (For more information about other media types, click <b>here<\/b>.) However, to better illustrate how the getByAttribute method can be used to retrieve other media items, the following script returns the name and dimensions of all the photo items in the Media Collection:\nSet objPlayer = CreateObject(&#8220;WMPlayer.OCX&#8221; )\nSet colMediaCollection = objPlayer.mediaCollection\nSet objPhotos = colMediaCollection.getByAttribute(&#8220;MediaType&#8221;, &#8220;photo&#8221;)\nFor i = 0 to objPhotos.count &#8211; 1\nSet objPhoto = objPhotos.Item(i)\nWscript.Echo &#8220;Name: &#8221; &amp; objPhoto.Name\nWscript.Echo &#8220;Height: &#8221; &amp; objPhoto.getItemInfo(&#8220;WM\/VideoHeight&#8221;)\nWscript.Echo &#8220;Width: &#8221; &amp; objPhoto.getItemInfo(&#8220;WM\/VideoWidth&#8221;)\nNext\nBy default, Windows Media Player doesn&rsquo;t automatically keep track of photo items. To add .JPG files to your media collection you can either use a script or, in the Player itself, click <b>File<\/b> and then select one of the options under <b>Add to Library<\/b>. Before you can add .JPG files to Windows Media Player 10, however, you must first click <b>Tools<\/b>, click <b>Options<\/b>, and then, on the <b>Player<\/b> tab, select <b>Enable picture support for devices<\/b>.\n<b>Working with Media Object Attributes<\/b><\/p>\n<p>As we hinted at earlier, media objects have more attributes than just their name. For example, audio items have attributes such as: <\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">  &bull;<\/p>\n<\/td>\n<td>\n<p>Album (<b>Album<\/b>)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Artist (<b>WM\/AlbumArtist<\/b>)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Year of release (<b>WM\/Year<\/b>)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p>&bull;<\/p>\n<\/td>\n<td>\n<p>Number of times played (<b>UserPlayCount<\/b>)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Working with attributes can be a little tricky; that&rsquo;s because you typically have to work with two different sets of attributes. To begin with, the Media object has a set of attributes that are applied to all media items regardless of type; these attributes include such things as <b>Name<\/b>, <b>durationString<\/b> (the playing time for the item, in the format <i>minutes:seconds<\/i>), and <b>sourceURL<\/b> (the path to the media file). The values of these attributes can be directly reported, using code similar to this:\nWscript.Echo objItem.durationString\nWscript.Echo objItem.sourceURL\nOther attributes, however, are tied to particular media types; for example audio items have a different set of attributes from photo items, which have a different set of attributes from video items. To access these attributes you must use the <b>getItemInfo<\/b> method, passing getItemInfo the name of the attribute you want to retrieve. For example, these two lines of code use getItemInfo to echo the values of the <b>WM\/AlbumArtist<\/b> and <b>UserPlayCount<\/b> attributes:\nWscript.Echo objSong.getItemInfo(&#8220;WM\/AlbumArtist&#8221;)\nWscript.Echo objSong.getItemInfo(&#8220;UserPlayCount&#8221;)\n&nbsp;<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\">  <b>Note<\/b>. So how are you supposed to know that WM\/AlbumArtist and UserPlayCount are attributes of an audio item? The best way that we know of is to browse the <b><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd758070(VS.85).aspx\">Windows Media Player SDK<\/a><\/b> on MSDN.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Incidentally, many of these attributes are read\/write; that means you can use a script to change the attribute values. We aren&rsquo;t going to talk about modifying attribute values in this article. However, here&rsquo;s a sample script showing how the <b>setItemInfo<\/b> method can be used to change the Name of a music file:\nSet objPlayer = CreateObject(&#8220;WMPlayer.OCX&#8221; )\nSet objMediaCollection = objPlayer.MediaCollection\nSet objTempList = objMediaCollection.getByName(&#8220;She Bop [Album Version]&#8221;)\nSet objSong = objTempList.Item(0)\nobjSong.setItemInfo &#8220;Name&#8221;, &#8220;She Bop&#8221;\nThat is all there is to using VBScript to work with Windows Media Player collections Next weekend we will post the remaining 2 parts. Join me tomorrow as I begin a new week on the TechNet Script Center. I invite you to follow me on <a href=\"http:\/\/www.twitter.com\/ScriptingGuys\"><b>Twitter<\/b><\/a> 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.<\/p>\n<p>Ed Wilson, Microsoft Scripting Guy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; [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 two of a four part series of articles about using VBScript to work with the Windows Media [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,5,61,192],"class_list":["post-16311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-vbscript","tag-weekend-scripter","tag-windows-media-player-and-audio"],"acf":[],"blog_post_summary":"<p>&nbsp; [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 two of a four part series of articles about using VBScript to work with the Windows Media [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}