January 2006 Archives
I made a Mac OS X Dashboard widget that displays the current playlist from RadioParadise.com, a great internet radio station. It's my first ever widget, so it's not very fancy, but it gets the job done.
The widget shows a scrollable list of the last 6 hours worth of music played, and if you click a song title, it opens the web page on RP's site dedicated to that song. Clicking the main logo takes you to RP's site. It refreshes when you show the Dashboard and you can manually refresh it by clicking the "refresh" link. I may add automatic refreshing in some future release.
Once you've downloaded the widget, unzip it by double-clicking it, then double-click the widget file. For older versions of Mac OS X, you might need to move the files into your home folder's /Library/Widgets folder first.
Apple doesn't seem to publicize this fact, but practically anyone can make a widget. Simple widgets are really just small web pages, and can be made using only HTML, JavaScript, and a couple of images. You should give it a try. I just read Apple's excellent tutorial, looked at the source code for one of their RSS-reading sample widgets, and went from there.
This widget uses JavaScript to extract data from an XML version of RP's playlist. I believe it is technically an AJAX-based widget, albeit a very simple one. A few weeks ago, I wrote a PHP script that generates an RSS feed from that XML.
I'm guessing it's trivial to convert Mac OS X widgets to Konfabulator/Yahoo! Widgets, so I will do that when I get a minute.
Summary
This post describes how to interact with and control iTunes from any Microsoft Office application in Windows. Specifically, it shows how to:
- Create an Excel spreadsheet that lists all the tracks in your iTunes library, organized by playlist
- Use an Access database to update the mp3 ID tags of your songs in iTunes
- Create a Microsoft Word document that lists all your iTunes tracks info in a table
- Control iTunes playback from any Office application
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:
- Excel spreadsheet that loads your iTunes library info and lets you play any selected song
- Access database that lets you browse your collection, update tags, and control playback
- Word document that displays all your iTunes tracks in a table
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.
- Open iTunes.
- Open Word.
- From the Tools menu, select Macro, then Macros.
- 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.
- 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.
- 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 - 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 - 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.
- Open iTunes.
- Open Microsoft Access,
- In the database window, choose Forms, and create a new form using Design View.
- Add two buttons to the form. Name one cmdPlay and one cmdPause.
- Set the captions to "Play" and "Pause" accordingly.
- 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.
- When the VBA window opens, set your references to the iTunes 1.2 Type Library as described above.
- In the subroutine for cmdPlay_Click, enter this code:
Dim iTunes As iTunesApp Set iTunes = New iTunesApp iTunes.Play Set iTunes = Nothing - 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 - 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.

