/ Published in: AppleScript
URL: http://dougscripts.com/itunes/scripts/ss.php?sp=albumranking
Expand |
Embed | Plain Text
(* Album Ranking This script will create an HTML file of your highest rated albums, calculating an album's average rating with your ratings for every song in that album. Albums with unrated songs will not be ranked. By Cantus Vetustus Report Bugs/Suggestions to [email protected] *) property unratedTrack : false tell application "iTunes" if (player state is playing) and (selection = {}) then set second_Button to "Playing Track" set track_type to "the currently playing" else if selection ≠ {} then set second_Button to "Selected Track" set track_type to "the selected" else set second_Button to "Random Track" set track_type to "a random" end if set theResult to button returned of (display dialog "Welcome to Album Ranking." & return & return & "<Make Web Page> creates an HTML file listing your highest rated albums. This may take a few minutes depending on the size of your Library." & return & return & "<" & second_Button & "> will display the album rating for " & track_type & " track only." & return & return & "Make your selection:" buttons {"Cancel", second_Button, "Make Web Page"} default button 3) end tell if theResult is second_Button then tell application "iTunes" copy selection to allTracks if allTracks = {} then if second_Button = "Random Track" then tell source "Library" to tell playlist "Library" to set thisAlbum to album of some track else set thisAlbum to album of current track end if else set thisAlbum to album of (item 1 of allTracks) end if if thisAlbum = "" then display dialog "This track does not have an album specified. Please select a track which contains an album tag and try again." buttons {"Quit"} default button 1 with icon 2 return false else tell source "Library" to tell playlist "Library" to set albumTracks to (every track whose album is thisAlbum) set trackRating to 0 set numberofTracks to the count of albumTracks --Obtain rating for every song in Library. repeat with x from 1 to the count of albumTracks if (rating of item x of albumTracks) is 0 then display dialog "Some of the tracks in this album are unrated. Script will exit." buttons {"Quit"} default button 1 with icon 2 set unratedTrack to true return false else set trackRating to trackRating + (rating of item x of albumTracks) end if end repeat --Calculate average rating for every album. set albumRating to (((5 * trackRating / numberofTracks) as integer) / 100) as number display dialog "The album \"" & thisAlbum & "\" contains " & numberofTracks & " tracks." & return & return & "This album's average rating is " & albumRating & " out of 5 stars." buttons {"OK"} default button 1 with icon 1 end if end tell else if theResult is "Make Web Page" then tell application "iTunes" set the masterList to {} set theLibrary to every track in library playlist 1 --Create a list of every album in the Library. repeat with i from 1 to the count of theLibrary set the end of the masterList to the album of item i in theLibrary end repeat set the albumList to {} --Filter out repeated items. repeat with i from 1 to the count of the masterList set thisAlbum to (item i of the masterList) as string if the albumList does not contain thisAlbum and thisAlbum is not "" then copy thisAlbum to the end of the albumList end if end repeat set the ratingList to {} repeat with i from 1 to the count of albumList set unratedTrack to false tell source "Library" to tell playlist "Library" to set albumTracks to (every track whose album is (item i of albumList)) set trackRating to 0 set numberofTracks to the count of albumTracks --Obtain rating for every song in Library. repeat with x from 1 to the count of albumTracks if (rating of item x of albumTracks) is 0 then set unratedTrack to true exit repeat else set trackRating to trackRating + (rating of item x of albumTracks) end if end repeat if unratedTrack is false then --Calculate average rating for every album. set albumRating to (((5 * trackRating / numberofTracks) as integer) / 100) as number copy (albumRating & " | " & item i of albumList & (ASCII character 13) & (ASCII character 10)) to end of ratingList end if end repeat --Sort entire list by highest rated album. set ratingList to do shell script "echo " & quoted form of (ratingList as string) & " | sort -k 1,1r -k 1.7,1.8" end tell set the_counter to 1 set theContent to {} --Generate web page content. repeat with i from 1 to the (number of paragraphs of ratingList) - 1 set m to the offset of "|" in (paragraph i of the ratingList) set the_album to (characters (m + 2) thru -1 of (paragraph i of the ratingList)) as string set the_rating to (characters 1 thru (m - 2) of (paragraph i of the ratingList)) as string copy tab & tab & tab & "<tr>" & return & ¬ tab & tab & tab & tab & "<td>" & return & ¬ tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬ tab & tab & tab & tab & tab & tab & the_counter & return & ¬ tab & tab & tab & tab & tab & "</span>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & tab & "<td>" & return & ¬ tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬ tab & tab & tab & tab & tab & tab & the_album & return & ¬ tab & tab & tab & tab & tab & "</span>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & tab & "<td>" & return & ¬ tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬ tab & tab & tab & tab & tab & tab & the_rating & return & ¬ tab & tab & tab & tab & tab & "</span>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & "</tr>" & return to the end of theContent set the_counter to the_counter + 1 end repeat --Create basic web page structure. set thePage to "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" & return & ¬ " \"http://www.w3.org/TR/html4/loose.dtd\">" & return & ¬ "<html lang=\"en\">" & return & ¬ tab & "<head>" & return & ¬ tab & tab & "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" & return & ¬ tab & tab & "<title>" & return & ¬ tab & tab & tab & "My Album Ranking" & return & ¬ tab & tab & "</title>" & return & ¬ "<style type=\"text/css\">" & return & ¬ "body {font-size: 13px; font-family: Lucida Grande, Verdana, Arial, sans-serif; background: #FFFFFF; padding: 0px; margin: 10px}" & return & ¬ "span.code {color: #000000}" & return & ¬ "h1 {font-size: 16px; font-weight: bold; border: 0px; margin-bottom: 20px; font-family: Lucida Grande, Helvetica, Arial, sans-serif; color: #32469E}" & return & ¬ "</style>" & return & ¬ tab & "</head>" & return & ¬ tab & "<body>" & return & ¬ tab & tab & "<h1>" & return & ¬ tab & tab & tab & "My Album Ranking" & return & ¬ tab & tab & "</h1>" & return & ¬ tab & tab & "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">" & return & ¬ tab & tab & tab & "<tr>" & return & ¬ tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬ tab & tab & tab & tab & tab & "<b>" & return & ¬ tab & tab & tab & tab & tab & tab & "Rank" & return & ¬ tab & tab & tab & tab & tab & "</b>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬ tab & tab & tab & tab & tab & "<b>" & return & ¬ tab & tab & tab & tab & tab & tab & "Album" & return & ¬ tab & tab & tab & tab & tab & "</b>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬ tab & tab & tab & tab & tab & "<b>" & return & ¬ tab & tab & tab & tab & tab & tab & "Rating" & return & ¬ tab & tab & tab & tab & tab & "</b>" & return & ¬ tab & tab & tab & tab & "</td>" & return & ¬ tab & tab & tab & "</tr>" & return & ¬ theContent & ¬ tab & tab & "</table>" & return & ¬ tab & "</body>" & return & ¬ "</html>" --The following done to make sure we're not overwriting an existing file. tell application "Finder" if exists (file "albumranking.html" of desktop) then set x to 1 set my_file to "albumranking" & x & ".html" repeat if not (exists (file my_file of desktop)) then do shell script "echo " & quoted form of thePage & " > ~/Desktop/albumranking" & x & ".html" exit repeat end if set x to x + 1 end repeat else set my_file to "albumranking.html" do shell script "echo " & quoted form of thePage & " > ~/Desktop/albumranking.html" end if end tell set theResult to button returned of (display dialog "We're done!" & return & return & "The file " & my_file & " is in your Desktop." buttons {"Show File", "Quit"} default button 2 with icon 1) --Reveal file in Finder. if theResult = "Show File" then tell application "Finder" reveal file my_file of desktop activate end tell else return false end if else return false end if --Created by Cantus Vetustus on Mon Jul 7, 2003 --Modified by Cantus Vetustus on Fri Jul 18, 2003
You need to login to post a comment.
