Applescript to Export from iTunes Playlists


I’ve always been kind of an audiophile, looking for better music as well as better sound quality. I have a huge music database, and I made a huge effort to keep their metadata clean and correct. In iTunes, I have a few smart playlists that contains music with ratings of more than 3 stars.

I recently bought a new music player – Sony ZX100, a really nice addition to my daily life. So naturally I want to have an automatic way to export my music from my iTunes playlists to my music player, and also in the future to update the files in my music player without overwriting the whole thing. So here it is my applet written by applescript to finish this quest.

Getting the Playlists

The first step, obviously is to choose which playlist to export from. Since I have a playlist folder of all the playlists called “★★★”, it is easier to use that to have a list of all the playlists.


tell application "iTunes"

    set pls to {}
    set plfolder to folder playlist "★★★"
    repeat with pl in playlists
        if pl's parent = plfolder then
        copy name of pl to end of pls
        end if
    end try
    end repeat
    set theChoice to (choose from list pls with prompt "Select the Playlist to Export Files From:" with title "Export Playlist" default items "None" OK button name {"Select"} cancel button name {"Cancel"})
    --Exit the Program if User Cancels
    if theChoice is false then
    end if

It looks like this:case-review-applescript-to-export-itunes-library-1

Get the Tracks to be Exported

The next step is to figure out in the selected playlist, which tracks are needed to be exported, because I don’t want to blindly export them all even most of them are already exported before.

My first thought is to check with every track in the playlist to see if their name matches with any files in the target folder. If not it means that track needs to be exported. It’s obviously this method takes a long time especially for bigger playlists.

Then I decided to use the fact that my naming convention for the file – They all starts with the artist of the song. So I only check those songs with the same artist in the target folder, and it got a lot faster, but still not quite fast enough.

tell application "Finder" to set sameArtist to (every item in folder targetPath that its name contains artist)

But even then the speed is still not very satisfying. So instead of using applescript I decided to use shell “find” command to get the list of music with the same artist.

set com to "find " & POSIX path of targetPath & " -name " & quoted form of sartist & "* -exec basename {} ';'"
set sameArtist to every paragraph of (do shell script com)

Doing so significantly increased the speed.

Finally I reallized that I can use the comments of music tracks to remember if a track has been exported. So if a track has been exported, it doesn’t need to be checked at all. Then I just add the comment of “Exported” everytime I actually export a song.

set sel_songs to (every track of playlist (theChoice as string) that its comment is not "Exported")
    if (count of sel_songs) is 0 then
    display dialog "No Songs Needs to be Exported"
    end if

Then I still need to check if the tracks already exist in the target folder. This step is just some string manipulation.

repeat with s in sel_songs
    set sfile to (the location of s)
    set sartist to artist of s
    set sname to name of s
    set sname2 to sartist & " - " & sname
    set com to "find " & POSIX path of targetPath & " -name " & quoted form of sartist & "* -exec basename {} ';'"
    set sameArtist to every paragraph of (do shell script com)
    set isDuplicate to false
    repeat with sa in sameArtist
    set sa_name to name of sa
    set sa_name2 to text 1 thru ((sa_name's length) - (offset of "." in (the reverse of every character of sa_name) as text)) of sa_name
    if sa_name2 is equal to sname2 then
        set isDuplicate to true         
    end if
end repeat

After that I can get a list of songs needed to be exported. Then display a dialog to show them.

display dialog ((count of new_songs) as string) & " New Songs Found: " & (items of new_song_names2) buttons {"Cancel", "Continue"} default button 2

It looks like this:


Exporting the Songs

Finally I need to copy the audio files to my music player, and then rename them according to my name convention.

repeat with i from 1 to count of new_songs
    set ex to the text -4 thru -1 of (item i of new_songs as string)
    tell application "Finder" to set dupFile to duplicate item i of new_songs to targetPath without replacing
        set name of dupFile to item i of new_song_names & ex
        on error
                tell application to delete dupFile
    end try
    tell application "iTunes" to set the comment of item i of new_songs_track to "Exported"
end repeat

Show Progress of Export

The latest version of OS X added support to easily write applescript to make a progress window. A few notes:

  • the progress window only shows when the script is exported as an app. Other wise it would be a menu bar item showing the progress.
  • You would get errors if any of the progress code was placed under any “tell application” sections. It has to be independent of any applications.

For me the code looks like this:

set progress total steps to count of new_songs
set progress description to "Copying Files..."
set progress completed steps to 0
repeat with i from 1 to count of new_songs  
    set progress description to ("Copying Files...( " & i & " of " & (count of new_songs) as string) & " )"
    set progress additional description to item i of new_song_names & ex
    set progress completed steps to i
end repeat

The progress window:



It was another nice and small project to automate and speed up things I do regularly, and I am pretty satisfied with the result.

During the process I reinforced my general knowledge of applescript, skills to manipulate tracks in iTunes and files in Finder, and learned how to make a progress window through applescript.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s