January 07, 2006

How to control iTunes from any Microsoft Office application

itunes in excel

Summary

This post describes how to interact with and control iTunes from any Microsoft Office application in Windows. Specifically, it shows how to:

Downloads

If you don't want to read the boring explanation about how to do these things, and you just want to download the files that already do them, here you go:

To use the Word and Excel files, run the macros in the Tools:Macro menu. Note that you will probably get all kinds of warnings about macro security and so on when you open the files, so you'll have to figure out how you want to handle that. Or you can also just get the source code from these text files, but you'll need to know how to connect that code to macros, etc. Please also note that these files are offered primarily as tutorials, not as complete applications, so they are likely to be buggy and not fail gracefully. Also, it's probably helpful to open iTunes before opening any of these documents. You can file bug reports in the comments, and I'll try to fix them when I'm able.

Technical Background

Many Windows applications, including iTunes, provide "libraries" of functionalities to other applications. Certains kinds of these libraries are called "COM objects", and they tend to be relatively easy to work with. Fortunately, Apple provides one of these for iTunes, and Visual Basic for Applications (VBA), the scripting language provided with Microsoft Office, can make use it. Using VBA, you can tap into the iTunes COM, and manipulate iTunes objects like playlists, tracks, and even the playback functionality. This post describes how to do so.

Getting Started

Interacting with the iTunes Library

Let's start by connecting a Word document to iTunes, so you can output a list of all the songs in your iTunes library to a Word document.

  1. Open iTunes.
  2. Open Word.
  3. From the Tools menu, select Macro, then Macros.
  4. Type the name of the macro you want to create ("Show_iTunes_Library" might be a good name), and click Create. A VBA editing window will appear. This is where you will type the code that will make up your macro.
  5. Before you can make VBA do anything with iTunes, you need to tell it where to find the library of iTunes functions it can use. From the Tools menu, select References... Go through the list until you find "iTunes 1.2 Type Library", check it, and click OK.
  6. Back in the VBA editing window, paste this text in between the first and last lines of your subroutine:
    
    Dim iTunes As iTunesApp
    Dim tr As IITTrack
    
    Set iTunes = New iTunesApp
    
    For Each tr In iTunes.LibraryPlaylist.tracks
        ActiveDocument.Range.InsertAfter (tr.Name & vbCrLf)
    Next
    
    Set iTunes = Nothing
    
  7. Let's take another look at that code and see what it all does:
    
    'declare an object variable for the iTunes application and an iTunes "track" (song)
    Dim iTunes As iTunesApp
    Dim tr As IITTrack
    
    '"instantiate" the class, i.e., prepare it for use.
    Set iTunes = New iTunesApp
    
    'go through each track in the LibraryPlaylist
    For Each tr In iTunes.LibraryPlaylist.tracks
        'in the active document, insert the track's name (i.e., song title) 
        'and a new line (carriage return/line feed)
        ActiveDocument.Range.InsertAfter (tr.Name & vbCrLf)
    Next
    
    'reset the iTunes object to free up the memory used in this code
    set iTunes = Nothing
  8. Back in your Macros dialog box, click the name of the macro you just created and run it. It should output all the track info in your library to the active document in Word. (There is code in the Word document above that shows how to convert this data into a table automatically.)

This is just a basic example to show how to iterate your iTunes song library in VBA. If you wanted to have more info about each track appear, you can change the tr.Name part to include things like tr.Artist, tr.Album, tr.PlayedDate, etc. The VBA programming environment should display all the properties available to you as you start typing them. For example, if you declare tr as an iTunes track object, when you type tr., all the available properties and actions (aka "methods") will appear in a drop-down selection box text to the variable name.

Controlling iTunes Playback

Similarly, to make the iTunes application itself take certain actions, you can do thing like iTunes.Play or iTunes.Pause, assuming your code has an iTunesApp object called iTunes. The names of the available actions ("methods") will appear when you type the name of the object variable followed by a period.

For example, we can make a simple form in Microsoft Access that will control iTunes playback. This will probably go more smoothly if you've had a little experience designing forms in Access.

  1. Open iTunes.
  2. Open Microsoft Access,
  3. In the database window, choose Forms, and create a new form using Design View.
  4. Add two buttons to the form. Name one cmdPlay and one cmdPause.
  5. Set the captions to "Play" and "Pause" accordingly.
  6. In the properties for cmdPlay, go to the Events tab, and in the On Click property, click "..." to create a new subroutine that will run when the button is clicked.
  7. When the VBA window opens, set your references to the iTunes 1.2 Type Library as described above.
  8. In the subroutine for cmdPlay_Click, enter this code:
    
         Dim iTunes As iTunesApp
         Set iTunes = New iTunesApp
         iTunes.Play
         Set iTunes = Nothing
    
  9. Go back to your form, and in the Click event for cmdPause, create a new subroutine that looks like this:
    
    Private Sub cmdPause_Click()
          Dim iTunes As iTunesApp
          Set iTunes = New iTunesApp
          iTunes.Pause
          Set iTunes = Nothing
    End Sub
    
    
  10. Open your form in Form view, and click the Play button. It should work.

If you want more information on all the iTunes objects, properties, and methods available to you, check out Apple's iTunes Software Development Kit for Windows. You can pretty much ignore all of it except for the help file (iTunesCOM.chm) which, in a somewhat arcane manner, describes each of the classes (objects) and their properties and methods. Unfortunately, the help file is heavy on specification and light on examples, but using the examples and downloadable files I provided above, you should be able to figure it out with a bit of those critical thinking skills you picked up when you were younger.

Next Steps

Once you've figured out how to iterate through the tracks in your library, you are pretty much only limited by your knowledge of how VBA works in each of the Microsoft Office applications. If you know how to use VBA to create new documents, or worksheets, or slides, or address book contacts, or whatever, it's fairly simple to manipulate an iTunes library, playlist, or track to do whatever you want with the Office application of your choice. For example, the downloadable Excel spreadsheet offered above loops through all your playlists, creates a worksheet for each one, and adds the tracks from each playlist to the appropriate spreadsheet, along with the last played date and the play count. It will also play a track when you click on a track name and run the "play track" macro. The Access database provided above shows you track info for the currently playing song, allows you to jump to any song you type in, and lets you update the mp3 tags for the songs in your iTunes Library. The Word document enters all the tracks in your iTunes library into a table. Examine the source code to come up with your own projects, and have fun integrating Microsoft Office with iTunes.

categories: How To, Software
posted by adm at January 7, 2006 07:54 PM

Trackback Pings

TrackBack URL for this entry:
http://www.thousandrobots.com/mt/mt-tb.cgi/1352

Comments

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)


Remember me?