List iTunes album names: Display a list with An AppleScript app

iTunes album list AppleScript FAQ: How can I create a list of iTunes album names (a unique list of all iTunes album names)? (Also written as: Can you help me create an AppleScript to list all iTunes album names?)

As mentioned in my previous article, for a variety of reasons I'd really like to be able to generate a unique list of iTunes album names. For some reason iTunes doesn't let you do this well, so in my previous article I showed how to create an iTunes album list using iTunes.

In this article I'll show the source code for an iTunes AppleScript program that (in my opinion) generates a much nicer, unique iTunes album list.

Note: This program only works on Mac computers. Sorry, but to the best of my knowledge AppleScript only runs on Mac OS X, and that's all I have to test against.

An iTunes album list - The AppleScript source code

I'm not going to explain this script very much unless a lot of people write and want to know a lot more about how it works. So, first, here's my AppleScript source code to generate a unique iTunes album list:

#-------------------------------------------------------------
# PROGRAM: iTunes Album List
# PURPOSE: Display a list of unique, sorted iTunes album names
# DATE:    March 16, 2011
# Version: 0.1
#-------------------------------------------------------------
# COPYRIGHT: 
# copyright 2011, alvin j. alexander, http://devdaily.com
# This work is licensed under a Creative Commons 
# Attribution-ShareAlike 3.0 Unported License;
# see http://creativecommons.org/licenses/by-sa/3.0/ 
# for more information.
#-------------------------------------------------------------
# the simple_sort function below was found on this website:
# http://www.macosxautomation.com/applescript/sbrt/sbrt-05.html
# and has its own (unknown) license.
#-------------------------------------------------------------

tell application "iTunes"
  
  with timeout of 300 seconds
    
    activate
    
    # the list we'll populate
    set listOfAlbumNames to {}
    set itunesLibrary to a reference to (get view of front window)
    set totalNumberOfTracks to (count of every track in itunesLibrary)
    
    # loop through each track, adding unique album names to the list    
    repeat with i from 1 to totalNumberOfTracks
      try
        set currentTrack to (a reference to track i of itunesLibrary)
        set currentAlbumName to (currentTrack's album as string)
        if currentAlbumName is not "" then
          if listOfAlbumNames does not contain currentAlbumName then
            copy currentAlbumName to end of listOfAlbumNames
          end if
        end if
      end try
    end repeat
    
  end timeout
  
end tell

# sort the album list
set sortedAlbums to simple_sort(listOfAlbumNames)

# display in a dialog
set selectedAlbum to {choose from list sortedAlbums}

# see the url above for this function
on simple_sort(my_list)
  set the index_list to {}
  set the sorted_list to {}
  repeat (the number of items in my_list) times
    set the low_item to ""
    repeat with i from 1 to (number of items in my_list)
      if i is not in the index_list then
        set this_item to item i of my_list as text
        if the low_item is "" then
          set the low_item to this_item
          set the low_item_index to i
        else if this_item comes before the low_item then
          set the low_item to this_item
          set the low_item_index to i
        end if
      end if
    end repeat
    set the end of sorted_list to the low_item
    set the end of the index_list to the low_item_index
  end repeat
  return the sorted_list
end simple_sort

Next, here's the dialog box that is displayed after this script runs:

A list of unique iTunes album names

Besides my taste in music, that dialog shows the beginning of my list of unique iTunes album names.

iTunes album list - Discussion

There are a few things I should mention about this iTunes Unique Album List script:

  1. This may take a while to run, so in a future version I'll add a progress indicator. On my newer model iMac it takes about 15 seconds to display the dialog for 4,000 songs, but on my older MacBook Pro it takes about 100 seconds to display the dialog for ~3,000 songs.
  2. The timeout shown in the code doesn't work as it's currently written, at least not on Mac OS X 10.5. (I'll test it again on Mac 10.6 when I get back to my iMac.) So, if the app has run for several minutes and you still haven't seen the output dialog, you can kill the script in your Mac Dock. Just right-click the icon and then select Quit or Force Quit.
  3. The output is displayed in a dialog, as you can see in the image above. That's because in a next version I want to be able to play a selected album from this dialog.
  4. If I get a lot of requests I could modify this script to write the iTunes album list to a plain text file or HTML file, but right now I'm going to leave it like this.

Unique iTunes album list - Download

License: First, note that this code is released under the terms of the Creative Commons Attribution Share-Alike License. In addition to what that license says about the source code, it also means I'm not liable if your computer explodes for some reason. All I can say is it works fine on my iMac running Mac OS X 10.6 and my MacBook Pro running Mac OS X 10.5.

Download Option 1 - The Mac Application

If you'd rather just use the code and not know anything about it, you can download the script with the following links. First, if you just want to run the application and you never want to modify the source code, use this link:

Download that zip file, unzip it, and then you should be able to run it like a normal Mac application.

Download Option 2 - The AppleScript Source File

Alternatively, if you want an AppleScript program you can run AND you want to be able to look at or modify the source code, use this link:

In this case download this file, unzip it, and then you should be able to run it like a normal AppleScript script -- copy it to the Library/Scripts folder under your home directory. For instance, my home directory is /home/al, so my Scripts folder is:

/home/al/Library/Scripts

Once you copy the file there, you can run it from the AppleScript menu on the upper-right of your Mac menu bar. This script should show up on that menu. (There are other ways to save AppleScript files to different locations, this is just one approach.)

Unique iTunes album list - Performance

This script takes a while to run. It takes about 15 seconds on my newer model iMac with about 4,000 songs in my iTunes library, and a whopping 100 seconds to run on my older MacBook Pro with a little more than 3,000 songs in its library. I'll see what I can do about that in future versions of the program, but that's the way it is today.

Unique iTunes album list - Attributions, Summary

I've written more AppleScript than I care to recall since about 2006, but I still wouldn't have been able to write this script without poking through the many AppleScript examples on the Doug's Scripts website, so I have to mention them. Also, as shown in the code, I used an AppleScript list sorting algorithm from a website named Mac OS X Automation.

In summary, I hope this AppleScript program to generate a unique iTunes album list is helpful to you. I plan to come out with some improved versions soon, but if you'd like to see them really soon, contributions to my PayPal account have a way of speeding things up:

Some folks might think that's a little tacky, but greasing my palms will definitely get code to your door faster. ;)