iCame, iPod, iScripted: Scripting iTunes part 2.

ScriptingGuy1

 

[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 for part one.

Bonus Script: Printing Out Your Song Collection

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’s how:

Const ITPlaylistPrintKindPlaylist = 0

Set objApp = CreateObject(“iTunes.Application”)

Set objPlaylist = objApp.LibraryPlaylist

objPlaylist.Print False, ITPlaylistPrintKindPlaylist, “Songs”

Don’t worry, we’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’s a list of the categories, their constants, and their values:

Constant

Value

Description

ITPlaylistPrintKindPlaylist

0

Prints a list of tracks in the playlist

ITPlaylistPrintKindAlbumlist

1

Prints a list of albums in the playlist

ITPlaylistPrintKindInsert

2

Prints a CD jewel case insert

After defining the constant we create an instance of the iTunes.Application object and then connect to the iTunes library.

Note. We haven’t talked about user-created playlists yet, but the technique we’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.

All we need to do now is call the Print method:

objPlaylist.Print False, ITPlaylistPrintKindPlaylist, “Songs”

You probably noticed that the Print method takes three parameters. The first parameter – False – simply tells iTunes to print to the default printer. Had we set this parameter to True then iTunes would have displayed the Print dialog box rather than automatically printing out the requested information.

Parameter 2 is also pretty straightforward: it’s the constant ITPlaylistPrintKindPlaylist which, again, indicates what we want to print. That leaves us with one final parameter: “Songs”.

Songs is nothing more than the print “theme” 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’s easy: from within iTunes we clicked File and then clicked Print. In the Print “Library” dialog box we clicked Song listing and then clicked the Theme drop-down list:

clip_image002

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’s third parameter. Cool, huh?

Here’s another printing example. Suppose we burned all the songs in the library to a CD. (Granted, that means you either don’t have very many songs in your Library or you’re making one big CD. But play along with us for now.) Here’s a script that prints a jewel case insert to accompany that CD:

Const ITPlaylistPrintKindInsert = 2

Set objApp = CreateObject(“iTunes.Application”)

Set objPlaylist = objApp.LibraryPlaylist

objPlaylist.Print True, ITPlaylistPrintKindInsert, “White mosaic”

Very cool.

Name – and then Bind to – That Tune

Being able to retrieve – and print – 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.

Retrieving a Song by Name

Perhaps the easiest way to zero in on a specific song is to connect to the iTunes library and then use the ItemByName method to bind to the desired track. In other words:

Set objApp = CreateObject(“iTunes.Application”)

Set objLibrary = objApp.LibraryPlaylist

Set colTracks = objLibrary.Tracks

Set objSong = colTracks.ItemByName(“Do Right By You”)

objSong.Play

Again, pretty straightforward. We start by – stop us if you’ve heard this one before – 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:

Set colTracks = objLibrary.Tracks

From there we call the ItemByName method, passing as the sole parameter the name of the song in question:

Set objSong = colTracks.ItemByName(“Do Right By You”)

That’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, Play:

objSong.Play

Searching for a Song (or Songs)

The iTunes object model also makes it easy to search the library for a specified collection of songs. For example, here’s a script that searches the library and returns a list of songs that have the word Acoustic in one of their property values (for example, in the Genre field):

Const ITPlaylistSearchFieldAll = 0

Set objApp = CreateObject(“iTunes.Application”)

Set objLibrary = objApp.LibraryPlaylist

Set colTracks = objLibrary.Search(“Acoustic”, ITPlaylistSearchFieldAll)

For Each objSong in colTracks

Wscript.Echo objSong.Name

Next

How does this script work? Thanks for asking; if you hadn’t asked, we’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:

Search Type Constant

Value

Description

ITPlaylistSearchFieldAll

0

Search all fields of each track.

ITPlaylistSearchFieldVisible

1

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.

ITPlaylistSearchFieldArtists

2

Search only the artist field of each track.

ITPlaylistSearchFieldAlbums

3

Search only the album field of each track.

ITPlaylistSearchFieldComposers

4

Search only the composer field of each track.

ITPlaylistSearchFieldSongNames

5

Search only the song name field of each track.

After creating the iTunes.Application object and binding to the library, we then execute this line of code:

Set colTracks = objLibrary.Search(“Acoustic”, ITPlaylistSearchFieldAll)

Here we’re simply calling the Search method and passing it a pair of parameters: “Acoustic” (which happens to be the value we’re search for) and the constant ITPlaylistSearchFieldAll (which, again, represents the fields we want to search). Assuming the value Acoustic can be found somewhere in the library we’ll get back the appropriate collection, a collection we can then walk through using a simple For Each loop:

For Each objSong in colTracks

Wscript.Echo objSong.Name

Next

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.

What’s that? We haven’t talked about custom playlists yet? That’s a wrong we need to right. And right now.

But first, another Scripting Guys bonus.

 

Bonus Script: Ripping Songs from a CD to Your iTunes Library

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’t been digitized yet. We won’t go so far as to claim that scripts will make it a breeze to digitize all your existing CDs. However, you can write a simple little script that will automatically “rip” all the songs from a music CD and add those songs to your iTunes library:

Const ITSourceKindAudioCD = 3

Set objApp = CreateObject(“iTunes.Application”)

Set colSources = objApp.Sources

For Each objSource in colSources

If objSource.Kind = ITSourceKindAudioCD Then

strName = objSource.Name

Set colPlaylists = objSource.Playlists

Set objPlaylist = colPlaylists.ItemByName(strName)

Set colTracks = objPlaylist.Tracks

objApp.ConvertTracks2(colTracks)

End If

Next

We won’t discuss this bonus script in great detail; suffice to say that we bind to the Sources object (something we’ll talk about later) and look for any music CD source. This is the line of code that checks for the source Kind:

If objSource.Kind = ITSourceKindAudioCD Then

As you can see, we’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:

Source Type Constant

Value

Description

ITSourceKindUnknown

0

Unknown source kind

ITSourceKindLibrary

1

Library source

ITSourceKindIPod

2

iPod source

ITSourceKindAudioCD

3

Audio CD source

ITSourceKindMP3CD

4

MP3 CD source

ITSourceKindDevice

5

Device source

ITSourceKindRadioTuner

6

Radio tuner source

ITSourceKindSharedLibrary

7

Shared library source

If we do 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 ConvertTracks2 method to automatically import the songs (and their properties) to the Library:

objApp.ConvertTracks2(colTracks)

It’s that easy.

Note. 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 are connected to the Internet iTunes will automatically go out and retrieve information about the CD and all the songs on it.

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 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