on run

     start ()

end run

on start ()

     display dialog "iTunes playlist exporter" & return & return buttons {" Quit ", "About", "Begin"} default button 3

     if button returned of result is "About" then

         display dialog "An applescript written to iTunes playlist information as an XHTML-valid table to be pasted into an HTML file. Gets information on the song name, album, artist, and genre." & return & return & "Written by Grayson Hansard, From Concentrate Software" & return & return & "Note: If you have very large playlists, then this Applescript could take a long while to work its magic." buttons {"Okay"} default button 1

         my start ()

     else if button returned of result is "Begin" then

         tell application "iTunes"

             set lst to name of user playlists

             set lst to {"Library"} & lst

             choose from list lst with prompt "Which playlist to export?"

             if result is not false then

                 my export ( result as string )

                 display dialog "Finished"

             end if

         end tell

        

     end if

    

end start

on export ( playlst )

     tell application "iTunes"

         set final to ""

        

         set nms to the name of every track in playlist playlst

         set albms to the album of every track in playlist playlst

         set artst to the artist of every track in playlist playlst

         set gnr to the genre of every track in playlist playlst

        

         set html to "<table cellpading=\"0\" cellspacing=\"0\" border=\"0\">" & return & "<tr style=\"backround:#ddd; font-size: 1.2em;\"><td>Song</td><td>Album</td><td>Artist</td><td>Genre</td></tr>" & return

        

        -- Build the table

         repeat with i from 1 to length of nms

             if i mod 2 is equal to 0 then

                 set html to html & "<tr style=\"background:#fff\"><td>"

             else

                 set html to html & "<tr style=\"background:#edf3fe\"><td>"

             end if

            

             set thesong to item i of nms

             set thealbum to item i of albms

             set theartist to item i of artst

             set thegenre to item i of gnr

            

             set html to html & thesong & "</td><td>" & thealbum & "</td><td>" & theartist & "</td><td>" & thegenre

             set html to html & "</td></tr>" & return

            

         end repeat

         set html to html & "</table>"

        

        -- Write to a file

         set pathToFile to ( path to desktop folder )

         set pathToFile to ( pathToFile as string ) & playlst & ".txt"

         my writeToFile ( html , pathToFile )

        

     end tell

end export

(* **************** Write to File **************** *)

on writeToFile ( msg_data , target_file )

     try

         set open_file to open for access file target_file with write permission

         set eof of open_file to 0

         write msg_data to open_file starting at eof

         close access open_file

     end try

end writeToFile