Friday, January 16, 2009

Nifty little AppleScript to publish a playlist

Those of you with copy-editorial leanings may have noticed that I recently changed how I publish my set lists on the blog.

A few weeks ago I wrote a little AppleScript that will take an iTunes playlist and copy the artist, title, album, length, and bpm of each track to the clipboard, in a format specified by the user. I just run this apple script on one of my set lists and wham-bam I can paste it into my blog.

If you're running a Mac, you can use it too. Open the Script editor to create a new script, then copy the code below into the file. Save the file under Library\iTunes\Scripts. The script should now be accessible from iTunes, from the little script drop-down menu (between Window and Help).

Here's the code:
set dialogResult to display dialog "Enter delimiter and click OK" default answer " - "
if button returned of dialogResult is "OK" then
set delimiter_character to text returned of dialogResult
tell application "iTunes"
set thePlaylist to the front window's view
set playlist_data to {}
tell thePlaylist
repeat with i from 1 to the count of tracks
tell track i
set the end of the playlist_data to {name, delimiter_character, artist, delimiter_character, album, delimiter_character, time as string, delimiter_character, bpm, return}
end tell
end repeat
end tell
end tell
tell application "Finder"
display dialog "The playlist information has been copied to the clipboard"
set the clipboard to (playlist_data) as text
end tell
end if
FYI, this code is pretty rough-and-tumble--there's no error handling or anything like that, so it may not work for you. But it should be easy to modify, especially since the language is so easy to follow, almost English. I think the designers meant it to be the Apple equivalent of VBasic.

AppleScripts are pretty nifty. Don't know why I've never played with them before. I've got a few more in the works, so stay tuned.

No comments:

Post a Comment