Accessing iTunes DB through Applescript?

I know this may/may not be game-related, but iDG has a larger community than iDA, and it's not inconceivable that someone might use this code in a game.

What I want to do is use Applescript to get a list of playlists from iTunes. I don't want to read the xml version of the library - I want a realtime list of playlists (as far as I understand it the xml is only revised on quit - I'd like changes made in iTunes to show up in my application).

Thing is, and I've googled this a ton, I can't for the life of me figure out how on earth to do this. I've pored over the applescript dictionary, looking at open source code - I just don't get it. This seems like a fairly simple thing to do - can someone give me a hand here? Thanks in advance.

This is pretty basic and may be a little buggy. Plus my Script Editor app won't seem to run right now so I can't just copy and paste. Hopefully it works though .

Code:

set song_counter to 1

tell application "iTunes"
repeat while(song_counter <= (number of tracks of library playlist 1))
set song_name to name of (track song_counter of library playlist 1)
-- then repeat for other information and do what you will with it
end tell
end tell

I'm sorry if there's an error, but I had a similar script running moments ago before my computer froze. Now it refuses to open that script. I'm assuming the file got corrupted because I can open and run other scripts. I'm also not the Ã¼ber-scripter, so you may want to double check to make sure you won't be getting errors with it.