Auto-Rate, v2.0
Mar. 15th, 2005 12:20 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
It's still all AppleScript, but a few simple optimizations make it much faster:
The biggest, besides general loop-body reduction, being that I don't care about accuracy of my "logarithm" below the scaling cutoff, so I can skip those loop iterations entirely.
on poorMansLogarithm(x) set exponent to 4 set bound to 32 set prevBound to 16 repeat if x > bound then set exponent to exponent + 1 set prevBound to bound set bound to bound * 2 else return exponent + x / prevBound - 1 end if end repeat end poorMansLogarithm tell application "iTunes" set tunes to the selection of the front browser window set maxTimePlayed to 0 set minTimePlayed to (duration of item 1 of tunes) * (played count of item 1 of tunes) set minTimePlayedCutoff to 32 repeat with currentTrack in tunes set timePlayed to (duration of currentTrack) * (played count of currentTrack) if (timePlayed > maxTimePlayed) then set maxTimePlayed to timePlayed if (timePlayed < minTimePlayed) then set minTimePlayed to timePlayed end repeat if (minTimePlayed < minTimePlayedCutoff) then set minTimePlayed to minTimePlayedCutoff set maxLog to my poorMansLogarithm(maxTimePlayed) set minLog to my poorMansLogarithm(minTimePlayed) set ratingScale to 115.0 / (maxLog - minLog) repeat with currentTrack in tunes set timePlayed to (duration of currentTrack) * (played count of currentTrack) set newRating to ((my poorMansLogarithm(timePlayed)) - minLog) * ratingScale if (newRating > 100) then set newRating to 100 if (newRating < 0) then set newRating to 0 set rating of currentTrack to newRating end repeat end tell
The biggest, besides general loop-body reduction, being that I don't care about accuracy of my "logarithm" below the scaling cutoff, so I can skip those loop iterations entirely.
no subject
Date: 2005-03-14 10:23 pm (UTC)??
no subject
Date: 2005-03-15 05:02 am (UTC)To run it on your whole library, just do select-all in the browser (or modify the line where it gets the variable
tunes
).no subject
Date: 2005-03-15 12:56 am (UTC)no subject
Date: 2005-03-15 05:02 am (UTC)no subject
Date: 2005-03-15 06:39 am (UTC)