Skip to content

Instantly share code, notes, and snippets.

@reefdog
Created March 17, 2010 20:26
Show Gist options
  • Save reefdog/335686 to your computer and use it in GitHub Desktop.
Save reefdog/335686 to your computer and use it in GitHub Desktop.
(* "Most Played Artists" by Michael Henley ([email protected]) v1.0 2006-Feb-17 *)
-- Check system version --
if (system attribute "sysv") < 4160 then if button returned of (display dialog "Sorry, this script requires Database Events.app, which is only distributed with Mac OS 10.4 Tiger and later." buttons {"Ignore", "Quit"} default button 2) is "Quit" then error number -128
-- Retrieve raw data --
tell application "iTunes" to set {every_artist, every_play} to {artist, played count} of the first playlist's tracks
set tracks to the length of every_artist
-- Tally up play counts --
set script_timer to (current date)
tell application "Database Events"
-- doug adams edit...
try
delete database "mpa1"
end try
make new database with properties {name:"mpa1"}
tell database "mpa1" to tell (make new record with properties {name:""})
-- ...through here
repeat with i from 1 to tracks
set {an_artist, a_play} to {item i of every_artist, item i of every_play}
if an_artist is not "" and a_play is not 0 then
if field an_artist exists then
tell field an_artist to set value to (value + a_play)
else
make new field with properties {name:an_artist, value:a_play}
end if
end if
end repeat
set {names, values} to {name, value} of fields
end tell
quit
end tell
-- Rank artists --
set {names, values} to {rest of names, rest of values}
set {artists, old_ids, results, name_result, t2} to {length of names, {}, {}, {}, tab & tab}
set output to "Rank" & t2 & "Play Count" & t2 & "Artist" & return
repeat with x from 1 to artists
set low_val to 0
repeat with i from 1 to artists
if i is not in old_ids then tell item i of values to if low_val is 0 or contents > low_val then set {low_val, low_id, low_name} to {contents, i, item i of names}
end repeat
set output to output & x & t2 & low_val & t2 & low_name & return
set end of old_ids to low_id
end repeat
-- Save and open results --
set script_timer to (current date) - script_timer
set output to return & "Ranked " & artists & " artists out of " & tracks & " tracks in " & script_timer div minutes & " minutes, " & script_timer mod minutes & " seconds.
" & return & output & return & "\"Most Played Artists\" by Michael Henley (2006-Feb-17)" & return
set file_path to ((path to desktop folder) as Unicode text) & "Most Played Artists " & (do shell script "date '+%y-%b-%d %H.%M.%S'") & ".txt"
try
open for access file file_path with write permission
write output to file file_path starting at eof
close access file file_path
on error
try
close access file file_path
end try
end try
tell application "TextEdit"
activate
open file_path
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment