{"id":16391,"date":"2010-11-27T00:01:00","date_gmt":"2010-11-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/11\/27\/icame-ipod-iscripted-scripting-itunes-part-2\/"},"modified":"2010-11-27T00:01:00","modified_gmt":"2010-11-27T00:01:00","slug":"icame-ipod-iscripted-scripting-itunes-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/icame-ipod-iscripted-scripting-itunes-part-2\/","title":{"rendered":"iCame, iPod, iScripted: Scripting iTunes part 2."},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>[Disclaimer: this is a reprint of an old Scripting Guys article from the TechNet Script Center FunZone. The FunZone was not migrated from our old platform. It is printed here due to popular demand.]<\/p>\n<p><em>This is part two of a three part series of articles about scripting iTunes with VBScript. See last <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/11\/21\/icame-ipod-iscripted-scripting-itunes.aspx\">Sundays article <\/a>for part one. <\/em><\/p>\n<p><b>Bonus Script: Printing Out Your Song Collection<\/b><\/p>\n<p>One of the really nice features in the iTunes player is its print capability: iTunes enables you to print song lists, album listings, and even CD jewel case inserts. Best of all, you can print any of these items from within a script. And here&rsquo;s how:<\/p>\n<p>Const ITPlaylistPrintKindPlaylist = 0<\/p>\n<p>Set objApp = CreateObject(&#8220;iTunes.Application&#8221;)<\/p>\n<p>Set objPlaylist = objApp.LibraryPlaylist<\/p>\n<p>objPlaylist.Print False, ITPlaylistPrintKindPlaylist, &#8220;Songs&#8221;<\/p>\n<p>Don&rsquo;t worry, we&rsquo;ll explain how this all works. To begin with, we define a constant named ITPlaylistPrintKindPlaylist and set the value to 0; this simply tells iTunes what we want to print. As we noted, there are three categories of printable items in iTunes; here&rsquo;s a list of the categories, their constants, and their values: <\/p>\n<p><b>Constant<\/b><\/p>\n<p><b>Value<\/b><\/p>\n<p><b>Description<\/b><\/p>\n<p>ITPlaylistPrintKindPlaylist <\/p>\n<p>0 <\/p>\n<p>Prints a list of tracks in the playlist <\/p>\n<p>ITPlaylistPrintKindAlbumlist <\/p>\n<p>1 <\/p>\n<p>Prints a list of albums in the playlist <\/p>\n<p>ITPlaylistPrintKindInsert <\/p>\n<p>2 <\/p>\n<p>Prints a CD jewel case insert <\/p>\n<p>After defining the constant we create an instance of the iTunes.Application object and then connect to the iTunes library. <\/p>\n<p><b>Note<\/b>. We haven&rsquo;t talked about user-created playlists yet, but the technique we&rsquo;re about to show you works with those playlists as well. To print out a custom playlist you simply connect to the individual playlist rather than the iTunes library. <\/p>\n<p>All we need to do now is call the <b>Print<\/b> method:<\/p>\n<p>objPlaylist.Print False, ITPlaylistPrintKindPlaylist, &#8220;Songs&#8221;<\/p>\n<p>You probably noticed that the Print method takes three parameters. The first parameter &ndash; False &ndash; simply tells iTunes to print to the default printer. Had we set this parameter to True then iTunes would have displayed the <b>Print<\/b> dialog box rather than automatically printing out the requested information. <\/p>\n<p>Parameter 2 is also pretty straightforward: it&rsquo;s the constant ITPlaylistPrintKindPlaylist which, again, indicates what we want to print. That leaves us with one final parameter: &ldquo;Songs&rdquo;. <\/p>\n<p>Songs is nothing more than the print &ldquo;theme&rdquo; we want to apply to the printed document. (A theme is sort of like a Microsoft Word template.) How did we know that Songs was a valid theme? That&rsquo;s easy: from within iTunes we clicked <b>File<\/b> and then clicked<b> Print<\/b>. In the <b>Print &ldquo;Library&rdquo;<\/b> dialog box we clicked <b>Song listing<\/b> and then clicked the <b>Theme<\/b> drop-down list: <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7851.clip_image002_46437147.jpg\"><img decoding=\"async\" height=\"143\" width=\"244\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5466.clip_image002_thumb_662ECE44.jpg\" alt=\"clip_image002\" border=\"0\" title=\"clip_image002\" style=\"border-right-width: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>Notice the items in the drop-down list. Those are the available themes, and all we have to do is specify the theme name (make sure you use the name exactly as shown in the list) as the Print method&rsquo;s third parameter. Cool, huh? <\/p>\n<p>Here&rsquo;s another printing example. Suppose we burned all the songs in the library to a CD. (Granted, that means you either don&rsquo;t have very many songs in your Library or you&rsquo;re making one <i>big<\/i> CD. But play along with us for now.) Here&rsquo;s a script that prints a jewel case insert to accompany that CD:<\/p>\n<p>Const ITPlaylistPrintKindInsert = 2<\/p>\n<p>Set objApp = CreateObject(&#8220;iTunes.Application&#8221;)<\/p>\n<p>Set objPlaylist = objApp.LibraryPlaylist<\/p>\n<p>objPlaylist.Print True, ITPlaylistPrintKindInsert, &#8220;White mosaic&#8221;<\/p>\n<p><i>Very<\/i> cool. <\/p>\n<p><b>Name &ndash; and then Bind to &ndash; That Tune<\/b><\/p>\n<p>Being able to retrieve &ndash; and print &ndash; a collection of all the songs in your iTunes library is obviously useful. On the other hand, there are going to be other times when you want your script to work with a single song or with songs that meet some criteria (all the songs by a particular artist, all the songs released in a specified year, etc.). To work with a subset of songs in the library there are at least two things you can do: bind to an individual song by name, or search for a song or set of songs. <\/p>\n<p><b>Retrieving a Song by Name<\/b><\/p>\n<p>Perhaps the easiest way to zero in on a specific song is to connect to the iTunes library and then use the <b>ItemByName<\/b> method to bind to the desired track. In other words:<\/p>\n<p>Set objApp = CreateObject(&#8220;iTunes.Application&#8221;)<\/p>\n<p>Set objLibrary = objApp.LibraryPlaylist<\/p>\n<p>Set colTracks = objLibrary.Tracks<\/p>\n<p>Set objSong = colTracks.ItemByName(&#8220;Do Right By You&#8221;)<\/p>\n<p>objSong.Play<\/p>\n<p>Again, pretty straightforward. We start by &ndash; stop us if you&rsquo;ve heard this one before &ndash; creating an instance of the iTunes.Application object. From there we create an instance of the LibraryPlaylist object and then use this line of code to return a collection of all the songs (tracks) found in the library:<\/p>\n<p>Set colTracks = objLibrary.Tracks<\/p>\n<p>From there we call the ItemByName method, passing as the sole parameter the name of the song in question:<\/p>\n<p>Set objSong = colTracks.ItemByName(&#8220;Do Right By You&#8221;)<\/p>\n<p>That&rsquo;s all we have to do. Once we have a valid object reference to the song we can then echo back the song properties or, as shown here, call a method. A method like, well, <b>Play<\/b>:<\/p>\n<p>objSong.Play<\/p>\n<p><b>Searching for a Song (or Songs)<\/b><\/p>\n<p>The iTunes object model also makes it easy to search the library for a specified collection of songs. For example, here&rsquo;s a script that searches the library and returns a list of songs that have the word <i>Acoustic<\/i> in one of their property values (for example, in the <b>Genre<\/b> field):<\/p>\n<p>Const ITPlaylistSearchFieldAll = 0<\/p>\n<p>Set objApp = CreateObject(&#8220;iTunes.Application&#8221;)<\/p>\n<p>Set objLibrary = objApp.LibraryPlaylist<\/p>\n<p>Set colTracks = objLibrary.Search(&#8220;Acoustic&#8221;, ITPlaylistSearchFieldAll)<\/p>\n<p>For Each objSong in colTracks<\/p>\n<p>Wscript.Echo objSong.Name<\/p>\n<p>Next<\/p>\n<p>How does <i>this<\/i> script work? Thanks for asking; if you hadn&rsquo;t asked, we&rsquo;re not sure what we would have done with this answer. We start out by defining a constant named ITPlaylistSearchFieldAll, which tells the script to search every field for every song in the library. Could we create a more targeted search, one that searches only specified fields? Sort of; here are the available search types, values, and descriptions: <\/p>\n<p><b>Search Type Constant<\/b><\/p>\n<p><b>Value<\/b><\/p>\n<p><b>Description<\/b><\/p>\n<p>ITPlaylistSearchFieldAll <\/p>\n<p>0 <\/p>\n<p>Search all fields of each track. <\/p>\n<p>ITPlaylistSearchFieldVisible <\/p>\n<p>1 <\/p>\n<p>Search only the fields with columns that are currently visible in the display for the playlist. Note that song name, artist, album, and composer will always be searched, even if these columns are not visible. <\/p>\n<p>ITPlaylistSearchFieldArtists <\/p>\n<p>2 <\/p>\n<p>Search only the artist field of each track. <\/p>\n<p>ITPlaylistSearchFieldAlbums <\/p>\n<p>3 <\/p>\n<p>Search only the album field of each track. <\/p>\n<p>ITPlaylistSearchFieldComposers <\/p>\n<p>4 <\/p>\n<p>Search only the composer field of each track. <\/p>\n<p>ITPlaylistSearchFieldSongNames <\/p>\n<p>5 <\/p>\n<p>Search only the song name field of each track. <\/p>\n<p>After creating the iTunes.Application object and binding to the library, we then execute this line of code:<\/p>\n<p>Set colTracks = objLibrary.Search(&#8220;Acoustic&#8221;, ITPlaylistSearchFieldAll)<\/p>\n<p>Here we&rsquo;re simply calling the <b>Search<\/b> method and passing it a pair of parameters: &ldquo;Acoustic&rdquo; (which happens to be the value we&rsquo;re search for) and the constant ITPlaylistSearchFieldAll (which, again, represents the fields we want to search). Assuming the value Acoustic can be found <i>somewhere<\/i> in the library we&rsquo;ll get back the appropriate collection, a collection we can then walk through using a simple For Each loop:<\/p>\n<p>For Each objSong in colTracks<\/p>\n<p>Wscript.Echo objSong.Name<\/p>\n<p>Next<\/p>\n<p>Using a script to retrieve a collection of similar songs is kind of fun; it can also be very useful. How? Well, for one thing, it makes it very easy for you to create custom playlists. <\/p>\n<p>What&rsquo;s that? We haven&rsquo;t talked about custom playlists yet? That&rsquo;s a wrong we need to right. And right now. <\/p>\n<p>But first, another Scripting Guys bonus. <\/p>\n<p>&nbsp;<\/p>\n<p><b>Bonus Script: Ripping Songs from a CD to Your iTunes Library<\/b><\/p>\n<p>Digital music is great, but moving to this cool new world can be daunting, especially if you have a large quantity of music CDs that haven&rsquo;t been digitized yet. We won&rsquo;t go so far as to claim that scripts will make it a breeze to digitize all your existing CDs. However, you <i>can<\/i> write a simple little script that will automatically &ldquo;rip&rdquo; all the songs from a music CD and add those songs to your iTunes library:<\/p>\n<p>Const ITSourceKindAudioCD = 3<\/p>\n<p>Set objApp = CreateObject(&#8220;iTunes.Application&#8221;)<\/p>\n<p>Set colSources = objApp.Sources<\/p>\n<p>For Each objSource in colSources<\/p>\n<p>If objSource.Kind = ITSourceKindAudioCD Then<\/p>\n<p>strName = objSource.Name<\/p>\n<p>Set colPlaylists = objSource.Playlists<\/p>\n<p>Set objPlaylist = colPlaylists.ItemByName(strName)<\/p>\n<p>Set colTracks = objPlaylist.Tracks<\/p>\n<p>objApp.ConvertTracks2(colTracks)<\/p>\n<p>End If<\/p>\n<p>Next<\/p>\n<p>We won&rsquo;t discuss this bonus script in great detail; suffice to say that we bind to the <b>Sources<\/b> object (something we&rsquo;ll talk about later) and look for any music CD source. This is the line of code that checks for the source <b>Kind<\/b>:<\/p>\n<p>If objSource.Kind = ITSourceKindAudioCD Then<\/p>\n<p>As you can see, we&rsquo;re checking to see if the Kind property is equal to a constant named ITSourceKindAudioCD, a constant we gave the value 3. How did we know that anything with a Kind equal to 3 is a music CD? Why, because we consulted this table, of course: <\/p>\n<p><b>Source Type Constant<\/b><\/p>\n<p><b>Value<\/b><\/p>\n<p><b>Description<\/b><\/p>\n<p>ITSourceKindUnknown <\/p>\n<p>0 <\/p>\n<p>Unknown source kind <\/p>\n<p>ITSourceKindLibrary <\/p>\n<p>1 <\/p>\n<p>Library source <\/p>\n<p>ITSourceKindIPod <\/p>\n<p>2 <\/p>\n<p>iPod source <\/p>\n<p>ITSourceKindAudioCD <\/p>\n<p>3 <\/p>\n<p>Audio CD source <\/p>\n<p>ITSourceKindMP3CD <\/p>\n<p>4 <\/p>\n<p>MP3 CD source <\/p>\n<p>ITSourceKindDevice <\/p>\n<p>5 <\/p>\n<p>Device source <\/p>\n<p>ITSourceKindRadioTuner <\/p>\n<p>6 <\/p>\n<p>Radio tuner source <\/p>\n<p>ITSourceKindSharedLibrary <\/p>\n<p>7 <\/p>\n<p>Shared library source <\/p>\n<p>If we <i>do<\/i> have a music CD we then bind to the playlist for that disc (a music CD will have only one playlist). We create a collection of song tracks (using the code Set colTracks = objPlaylist.Tracks) and then call the <b>ConvertTracks2<\/b> method to automatically import the songs (and their properties) to the Library:<\/p>\n<p>objApp.ConvertTracks2(colTracks)<\/p>\n<p>It&rsquo;s that easy. <\/p>\n<p><b>Note<\/b>. To get a list of all the song properties (including name and artist), you need to be connected to the Internet at the time you run the script. If you <i>are<\/i> connected to the Internet iTunes will automatically go out and retrieve information about the CD and all the songs on it. <\/p>\n<p>That is all there is to using VBScript to work with your iTunes collection. Join me tomorrow for the exciting conclusion to scripting iTunes. I invite you to follow me on <a href=\"http:\/\/www.twitter.com\/ScriptingGuys\">Twitter<\/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 an old Scripting Guys article from the TechNet Script Center FunZone. The FunZone was not migrated from our old platform. It is printed here due to popular demand.] This is part two of a three part series of articles about scripting iTunes with VBScript. See last Sundays article [&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":[123,3,5,61,199],"class_list":["post-16391","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-multimedia","tag-scripting-guy","tag-vbscript","tag-weekend-scripter","tag-zune-and-other-media-devices"],"acf":[],"blog_post_summary":"<p>&nbsp; [Disclaimer: this is a reprint of an old Scripting Guys article from the TechNet Script Center FunZone. The FunZone was not migrated from our old platform. It is printed here due to popular demand.] This is part two of a three part series of articles about scripting iTunes with VBScript. See last Sundays article [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16391","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=16391"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16391\/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=16391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}