iTunes provides a documented COM API that can easily be used from .NET. You can do lots of cool stuff using F# Interactive, especially if you add some F# extension properties to their provided classes (see iTunesExt.fs). You can checkout the Visual Studio 2010 solution. The below code is committed as Blog_2010-08-24.fsx and you can run what you want a piece at a time using Alt+Enter. The last section of code is another way to remove the dead tracks from iTunes that Scott Hanselman blogged about in July. If you want to see other stuff, clone the repo, or let me know.
#r "iTunes.Net.dll"
#r @"bin\Debug\iTunesExt.dll"
open System
open System.IO
open iTunes
open iTunesExt
// calling this will launch iTunes if not already open
let app = iTunesAppClass()
// see what verion of iTunes your running
app.Version
// control the iTunes app
app.Play()
app.Stop()
app.NextTrack()
app.BackTrack()
app.Mute <- true
app.Mute <- false
app.BrowserWindow.MiniPlayer <- true
app.BrowserWindow.MiniPlayer <- false
// print all playlists
app.Playlists
|> Seq.iter (fun p -> printfn "%s" p.Name)
// print the user playlists that are not smart
app.UserPlaylists
|> Seq.filter (fun p -> false = p.Smart)
|> Seq.iter(fun p -> printfn "%s" p.Name)
// get a list of tracks that are missing files
// they are marked with an '!' in the iTunes UI
let tracksMissingFiles =
app.LibraryPlaylist.Tracks.Files
|> Seq.filter (fun t -> false = File.Exists t.Location)
|> List.ofSeq
// see how many were found
tracksMissingFiles.Length
// print the list
tracksMissingFiles |> Seq.iter (fun t -> printfn "%s" t.Name)
// remove the tracks from iTunes
tracksMissingFiles |> Seq.iter (fun t -> t.Delete())
iTunes.Net.dll was created using tlbimp from the Windows SDK 7.1 and iTunes 9.2.1.5 like so:
set PATH=C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin;%PATH%
tlbimp /out:iTunes.Net.dll /namespace:iTunes /asmversion:9.2.1.5 "C:\Program Files (x86)\iTunes\iTunes.exe"
Also, huge thanks to Microsoft for finally providing a free F# 2.0 + Visual Studio 2010 Shell environment! I used it for this.