macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Cleaning iTunes
Pages: 1, 2

A Little AppleScript to the Rescue

I wrote a little AppleScript to grab the selected tracks in the playlist, grab the "name" of the first track in the selection, and use that for the name of the remaining tracks. I need to be careful here -- depending on which way I have the playlist ordered (ascending or descending in a certain column), the first track may be different than what I see as the first track in the window.



This is a quick script to prove to myself that this idea will work. I do not want to get too deep into a problem before I find out if it is not going to work.

tell application "iTunes"
    set myWindow to browser window 1
    set mySelection to selection of myWindow
    set myNewName to name of first item in (get the selection of browser window 1)
    repeat with myTrack in (get the selection of browser window 1)
        set name of myTrack to myNewName
    end repeat
end tell

I used this little script for a couple of CDs, and messed up a couple of times because the right name did not end up in myNewName. I added a dialog to allow me to choose the correct track name from all of the selected tracks, and another dialog to confirm the selection and give me a chance to edit it.

tell application "iTunes"
    set myNames to name of selection of browser window 1
    set myNewName to choose from list myNames with prompt "Which name?" multiple selections allowed 0
    if myNewName is false then --nothing selected, just stop
        return
    end if
    display dialog "Use this name?" default answer myNewName buttons {"Cancel", "Ok"} default button 1 with icon 1 giving up after 5
    if the button returned of the result is "Cancel" then
        return
    end if
    
    repeat with myTrack in (get the selection of browser window 1)
        set name of myTrack to myNewName
    end repeat
end tell

After I use this script, the track names should be half-right. I still need to combine them with the string in the artist portion. I do this a bit differently, so I do not need to select tracks, since I want to change all tracks in the playlist at the same time -- I easily create temporary playlists by dragging a group of selected tracks into the playlist window. Once I have those tracks isolated in their own playlist, I have an easier time working with them, even if I de-select them. I tell iTunes to cycle through all of the tracks, join the name and artist strings with a ", ", then set the name to the combined string. Once the string in the artist section is safely in the track name, I set the artist string to the real artist, which for me means the performer (iTunes has a separate column for Composer -- see the "Edit -> View Options..." menu item to see how you can change the columns in iTunes).


Screen shot.
iTunes View Options dialog.


tell application "iTunes"
    set myPlaylist to playlist "To Do"
    repeat with i from 1 to count of tracks of myPlaylist
        set myTrack to track i of myPlaylist
        set myArtist to artist of myTrack
        set myName to name of myTrack
        set myNewName to myName & ", " & myArtist
        set name of myTrack to myArtist
        set artist of myTrack to "Glenn Gould"
    end repeat
end tell

Once I have the basic idea of the script to combine the artist and track names, I fill out the script with dialogs to select the playlist and the artist name so I do not have to change the script for every group of changes that I want to make.

Screen shot.
AppleScript dialogs for iTunes script.


tell application "iTunes"
    set allPlaylists to name of user playlists
    set myPlaylist to choose from list allPlaylists with prompt
"Which playlist?" multiple selections allowed 0
    if myPlaylist is false then --nothing selected, just stop
        return
    end if
    
    display dialog "What is the new artist name?" default answer "Glenn Gould" buttons {"Cancel", "Ok"} default button 1 with icon 1 giving up after 5
    if the button returned of the result is "Cancel" then
        return
    end if
    
    repeat with i from 1 to count of tracks of myPlaylist
        set myTrack to track i of myPlaylist
        set myArtist to artist of myTrack
        set myName to name of myTrack
        set myNewName to myName & ", " & myArtist
        set name of myTrack to myArtist
        set artist of myTrack to "Glenn Gould"
    end repeat
end tell

Once I get the scripts just how I like them, I add them to the iTunes Scripts directory (/Users/user-name/Library/iTunes/Scripts), which I had to create myself since it did not exist. When iTunes recognizes the directory, an AppleScript icon shows up to the left of the Help menu, and the names of the scripts in the directory show up in the Scripts menu, ready for use.

Screen shot.
iTunes menu bar with AppleScript icon.

Wrapping Up

In a short time, I created two scripts that clean up quite a bit of classical music information in my iTunes Music Library. AppleScript gives me easy access to almost everything that iTunes knows, and lets me automate repetitive tasks. Other problems that I need to fix are special cases or different situations, so I create other scripts for them, first trying very small scripts to make sure the idea works, then adapting the small script for general use. Very quickly I have a powerful set of tools to fix most of the problems I find in my MP3 files without having to do a lot of typing. Now I can spend time listening to my music rather than categorizing it.

brian d foy is a Perl trainer for Stonehenge Consulting Services and is the publisher of The Perl Review.


Return to the Mac DevCenter.