{"id":16441,"date":"2010-11-21T03:01:00","date_gmt":"2010-11-21T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/11\/21\/icame-ipod-iscripted-scripting-itunes\/"},"modified":"2010-11-21T03:01:00","modified_gmt":"2010-11-21T03:01:00","slug":"icame-ipod-iscripted-scripting-itunes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/icame-ipod-iscripted-scripting-itunes\/","title":{"rendered":"iCame, iPod, iScripted: Scripting iTunes"},"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>If you&rsquo;re like most people, at one time or another you&rsquo;ve been given a present that was really good but, for some reason, just never got used. For example, one year one of the Scripting Guys was given some rock-climbing gear for his birthday. It was a very thoughtful and generous gift, but: 1) he didn&rsquo;t rock climb; 2) he wasn&rsquo;t sure he ever <i>wanted<\/i> to rock climb; and, 3) even if he did, he had no one to rock climb with. Feeling somewhat guilty he <i>did<\/i> take a couple of rock-climbing lessons, which he actually found kind of fun, To be honest, though, the rock-climbing gear has spent most of its time in his closet. Where it remains to this day.<\/p>\n<p>If you&rsquo;ve ever been given an Apple iPod then you know the feeling. No doubt upon opening your gift the first thing you said was, &ldquo;Oh, wow: an iPod! This is the best present anyone has ever given me!&rdquo; And then, upon further reflection, you probably added, &ldquo;Of course, the iPod and iTunes <i>is<\/i> scriptable, right? Right?&rdquo; And as silence descended over the room you sighed and then tossed the iPod into your closet. Where it remains to this day. After all, what&rsquo;s the point of having an iPod and the iTunes software if you can&rsquo;t even script it?<\/p>\n<p>And so you &ndash; whoa, hold on a minute: who <i>said<\/i> you can&rsquo;t script the iPod and the iTunes software? You can write scripts to manage iTunes and the iPod. Who told you different? Aunt Martha? Listen, who are you going to believe: Aunt Martha, or the Scripting Guys?<\/p>\n<p>Well, OK, we can&rsquo;t argue with you there. But this time we really <i>do<\/i> know what we&rsquo;re talking about. As it turns out, you <i>can<\/i> write scripts to manage the iTunes software; in addition, you can also use scripts that interact with your iPod. We&rsquo;re absolutely serious: in this case, Aunt Martha is wrong. <\/p>\n<p>And we&rsquo;re about to prove it to you.<\/p>\n<h4>The Longest Journey Begins with the First Script<\/h4>\n<p>Let&rsquo;s do something a bit out of character for the Scripting Guys and start at the beginning. Here&rsquo;s as simple a script as you could ever write, a script that tells you the version of the iTunes software installed on your computer:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nWscript.Echo \"Version: \" &amp; objApp.Version<\/pre>\n<p>Admittedly, this might not be the most exciting script you&rsquo;ve ever seen. (If it <i>is<\/i> the most exciting script you&rsquo;ve ever seen, well, you really need to get out and look at more scripts.) However, it&rsquo;s a useful starting point, and for a couple of reasons.<\/p>\n<p>For one, note the first line of code, which simply creates an instance of the <b>iTunes.Application<\/b> object:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")<\/pre>\n<p>Is that important? You bet it is: generally speaking, the first line of code in any iTunes script needs to create the iTunes.Application object. It&rsquo;s only after you have the Application object in tow that you can start creating various child objects, which tend to do things a little more interesting than simply echo back the version number.<\/p>\n<p><b>Note<\/b>. We had a feeling you were going to ask that. As it turns out, there are all sorts of child objects you can create, far too many to discuss in this article. For details, download the iTunes SDK, available from the Apple Web site. With any luck, the information found there &ndash; combined with the explanations offered in this article &ndash; will be enough to get you going.<\/p>\n<p>And, if all else fails, you can always ask Aunt Martha.<\/p>\n<p>Here&rsquo;s another little script, one that tells you whether the iTunes application is configured for mini-player mode:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nSet objWindow = objApp.BrowserWindow\nWscript.Echo \"Mini-player mode: \" &amp; objWindow.MiniPlayer<\/pre>\n<p>You&rsquo;re right: it <i>is<\/i> easy. As you can see, we start off by creating an instance of the iTunes.Application object. We then use this line of code to create an instance of the <b>BrowserWindow<\/b> object, something we do simply by creating an object reference (objWindow) to the Application object&rsquo;s BrowserWindow property:<\/p>\n<pre>Set objWindow = objApp.BrowserWindow<\/pre>\n<p>By the way, this is how you usually create child objects and child collections. For example, do you want to retrieve a collection of the equalizer presets? That&rsquo;s fine; just create an object reference to the <b>EQPresets<\/b> property:<\/p>\n<pre>Set colPresets = objApp.EQPresets<\/pre>\n<p>Oh, sorry: you said you wanted to retrieve a collection of encoders, didn&rsquo;t you? Well, then simply create an object reference to the <b>Encoders<\/b> properties:<\/p>\n<pre>Set colEncoders = objApp.Encoders<\/pre>\n<p>And so on. <\/p>\n<p>After you have an object reference to the BrowserWindow object all you have to do is echo back the value of the <b>MiniPlayer<\/b> property:<\/p>\n<pre>Wscript.Echo \"Mini-player mode: \" &amp; objWindow.MiniPlayer<\/pre>\n<p>That&rsquo;s nice, but here&rsquo;s one of the <i>really<\/i> cool features of the iTunes application: a large number of the properties are read\/write. For example, it&rsquo;s useful to know whether or not the iTunes player is configured in mini-player mode (although you could probably just look at the screen and figure that out for yourself). What might be more useful is a script that can <i>put<\/i> the player into mini-player mode. <\/p>\n<p>Can we do that? You bet we can. Because MiniPlayer is a Boolean (True\/False) value all we have to do is set MiniPlayer to True:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nSet objWindow = objApp.BrowserWindow\nobjWindow.MiniPlayer = True<\/pre>\n<p>Just like that, our iTunes application will switch to mini-player mode.<\/p>\n<p>Incidentally, we should mention that all these scripts automatically start and display the iTunes player (if it isn&rsquo;t already up and running), even the script that simply echoes back the iTunes version number. To tell you the truth, we don&rsquo;t know of any way to prevent the player from appearing on screen. However, you can always display the player, have your script do its thing, and then use the <b>Quit<\/b> method to dismiss the application. For example, this simple little script creates an instance of the iTunes.Application object, reports back the version number, and then terminates the application:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nWscript.Echo \"Version: \" &amp; objApp.Version\nobjApp.Quit<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4>With a Song in Your Heart (or at Least in Your iTunes Library)<\/h4>\n<p>You know, you&rsquo;re right: when you installed the iTunes software you were probably hoping to do more with it than simply determine the version number. Fair enough; let&rsquo;s see what else we can do with an iTunes script. How about this: let&rsquo;s say we retrieve a list of all the songs in your iTunes library.<\/p>\n<p>You know, you can always tell people who are new to the Script Center; they worry that any task we propose is going to be hard. Don&rsquo;t worry; the Scripting Guys don&rsquo;t <i>do<\/i> hard. Instead, we only do things that are easy, things like retrieve information about all the songs in your iTunes library:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nSet objLibrary = objApp.LibraryPlaylist\nSet colTracks = objLibrary.Tracks\nFor Each objTrack in colTracks\n    Wscript.Echo \"Name: \" &amp; objTrack.Name\n    Set objPlaylist = objTrack.Playlist\n    Wscript.Echo \"Playlist: \" &amp; objPlaylist.Name\n    Wscript.Echo \"Album: \" &amp; objTrack.Album\n    Wscript.Echo \"Artist: \" &amp; objTrack.Artist\n    Wscript.Echo \"Bit rate: \" &amp; objTrack.BitRate\n    Wscript.Echo \"Beats per minute: \" &amp; objTrack.BPM\n    Wscript.Echo \"Comment: \" &amp; objTrack.Comment\n    Wscript.Echo \"Compilation: \" &amp; objTrack.Compilation\n    Wscript.Echo \"Composer: \" &amp; objTrack.Composer\n    Wscript.Echo \"Date added: \" &amp; objTrack.DateAdded\n    Wscript.Echo \"Disc count: \" &amp; objTrack.DiscCount\n    Wscript.Echo \"Disc number: \" &amp; objTrack.DiscNumber\n    Wscript.Echo \"Duration (seconds): \" &amp; objTrack.Duration\n    Wscript.Echo \"Checked for playback: \" &amp; objTrack.Enabled\n    Wscript.Echo \"Artist: \" &amp; objTrack.Artist\n    Wscript.Echo \"Equalizer preset: \" &amp; objTrack.EQ\n    Wscript.Echo \"Stop time (seconds): \" &amp; objTrack.Finish\n    Wscript.Echo \"Genre: \" &amp; objTrack.Genre\n    Wscript.Echo \"Grouping: \" &amp; objTrack.Grouping\n    Wscript.Echo \"Kind: \" &amp; objTrack.KindAsString\n    Wscript.Echo \"Modification date: \" &amp; objTrack.ModificationDate\n    Wscript.Echo \"Times played: \" &amp; objTrack.PlayedCount\n    Wscript.Echo \"Last played on: \" &amp; objTrack.PlayedDate\n    Wscript.Echo \"Play order index: \" &amp; objTrack.PlayOrderIndex\n    Wscript.Echo \"Rating: \" &amp; objTrack.Rating\n    Wscript.Echo \"Sample rate: \" &amp; objTrack.SampleRate\n    Wscript.Echo \"Size (bytes): \" &amp; objTrack.Size\n    Wscript.Echo \"Start time (seconds): \" &amp; objTrack.Start\n    Wscript.Echo \"Track length: \" &amp; objTrack.Time\n    Wscript.Echo \"Total tracks on album: \" &amp; objTrack.TrackCount\n    Wscript.Echo \"Track number on album: \" &amp; objTrack.TrackNumber\n    Wscript.Echo \"Volume adjustment: \" &amp; objTrack.VolumeAdjustment\n    Wscript.Echo \"Year: \" &amp; objTrack.Year\n    Wscript.Echo\nNext<\/pre>\n<p>Don&rsquo;t let the size of the script deter you; it&rsquo;s a long script simply because we&rsquo;re returning quite a bit of information about each song. Tell you what; let&rsquo;s pare this script down a bit and return only the name of each song in the library:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nSet objLibrary = objApp.LibraryPlaylist\nSet colTracks = objLibrary.Tracks\nFor Each objTrack in colTracks\n    Wscript.Echo \"Name: \" &amp; objTrack.Name\nNext<\/pre>\n<p>Better? As you can see we start out by creating our old friend the iTunes.Application object, then we create an instance to the iTunes library. That&rsquo;s what we do here:<\/p>\n<pre>Set objApp = CreateObject(\"iTunes.Application\")\nSet objLibrary = objApp.LibraryPlaylist<\/pre>\n<p>Once we&rsquo;re connected to the library we can use the <b>Tracks<\/b> property to return a collection of all the songs in the library:<\/p>\n<pre>Set colTracks = objLibrary.Tracks<\/pre>\n<p>From there it&rsquo;s just a matter of setting up a For Each loop to loop through the collection and echo back the song name:<\/p>\n<pre>For Each objTrack in colTracks\n    Wscript.Echo \"Name: \" &amp; objTrack.Name\nNext<\/pre>\n<p>Will that really work? You bet it will:<\/p>\n<pre>Name: Out Here All Night\nName: Easy Love\nName: Steady, As She Goes (Acoustic Version)\nName: Ciaccona\nName: Mama's Room<\/pre>\n<p>As you saw in the initial script we showed you, you can retrieve a lot more information about a song than just the song title; that includes everything from the artist name to the album to the year the song was recorded. Furthermore, most of these song properties (or, to use iTunes lingo, <i>track<\/i> properties) are read\/write. But we&rsquo;ll talk about that next weekend. <\/p>\n<p>That is all there is to using VBScript to work with your iTunes collection. Join&nbsp;me tomorrow as we begin a new week on the Script Center. 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.] If you&rsquo;re like most people, at one time or another you&rsquo;ve been given a present that was really good but, [&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-16441","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.] If you&rsquo;re like most people, at one time or another you&rsquo;ve been given a present that was really good but, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16441","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=16441"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16441\/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=16441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}