Wednesday, December 06, 2006

I got some coding done in a language I haven't used before: AppleScript. After playing around with it a bit, I wrote a script that picks a random playlist in the library with an average of more than 10 playcounts per song and plays it. It's great for when I'm really indecisive and can't decide what I want to listen to. Here's the code (note, you need to be on a mac. just copy and paste this in the script editor (Applications -> AppleScript -> Script Editor) and compile and save it):
tell application "iTunes"
set the_playlists to the name of every playlist
set count_pl to count the_playlists
set ave_play_count to 0
set good_playlist to false
repeat while (good_playlist = false)
set the_choice to item (random number from 1 to count_pl) of the_playlists
set the_pl to item 1 of (every playlist whose name is the_choice)
set the_total to count tracks in the_pl
set this_track to 1
repeat while (this_track ≤ the_total)
set ave_play_count to (ave_play_count + ((played count) of track this_track of the_pl))
set this_track to (this_track + 1)
end repeat
set ave_play_count to ave_play_count / the_total
if ave_play_count ≥ 10 then
set good_playlist to true
end if
end repeat
play track 1 of the_pl
end tell
I like it. I also like how much AppleScript looks like English.

If you want to use it, compile it and inside Library -> iTunes, make a new folder called "Scripts" and put it in there. You'll notice a new scripts menu in iTunes, and it'll be in there.

--
Current music: Kajinka - Breathtaking Vision (Sky Sanctuary remix) (the most awesome orchestral remix of a video game song I've ever heard)

No comments: