Posted By

testman on 02/19/10


Tagged

itunes


Versions (?)

Album Ranking.scpt


 / Published in: AppleScript
 

URL: http://dougscripts.com/itunes/scripts/ss.php?sp=albumranking

  1. (* Album Ranking
  2.  
  3. 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.
  4.  
  5. By Cantus Vetustus
  6. Report Bugs/Suggestions to [email protected] *)
  7.  
  8.  
  9. property unratedTrack : false
  10.  
  11. tell application "iTunes"
  12. if (player state is playing) and (selection = {}) then
  13. set second_Button to "Playing Track"
  14. set track_type to "the currently playing"
  15. else if selection ≠ {} then
  16. set second_Button to "Selected Track"
  17. set track_type to "the selected"
  18. else
  19. set second_Button to "Random Track"
  20. set track_type to "a random"
  21. end if
  22. 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)
  23. end tell
  24.  
  25. if theResult is second_Button then
  26. tell application "iTunes"
  27. copy selection to allTracks
  28.  
  29. if allTracks = {} then
  30. if second_Button = "Random Track" then
  31. tell source "Library" to tell playlist "Library" to set thisAlbum to album of some track
  32. else
  33. set thisAlbum to album of current track
  34. end if
  35. else
  36. set thisAlbum to album of (item 1 of allTracks)
  37. end if
  38.  
  39. if thisAlbum = "" then
  40. 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
  41. return false
  42. else
  43. tell source "Library" to tell playlist "Library" to set albumTracks to (every track whose album is thisAlbum)
  44. set trackRating to 0
  45. set numberofTracks to the count of albumTracks
  46. --Obtain rating for every song in Library.
  47. repeat with x from 1 to the count of albumTracks
  48. if (rating of item x of albumTracks) is 0 then
  49. display dialog "Some of the tracks in this album are unrated. Script will exit." buttons {"Quit"} default button 1 with icon 2
  50. set unratedTrack to true
  51. return false
  52. else
  53. set trackRating to trackRating + (rating of item x of albumTracks)
  54. end if
  55. end repeat
  56. --Calculate average rating for every album.
  57. set albumRating to (((5 * trackRating / numberofTracks) as integer) / 100) as number
  58. 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
  59. end if
  60. end tell
  61.  
  62. else if theResult is "Make Web Page" then
  63. tell application "iTunes"
  64. set the masterList to {}
  65. set theLibrary to every track in library playlist 1
  66. --Create a list of every album in the Library.
  67. repeat with i from 1 to the count of theLibrary
  68. set the end of the masterList to the album of item i in theLibrary
  69. end repeat
  70.  
  71. set the albumList to {}
  72. --Filter out repeated items.
  73. repeat with i from 1 to the count of the masterList
  74. set thisAlbum to (item i of the masterList) as string
  75. if the albumList does not contain thisAlbum and thisAlbum is not "" then
  76. copy thisAlbum to the end of the albumList
  77. end if
  78. end repeat
  79.  
  80. set the ratingList to {}
  81. repeat with i from 1 to the count of albumList
  82. set unratedTrack to false
  83. tell source "Library" to tell playlist "Library" to set albumTracks to (every track whose album is (item i of albumList))
  84. set trackRating to 0
  85. set numberofTracks to the count of albumTracks
  86. --Obtain rating for every song in Library.
  87. repeat with x from 1 to the count of albumTracks
  88. if (rating of item x of albumTracks) is 0 then
  89. set unratedTrack to true
  90. exit repeat
  91. else
  92. set trackRating to trackRating + (rating of item x of albumTracks)
  93. end if
  94. end repeat
  95. if unratedTrack is false then
  96. --Calculate average rating for every album.
  97. set albumRating to (((5 * trackRating / numberofTracks) as integer) / 100) as number
  98. copy (albumRating & " | " & item i of albumList & (ASCII character 13) & (ASCII character 10)) to end of ratingList
  99. end if
  100. end repeat
  101.  
  102. --Sort entire list by highest rated album.
  103. set ratingList to do shell script "echo " & quoted form of (ratingList as string) & " | sort -k 1,1r -k 1.7,1.8"
  104. end tell
  105.  
  106. set the_counter to 1
  107. set theContent to {}
  108. --Generate web page content.
  109. repeat with i from 1 to the (number of paragraphs of ratingList) - 1
  110. set m to the offset of "|" in (paragraph i of the ratingList)
  111. set the_album to (characters (m + 2) thru -1 of (paragraph i of the ratingList)) as string
  112. set the_rating to (characters 1 thru (m - 2) of (paragraph i of the ratingList)) as string
  113. copy tab & tab & tab & "<tr>" & return & ¬
  114. tab & tab & tab & tab & "<td>" & return & ¬
  115. tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬
  116. tab & tab & tab & tab & tab & tab & the_counter & return & ¬
  117. tab & tab & tab & tab & tab & "</span>" & return & ¬
  118. tab & tab & tab & tab & "</td>" & return & ¬
  119. tab & tab & tab & tab & "<td>" & return & ¬
  120. tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬
  121. tab & tab & tab & tab & tab & tab & the_album & return & ¬
  122. tab & tab & tab & tab & tab & "</span>" & return & ¬
  123. tab & tab & tab & tab & "</td>" & return & ¬
  124. tab & tab & tab & tab & "<td>" & return & ¬
  125. tab & tab & tab & tab & tab & "<span class=\"code\">" & return & ¬
  126. tab & tab & tab & tab & tab & tab & the_rating & return & ¬
  127. tab & tab & tab & tab & tab & "</span>" & return & ¬
  128. tab & tab & tab & tab & "</td>" & return & ¬
  129. tab & tab & tab & "</tr>" & return to the end of theContent
  130. set the_counter to the_counter + 1
  131. end repeat
  132.  
  133. --Create basic web page structure.
  134. set thePage to "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" & return & ¬
  135. " \"http://www.w3.org/TR/html4/loose.dtd\">" & return & ¬
  136. "<html lang=\"en\">" & return & ¬
  137. tab & "<head>" & return & ¬
  138. tab & tab & "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" & return & ¬
  139. tab & tab & "<title>" & return & ¬
  140. tab & tab & tab & "My Album Ranking" & return & ¬
  141. tab & tab & "</title>" & return & ¬
  142. "<style type=\"text/css\">" & return & ¬
  143. "body {font-size: 13px; font-family: Lucida Grande, Verdana, Arial, sans-serif; background: #FFFFFF; padding: 0px; margin: 10px}" & return & ¬
  144. "span.code {color: #000000}" & return & ¬
  145. "h1 {font-size: 16px; font-weight: bold; border: 0px; margin-bottom: 20px; font-family: Lucida Grande, Helvetica, Arial, sans-serif; color: #32469E}" & return & ¬
  146. "</style>" & return & ¬
  147. tab & "</head>" & return & ¬
  148. tab & "<body>" & return & ¬
  149. tab & tab & "<h1>" & return & ¬
  150. tab & tab & tab & "My Album Ranking" & return & ¬
  151. tab & tab & "</h1>" & return & ¬
  152. tab & tab & "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">" & return & ¬
  153. tab & tab & tab & "<tr>" & return & ¬
  154. tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬
  155. tab & tab & tab & tab & tab & "<b>" & return & ¬
  156. tab & tab & tab & tab & tab & tab & "Rank" & return & ¬
  157. tab & tab & tab & tab & tab & "</b>" & return & ¬
  158. tab & tab & tab & tab & "</td>" & return & ¬
  159. tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬
  160. tab & tab & tab & tab & tab & "<b>" & return & ¬
  161. tab & tab & tab & tab & tab & tab & "Album" & return & ¬
  162. tab & tab & tab & tab & tab & "</b>" & return & ¬
  163. tab & tab & tab & tab & "</td>" & return & ¬
  164. tab & tab & tab & tab & "<td bgcolor=\"#ECECEC\">" & return & ¬
  165. tab & tab & tab & tab & tab & "<b>" & return & ¬
  166. tab & tab & tab & tab & tab & tab & "Rating" & return & ¬
  167. tab & tab & tab & tab & tab & "</b>" & return & ¬
  168. tab & tab & tab & tab & "</td>" & return & ¬
  169. tab & tab & tab & "</tr>" & return & ¬
  170. theContent & ¬
  171. tab & tab & "</table>" & return & ¬
  172. tab & "</body>" & return & ¬
  173. "</html>"
  174.  
  175. --The following done to make sure we're not overwriting an existing file.
  176. tell application "Finder"
  177. if exists (file "albumranking.html" of desktop) then
  178. set x to 1
  179. set my_file to "albumranking" & x & ".html"
  180. repeat
  181. if not (exists (file my_file of desktop)) then
  182. do shell script "echo " & quoted form of thePage & " > ~/Desktop/albumranking" & x & ".html"
  183. exit repeat
  184. end if
  185. set x to x + 1
  186. end repeat
  187. else
  188. set my_file to "albumranking.html"
  189. do shell script "echo " & quoted form of thePage & " > ~/Desktop/albumranking.html"
  190. end if
  191. end tell
  192.  
  193. 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)
  194.  
  195. --Reveal file in Finder.
  196. if theResult = "Show File" then
  197. tell application "Finder"
  198. reveal file my_file of desktop
  199. activate
  200. end tell
  201. else
  202. return false
  203. end if
  204.  
  205. else
  206. return false
  207. end if
  208.  
  209. --Created by Cantus Vetustus on Mon Jul 7, 2003
  210. --Modified by Cantus Vetustus on Fri Jul 18, 2003

Report this snippet  

You need to login to post a comment.