Ratnakar’s Weblog

playlists based on the folders (using the iTunes SDK)..

Posted in General I.T by Ratnakar Garikipati on November 19, 2009

Huge relief that I finally have managed to reorganise my music on my home server (was putt off for a while now:))

Realised that a number of people on the net are looking to do some of the things that I’ve done such as cleaning up the duplicates, removing the dead ones, recreating the playlists based on the folder structure on the server.

Here’s a snippet that recreates playlists based on the folders  (obviously in C#) – 

             iTunesApp app = new iTunesApp();
            var mainLibrary = app.LibraryPlaylist;
            var mainLibrarySource = app.LibrarySource;
            var tracks = mainLibrary.Tracks;
            var numTracks = tracks.Count;
            var numPlaylistsCreated = 0;
            var numTracksAddedToPlayLists = 0;
         
            var playlists = mainLibrarySource.Playlists;
             for (var i = 1; i <= numTracks; i++)
            {
   
            // get the track from the current tracklist                
            IITTrack currTrack = tracks[i] as IITFileOrCDTrack;
            // only work with files

         if (currTrack.Kind == ITTrackKind.ITTrackKindFile)
         {
         IITFileOrCDTrack file = (IITFileOrCDTrack)currTrack;
                if (file.Location != null)
                {
                    var p =file.Location;
        
                    var location = p; 
                 if ((location != null) && (location != “”))
                 {
                 FileInfo fi = new FileInfo(file.Location);
                       if (fi.Exists)
                       {
                           string dir = fi.Directory.Name;

                           if (app.LibrarySource.Playlists.get_ItemByName(dir) == null)
                           {
                              var currPL = app.CreatePlaylist(dir) as IITUserPlaylist;
                              numPlaylistsCreated++;

                              currPL.AddFile(location);
                              numTracksAddedToPlayLists++;
                             
                           }
                           else
                           {
                               IITUserPlaylist currPL = app.LibrarySource.Playlists.get_ItemByName(dir) as IITUserPlaylist;

                               IITTrackCollection iTracksInPlayList = currPL.Tracks;
                               if (iTracksInPlayList.get_ItemByName(currTrack.Name) == null)
                               {
                                   currPL.AddFile(location);
                                   numTracksAddedToPlayLists++;
                               }
                           }
                       }
                   }
                 }
         }


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: