Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Apple > iPod

Hacking Your iPod: Building Windows Solutions to Load Content onto Your iPod and iTunes

📄 Contents

  1. Using the Software Developers Kit
  2. Building a Windows iPod Tool
  3. Moving Forward
  • Print
  • + Share This
Like your iPod? But don't you wish it could do just one more thing? Stop complaining and start programming! Matthew David exposes you to the fundamentals of programming iTunes to store more on your iPod.
Like this article? We recommend

Over the last few months, I have written a number of articles about extending the capabilities of the iPod. This article gives you Windows developers the ultimate trip (yes, the secrets of developing iPod and iTunes solutions for the Windows platform!).

To begin developing solutions for iPod and iTunes, you need to download and review the Software Developers Kit, or SDK, developed by Apple for Windows iTunes and iPod development.

After reviewing these materials, you need to jump into the development of your first application. Fortunately for you, my friends over at SharePod have allowed us to use the Open Source code for their iPod tool. Yes, that means you get to see an entire application that you can develop today. No need to dismantle code from meaningless examples in the SDK.

On to the code!

Using the Software Developers Kit

You have to review and then download the Apple SDK, and there are several you can look into. One that I really like is the iTunes' SDK. The iPod is really a hard drive that lets you look at text, photos, and music. The only challenge is that unless you load LINUX onto your iPod (which you can do), you can't program solutions directly to the OS running the software on your iPod. The tool that you can control is iTunes.

The iTunes SDK comes loaded with JScript code that you can use to add commonly used features on your iTunes. For instance, the following script allows you to create a new album playlist:

var iTunesApp = WScript.CreateObject("iTunes.Application");
var mainLibrary = iTunesApp.LibraryPlaylist;
var mainLibrarySource = iTunesApp.LibrarySource;
var tracks = mainLibrary.Tracks;
var numTracks = tracks.Count;
var numPlaylistsCreated = 0;
var i;

// FIXME take a -v parameter eventually
var verbose = false;

// first, make an array indexed by album name
var albumArray = new Array();

for (i = 1; i <= numTracks; i++)
{
   var currTrack = tracks.Item(i);
   var album = currTrack.Album;
   
   if ((album != undefined) && (album != ""))
   {
     if (albumArray[album] == undefined)
     {
        if (verbose)
          WScript.Echo("Adding album " + album);
        albumArray[album] = new Array();
     }
     
     // add the track to the entry for this album
     albumArray[album].push(currTrack);
   }
}

for (var albumNameKey in albumArray)
{
   var albumPlayList;
   var trackArray = albumArray[albumNameKey];

   if (verbose)
     WScript.Echo("Creating playlist " + albumNameKey);
   
   numPlaylistsCreated++;
   
   albumPlaylist = iTunesApp.CreatePlaylist(albumNameKey);
   
   for (var trackIndex in trackArray)
   {
     var    currTrack = trackArray[trackIndex];
     
     if (verbose)
        WScript.Echo("  Adding " + currTrack.Name);
     
     albumPlaylist.AddTrack(currTrack);
   }
}

if (numPlaylistsCreated == 0)
{
   WScript.Echo("No playlists created.");
}
else if (numPlaylistsCreated == 1)
{
   WScript.Echo("Created 1 playlist.");
}
else
{
   WScript.Echo("Created " + numPlaylistsCreated + " playlists.");
}

Apple has done us a big favor: All its code is in JavaScript. In many ways, JavaScript is the universal scripting language. You can easily step through it and build more robust solutions. For example, here is the same code modified to remove a playlist:

var ITPlaylistKindUser = 2;
var iTunesApp = WScript.CreateObject("iTunes.Application");
var deletedPlaylists = 0;
var mainLibrary = iTunesApp.LibrarySource;
var playlists = mainLibrary.Playlists;
var numPlaylists = playlists.Count;

while (numPlaylists != 0)
{
   var currPlaylist = playlists.Item(numPlaylists);
   
   // is this a user playlist?
   if (currPlaylist.Kind == ITPlaylistKindUser)
   {
     // yes, is it a dumb playlist?
     if (!currPlaylist.Smart)
     {
        try
        {
          // yes, delete it
          currPlaylist.Delete();
          deletedPlaylists++;
        }
        catch (exception)
        {
          // ignore errors (e.g. trying to delete a locked playlist)
        }
     }
   }
   
   numPlaylists--;
}

if (deletedPlaylists > 0)
{
   if (deletedPlaylists == 1)
   {
     WScript.Echo("Removed 1 user playlist.");
   }
   else
   {
     WScript.Echo("Removed " + deletedPlaylists + " user playlists.");
   }
}
else
{
   WScript.Echo("No user playlists were removed.");
}

Being able to control playlists is critical for your control over the iPod. The iPod builds its playlists from those constructed in iTunes. Being able to construct playlists allows you to synchronize files that are downloaded over the Internet—such as those from Pod Cast XML streams—right into iTunes and your iPod.

The next step is to create your own application.

  • + Share This
  • 🔖 Save To Your Account