Wednesday, April 04, 2007

iTunes drag and drop bug: the Doug's AppleScripts workaround

iTunes OS X supports drag and drop export of a playlist to a folder. I use this to export AAC and MP3 (non-FairPlay) tunes to a thumb drive for play in the car or on my corporate laptop.

There's no other built-in method for exporting playlist files, and this method isn't documented. One reason it may be undocumented is that it's buggy. If a playlist contains two tunes of the same name (possibly same file name, I didn't have time to finish testing) the drag and drop aborts without an error message. It's a funny abort -- the files briefly appear then vanish.

Happily there appear to be workarounds. Doug's AppleScripts for iTunes, a site mostly built in 2005, has several scripts that sync or copy a playlist to an external folder. I'm going to try this one. Doug's site accepts Amazon donations (in addition to PayPal, a service I dislike for several reasons), so if the scripts work I'll send him a fiver.

PS. Doug's 'What's New' feed has some good tips and comments. He mentions a "tumblelog", a new categorical term which sounds like it might apply to this blog.

Update: Alas, the 'copy to folder' script has the same bug. I'll take a look at it and see if I can hack a fixed version that will do name collision fixes. I've written Doug in the hopes he might add this code.

Update 4/6/07: I haven't heard from Doug, so I decided to see if I could hack a workaround. I find AppleScript pretty painful to work with (anyone else notice Apple's online documentation is dated 1999?) but a relatively simple hack occurred to me; if I prefixed all the output filenames with an incrementing counter they'd be guaranteed to be unique. It took at least two hours to figure out how to do this (Ethan Wilde's AppleScript for Applications was by far the best reference I could find and it's very dated). Here's the modified script, my additions are in bold.
tell application "iTunes"
set counter to 0
set trackList to the selection of window 1 -- any window with a selection
set theTrackCount to count trackList
if ((count trackList) is 0) then
display dialog "No tracks are selected!" with icon caution buttons {"Cancel"} default button 1
end if
set theFolder to choose folder with prompt "Pick folder to copy the selected tracks to"

if theFolder is not "" then
repeat with theTrack in trackList
set counter to counter + 1
set theTrackName to ((the location of theTrack))
tell application "Finder"
set NewFile to duplicate file theTrackName to theFolder
set the name of NewFile to ((counter as string) & "_" & the name of NewFile)
if (counter mod 10 is 0) then display dialog "Processing track " & counter & " of " & theTrackCount giving up after 1 with icon note
on error
tell application "iTunes"
display dialog "The Finder reported an error: The file [" & (theTrackName as string) & "] could not be copied to [" & theFolder & "]. The counter is " & (counter as string) with icon caution
end tell
end try
end tell
end repeat
display dialog "Finished!" buttons {"Okay"} default button 1 with icon 1
end if
end tell

It takes a long time to run through a mere 179 tracks, but it works. The hardest part was figuring out how to get a handle on the result of the "Duplicate file" command; browsing the Finder's "Dictionary" I discovered the Duplicate command in 10.4.9 returns a reference to the output file. That wasn't documented anywhere else I looked.

This would have been a trivial task in the old days of DOS Batch programming, and it would have completed in a second or two. Progress can be elusive.

No comments: