iCame, iPod, iScripted: Scripting iTunes
[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’re like most people, at one time or another you’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’t rock climb; 2) he wasn’t sure he ever wanted to rock climb; and, 3) even if he did, he had no one to rock climb with. Feeling somewhat guilty he did 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.
If you’ve ever been given an Apple iPod then you know the feeling. No doubt upon opening your gift the first thing you said was, “Oh, wow: an iPod! This is the best present anyone has ever given me!” And then, upon further reflection, you probably added, “Of course, the iPod and iTunes is scriptable, right? Right?” 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’s the point of having an iPod and the iTunes software if you can’t even script it?
And so you – whoa, hold on a minute: who said you can’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?
Well, OK, we can’t argue with you there. But this time we really do know what we’re talking about. As it turns out, you can write scripts to manage the iTunes software; in addition, you can also use scripts that interact with your iPod. We’re absolutely serious: in this case, Aunt Martha is wrong.
And we’re about to prove it to you.
The Longest Journey Begins with the First Script
Let’s do something a bit out of character for the Scripting Guys and start at the beginning. Here’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:
Set objApp = CreateObject("iTunes.Application") Wscript.Echo "Version: " & objApp.Version
Admittedly, this might not be the most exciting script you’ve ever seen. (If it is the most exciting script you’ve ever seen, well, you really need to get out and look at more scripts.) However, it’s a useful starting point, and for a couple of reasons.
For one, note the first line of code, which simply creates an instance of the iTunes.Application object:
Set objApp = CreateObject("iTunes.Application")
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’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.
Note. 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 – combined with the explanations offered in this article – will be enough to get you going.
And, if all else fails, you can always ask Aunt Martha.
Here’s another little script, one that tells you whether the iTunes application is configured for mini-player mode:
Set objApp = CreateObject("iTunes.Application") Set objWindow = objApp.BrowserWindow Wscript.Echo "Mini-player mode: " & objWindow.MiniPlayer
You’re right: it is 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 BrowserWindow object, something we do simply by creating an object reference (objWindow) to the Application object’s BrowserWindow property:
Set objWindow = objApp.BrowserWindow
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’s fine; just create an object reference to the EQPresets property:
Set colPresets = objApp.EQPresets
Oh, sorry: you said you wanted to retrieve a collection of encoders, didn’t you? Well, then simply create an object reference to the Encoders properties:
Set colEncoders = objApp.Encoders
And so on.
After you have an object reference to the BrowserWindow object all you have to do is echo back the value of the MiniPlayer property:
Wscript.Echo "Mini-player mode: " & objWindow.MiniPlayer
That’s nice, but here’s one of the really cool features of the iTunes application: a large number of the properties are read/write. For example, it’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 put the player into mini-player mode.
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:
Set objApp = CreateObject("iTunes.Application") Set objWindow = objApp.BrowserWindow objWindow.MiniPlayer = True
Just like that, our iTunes application will switch to mini-player mode.
Incidentally, we should mention that all these scripts automatically start and display the iTunes player (if it isn’t already up and running), even the script that simply echoes back the iTunes version number. To tell you the truth, we don’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 Quit 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:
Set objApp = CreateObject("iTunes.Application") Wscript.Echo "Version: " & objApp.Version objApp.Quit
With a Song in Your Heart (or at Least in Your iTunes Library)
You know, you’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’s see what else we can do with an iTunes script. How about this: let’s say we retrieve a list of all the songs in your iTunes library.
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’t worry; the Scripting Guys don’t do hard. Instead, we only do things that are easy, things like retrieve information about all the songs in your iTunes library:
Set objApp = CreateObject("iTunes.Application") Set objLibrary = objApp.LibraryPlaylist Set colTracks = objLibrary.Tracks For Each objTrack in colTracks Wscript.Echo "Name: " & objTrack.Name Set objPlaylist = objTrack.Playlist Wscript.Echo "Playlist: " & objPlaylist.Name Wscript.Echo "Album: " & objTrack.Album Wscript.Echo "Artist: " & objTrack.Artist Wscript.Echo "Bit rate: " & objTrack.BitRate Wscript.Echo "Beats per minute: " & objTrack.BPM Wscript.Echo "Comment: " & objTrack.Comment Wscript.Echo "Compilation: " & objTrack.Compilation Wscript.Echo "Composer: " & objTrack.Composer Wscript.Echo "Date added: " & objTrack.DateAdded Wscript.Echo "Disc count: " & objTrack.DiscCount Wscript.Echo "Disc number: " & objTrack.DiscNumber Wscript.Echo "Duration (seconds): " & objTrack.Duration Wscript.Echo "Checked for playback: " & objTrack.Enabled Wscript.Echo "Artist: " & objTrack.Artist Wscript.Echo "Equalizer preset: " & objTrack.EQ Wscript.Echo "Stop time (seconds): " & objTrack.Finish Wscript.Echo "Genre: " & objTrack.Genre Wscript.Echo "Grouping: " & objTrack.Grouping Wscript.Echo "Kind: " & objTrack.KindAsString Wscript.Echo "Modification date: " & objTrack.ModificationDate Wscript.Echo "Times played: " & objTrack.PlayedCount Wscript.Echo "Last played on: " & objTrack.PlayedDate Wscript.Echo "Play order index: " & objTrack.PlayOrderIndex Wscript.Echo "Rating: " & objTrack.Rating Wscript.Echo "Sample rate: " & objTrack.SampleRate Wscript.Echo "Size (bytes): " & objTrack.Size Wscript.Echo "Start time (seconds): " & objTrack.Start Wscript.Echo "Track length: " & objTrack.Time Wscript.Echo "Total tracks on album: " & objTrack.TrackCount Wscript.Echo "Track number on album: " & objTrack.TrackNumber Wscript.Echo "Volume adjustment: " & objTrack.VolumeAdjustment Wscript.Echo "Year: " & objTrack.Year Wscript.Echo Next
Don’t let the size of the script deter you; it’s a long script simply because we’re returning quite a bit of information about each song. Tell you what; let’s pare this script down a bit and return only the name of each song in the library:
Set objApp = CreateObject("iTunes.Application") Set objLibrary = objApp.LibraryPlaylist Set colTracks = objLibrary.Tracks For Each objTrack in colTracks Wscript.Echo "Name: " & objTrack.Name Next
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’s what we do here:
Set objApp = CreateObject("iTunes.Application") Set objLibrary = objApp.LibraryPlaylist
Once we’re connected to the library we can use the Tracks property to return a collection of all the songs in the library:
Set colTracks = objLibrary.Tracks
From there it’s just a matter of setting up a For Each loop to loop through the collection and echo back the song name:
For Each objTrack in colTracks Wscript.Echo "Name: " & objTrack.Name Next
Will that really work? You bet it will:
Name: Out Here All Night Name: Easy Love Name: Steady, As She Goes (Acoustic Version) Name: Ciaccona Name: Mama's Room
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, track properties) are read/write. But we’ll talk about that next weekend.
That is all there is to using VBScript to work with your iTunes collection. Join me tomorrow as we begin a new week on the Script Center. I invite you to follow me on Twitter or Facebook. If you have any questions, send e-mail to me at email@example.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy