« Kicking defenceless pine | Main | Bloxorz »

Expressive code

I’ve just made an ammendment to my script for renaming iTunes files. Like most people, my digital music comes from various sources and an inconsistent naming convention is pretty much guaranteed. Generally this is irrelevant, since the name of the file has little metadata value compared to the ID3 tags associated with it. But there still remains a desire to have consistently named music files.

It turns out AppleScript is just brilliant for this automating this task. Scripts are accessible from within iTunes, and the iTunes dictionary gives complete access to the data. My file renaming script simply loops through the tracks I’ve selected in iTunes, changing the name of the source file to “artist - track name” format. The complete script may prove useful to others, and contains little more than you would find at Doug’s Applescripts, so I’ve included it verbatim after the jump.

The point I wanted to make though, is that everytime I work with AppleScript I’m impressed with the language. It’s no kernal development language, but for scripting, can anyone honestly tell me they know of a more expressive language than this?

tell application "Finder"
repeat with i from 1 to the count of these_files
    set this_file to (item i of these_files) as alias
    set this_extension to the name extension of this_file
    set the name of this_file to (this_artist & this_track & this_extension)
end repeat

Couple that with syntax highlighting and the open dictionary feature, and you have, IMHO, a world-class language with unprecendented accessibility.

The complete script:

property required_version : "2.0.3"

try
tell application "iTunes"
    -- VERSION CHECK
    set this_version to the version as string
    if this_version is not greater than or equal to the required_version then
        beep
        display dialog "This script requires iTunes version: " & required_version & ¬
            return & return & ¬
            "Current version of iTunes: " & this_version ¬
            buttons {"Update", "Cancel"} default button 2 with icon 2
        if the button returned of the result is "Update" then
            my access_website("http://www.apple.com/itunes/download/")
            return "incorrect version"
        end if
    end if

    -- get a list of track references
    set these_tracks to the selection of browser window 1
    if these_tracks is {} then error "No tracks are selected in the front window."

    -- check for an iPod
    if the kind of container of view of browser window 1 is iPod then
        error "This script cannot rename files from an iPod."
    end if

    set these_files to {}
    set artist_names to {}
    set track_names to {}
    -- get the file path of each of the selected tracks
    repeat with i from 1 to the count of these_tracks
        set this_track to item i of these_tracks
        if the location of this_track is not missing value then
            set the end of these_files to (the location of this_track)
            set the end of artist_names to (the artist of this_track)
            set the end of track_names to (the name of this_track)
        end if
    end repeat
end tell
tell application "Finder"
    set AppleScript's text item delimiters to "."
    -- rename the files
    set the item_counter to 0
    repeat with i from 1 to the count of these_files
        set this_file to (item i of these_files) as alias
        set this_artist to (item i of artist_names)
        set this_track to (item i of track_names)
        set this_extension to name extension of this_file
        set name of this_file to (this_artist & " - " & this_track & "." & this_extension)
        set the item_counter to the item_counter + 1
    end repeat
end tell
-- confirmation dialog
tell application "iTunes"
    display dialog "Process completed." & return & return & ¬
        (item_counter as string) & " files have been renamed." ¬
        buttons {"OK"} default button 1
end tell
on error error_message number error_number
if the error_number is not -128 then
    tell application "iTunes"
        activate
        beep
        display dialog ("Error processing " & this_track & ": " & error_message) ¬
            buttons {"Cancel"} default button 1
    end tell
end if
end try

TrackBack

TrackBack URL for this entry:
http://heath.hrsoftworks.net/cgi-bin/mt-tracker.cgi/138

Post a comment