Using the iTunes SDK to Remove Orphaned Songs

:apple:

I looked at the iTunes SDK in “Scripting iTunes.. A look at Apple’s iTunes SDK” and I have shared with you a script I have wrote to eliminate duplicate song entries in your iTunes library in “Using the iTunes SDK to Remove Duplicate Songs“.   I have used the feedback to help improve this script and am working on a way to remove the orphaned (or dead) song entries using VBScript but I wanted to provide this as a stand alone script as well as incorporate it in the remove duplicate script.

My goals for this script are:

  • Use VBScript as a scripting language (the SDK contains this code in Javascript, so I needed to rewrite it).
  • Find song entries that do not have a corresponding file and remove the entries from the iTunes library.
  • Provide the code as a standalone script as well as being able to use the code in the remove duplicates script.
As stated in the previous article I do have experience in Visual basic and it is just a preference in this project.  A person could have just as easily wrote this in JavaScript they chose to.  

Let’s take a look at the script….

The first part should look familiar if you are familiar with the remove duplicates script.   We are creating our initial iTunes objects so we can communicate with the iTunes application
    set objApp = CreateObject(”iTunes.Application”)
    set objLibrary = objApp.LibraryPlaylist
    set colTracks = objLibrary.Tracks


Next we set an empty value to keep track of the number of songs that were orphaned and deleted from the library.
    OrphanTracks = 0

Now we loop through all the tracks in the library and fine the ones that have an empty location.   If an empty location is found, it is removed from the library and the “OrphanTracks” counter in increased by one.
    For Each objTrack in colTracks
        if objTrack.Kind = ITTrackKindFile then
            if objTrack.Location = “” then
                objTrack.Delete()
                OrphanTracks = OrphanTracks + 1
            end if
        end if
    next


After we go through the list of songs, the script then lets us know now many songs were removed from the library.
    if OrphanTracks > 0 then
        if OrphanTracks = 1 then
            WScript.Echo(”Removed 1 orphaned track.”)
        else
            WScript.Echo(”Removed ” + OrphanTracks + ” orphaned tracks.”)
        end if
    else
        WScript.Echo(”No orphan tracks were found.”)
    end if


If you are familiar with VBScript, the script is pretty straight forward.  No files are deleted from hard drive, just removed from the library. 

Please leave me a comment if you found this script useful or have any questions.

You can download the commented script using the following link:

removeorphantracks.zip


Technorati Tags: , , StumbleUpon It!

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Shouldn’t that be objTrack.delete() instead of currTrack.delete()? I’m a beginning scripter so don’t throw rocks at me…lol

No rocks coming from over here… You are correct, it should be objTrack. I made the change in the article and the download. Thanks

Jeff.

[...] Register « Using the iTunes SDK to Remove Orphaned Songs Scripting iTunes with VBScript » [...]

Leave a comment

(required)

(required)