Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / Windows Forms

Manage your iTunes library with simple clicks!

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
4 Feb 2007CPOL4 min read 57K   919   35   13
This project, along with the suggested form of use, will let you organize your iTunes libraries and make them ready for iPod Sync, making use of a reusable progress window that has a cancel button.

Introduction

There are two reasons I wanted to post this article. One of them is to suggest a solution to the problem of sync-ing large iTunes libraries with not-so-large iPods. The other is to introduce a reusable progress window form, that can cancel the process it is called from and can be forced to close upon the finishing of the process.

If you are reading this article, chances are you too are a fellow victim of a huge MP3 song library, a portion of which you never even have played! To make things worse, you bought an iPod that can handle only a portion of these songs, so you cannot simply put all those songs into the iPod.

Now you are stuck either syncing your iPod completely manually, or sitting down and cleaning your iTunes library from unwanted songs. I couldn't have done that, since I am usually at the computer for a reason, generally more important than cleaning up my MP3 files.

Here is my approach to go about cleaning your iTunes library.

To be carried out once:

  • Start by removing duplicates (i.e., multiple library entries that map to the same physical MP3 file).
  • Delete library entries with missing files.

To be carried out while listening on your iPod or computer:

  • Have you found a song you want deleted? Rate it with 1 star.
  • Have you found a song you want to keep only on your computer, but not on your iPod? Rate it with 2 stars.
  • Have you found a song you would like to keep on your iPod? Rate it with 3 to 5 stars depending on how much you like it.

To be carried out before iPod Sync:

  • Have a separate playlist that will sync with iPod. Put all the songs that are 3 stars and above in it. Depending on the capacity of iPod, put random songs into the playlist to fill up the iPod (can be very time-consuming).

Fortunately, iTunes has provided the SDK for iTunes as a COM component, so we can manipulate the library from .NET code. And that is exactly what this project is about. All steps described above, with the exception of rating the songs, are handled by the project.

Main screen

When you run the application, it will display the number of songs that are in your iTunes library.

Sample image

Let's take a look at what each of these buttons do, starting from the ones that we are likely to use once.

Delete entries with missing files:

This button starts a process that will remove all the library entries with missing files. These tracks are generally a result of moving files around from the Windows Explorer.

Remove duplicates:

This button removes multiple track entries in the library that map to the same physical MP3 file.

Sample image

The superstar! Create favourites playlists:

This button is the reason I wrote the application. Here is a list of playlists it creates:

  • Your iPod!: This playlist consists of songs that you have rated 3 and above, along with a selection of random songs that are not rated, to fill up your iPod.
  • Your Favourites!: All tracks rated 3 or above will be in this playlist.
  • The Rest!: All files that are not rated will be in this playlist.

Once you click the button, the system is going to ask you for your iPod capacity.

Sample image

Then it will start to delete the entries within the playlists it will create, so they will be refreshed.

Sample image

Then it will start to fill the playlists up with the songs.

Sample image

It will then fill up the remaining space on the Your iPod! playlist.

Sample image

Now you can sync your iPod with the playlists above (and optionally with the iTunes automatic playlist of "My Top Rated", since all files in it will already be in both Your iPod! and Your Favourites! playlists) and your iPod is set to go!

My suggestion is to sync your iPod before using the Create Favourites Playlists button so that the newest rated songs will be taken into account when creating the playlists.

Progress window and its usage

The heart of the form is the code below. Once started, it binds itself to two events: UpdateProgressBar and StatusDone. These are declared on a Module called MLibrary.

VB
Public Event UpdateStatus(ByRef Cancel As Boolean, ByVal CurrentNumber As Integer)
Public Event StatusDone(ByVal ForceClose As Boolean)
VB
Private mbCancel As Boolean = False

Public Shared Sub Start(ByVal Headline As String, ByVal Total As Integer)
    Dim oStatus As New frmStatus
    oStatus.lblHeadline.Text = Headline
    oStatus.Text = Headline
    oStatus.pb.Maximum = Total
    oStatus.Show()
    AddHandler MLibrary.UpdateStatus, AddressOf oStatus.UpdateProgress
    AddHandler MLibrary.StatusDone, AddressOf oStatus.StatusDone
End Sub

Private Sub UpdateProgress(ByRef Cancel As Boolean, _
            ByVal CurrentNumber As Integer)
    pb.Value = CurrentNumber
    lblStatusText.Text = CurrentNumber & " of " & pb.Maximum
    Application.DoEvents()
    btnOk.Enabled = CurrentNumber = pb.Maximum
    btnCancel.Enabled = Not btnOk.Enabled
    Cancel = mbCancel
    If Cancel Then Me.Close()
End Sub

Private Sub StatusDone(ByVal ForceQuit As Boolean)
    If ForceQuit Then
        btnOk_Click(Nothing, Nothing)
    Else
        btnOk.Enabled = True
        btnCancel.Enabled = False
    End If
End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnCancel.Click
    mbCancel = True
End Sub

Private Sub btnOk_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnOk.Click
    RemoveHandler MLibrary.UpdateStatus, AddressOf Me.UpdateProgress
    RemoveHandler MLibrary.StatusDone, AddressOf Me.StatusDone
    Me.Close()
    Me.Dispose()
End Sub

Here is how the code is used. Let's take a simple example as finding the tracks with missing files:

VB
Dim iCounter As Integer = 0
For Each oTrack As IITTrack In moTunes.LibraryPlaylist.Tracks
    If oTrack.Kind = ITTrackKind.ITTrackKindFile Then
        Dim oFileTrack As IITFileOrCDTrack = oTrack
        If oFileTrack.Location = "" Then
            ' Add to the list of tracks to be deleted
            alTracksToBeDeleted.Add(oFileTrack)
        End If
    End If
    iCounter += 1
    Dim bCancel As Boolean = False
    RaiseEvent UpdateStatus(bCancel, iCounter)
    Application.DoEvents()
    If bCancel Then Exit For
Next
RaiseEvent StatusDone(alTracksToBeDeleted.Count > 0)

The above code is pretty self-explanatory. On every iteration, we raise the UpdateStatus event. When we are done, we raise the StatusDone event with an optional parameter for closing the form without the user's intervention.

If you have any questions on how to use the form or how to use the iTunes Library Manager, let me know.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCool App Pin
Johannes Kleiber19-May-09 22:45
Johannes Kleiber19-May-09 22:45 
Generalunable to find to open Pin
Marcus Mack8-Mar-09 6:42
Marcus Mack8-Mar-09 6:42 
I downloaded and unzipped the file but I don't know where to find it or how to open. Should there be an icon? (Sorry for such a stupid question.)
GeneralRe: unable to find to open Pin
Igal Nassi9-Mar-09 5:02
Igal Nassi9-Mar-09 5:02 
QuestionPossible issue Pin
MikeHamilton9-Feb-07 6:00
MikeHamilton9-Feb-07 6:00 
AnswerRe: Possible issue Pin
igalnassi9-Feb-07 13:39
igalnassi9-Feb-07 13:39 
QuestioniTunes library Pin
chaldon6-Feb-07 23:50
chaldon6-Feb-07 23:50 
AnswerRe: iTunes library Pin
igalnassi7-Feb-07 15:14
igalnassi7-Feb-07 15:14 
AnswerRe: iTunes library Pin
chaldon8-Feb-07 4:33
chaldon8-Feb-07 4:33 
AnswerRe: iTunes library Pin
igalnassi10-Feb-07 12:38
igalnassi10-Feb-07 12:38 
GeneralRe: iTunes library Pin
chaldon11-Feb-07 23:30
chaldon11-Feb-07 23:30 
AnswerRe: iTunes library Pin
igalnassi10-Feb-07 12:40
igalnassi10-Feb-07 12:40 
GeneralRe: iTunes library Pin
chaldon11-Feb-07 23:24
chaldon11-Feb-07 23:24 
GeneralRe: iTunes library Pin
igalnassi14-Feb-07 17:53
igalnassi14-Feb-07 17:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.