Error with iTunes script to set album rating from track ratings

Hello,

The iTunes script posted below uses track ratings and the total number of tracks (across all disks for a given album) to calculate and set an album rating. It may be that I don’t still have all tracks for a given album in my iTunes library, but because of the ‘track count’ tag in iTunes, I can still calculate this.

For the purposes of calculating the album rating I want to treat a track rating of 4 and 5 as the same - hence the maximum track rating of 80.

I am experiencing an issue with the script where I have more than one album with the same name - here I am not talking about multi-disc albums, but albums by different artists which have the same album name.

I can’t work out how to change the script to get it working with all albums, so would be glad of some help.

Thanks,

Nick

tell application "iTunes"

--Make a list of all the selected albums:

	set theAlbums to {}
	set theAlbumsRef to a reference to theAlbums
	set tempList to album of selection
	set thePreviousTitle to ""

	repeat with thisTitle in tempList
		set thisTitle to thisTitle as text
		if thisTitle is not thePreviousTitle then
			copy thisTitle to the end of theAlbumsRef
			set thePreviousTitle to thisTitle
		end if
	end repeat

	set numberOfAlbums to count theAlbums

 ---Make a list containing one track for each selected album:

	set theTracks to {}
	set theTracksRef to a reference to theTracks

	repeat with thisAlbum in theAlbums
		copy (track 1 whose album is thisAlbum) to the end of theTracksRef
	end repeat

	--Set the album rating of each album contained in the selection:

	set theRatings to ""
	set k to 0

	repeat with thisTrack in theTracks
		set k to k + 1
		if k mod 25 = 0 then beep 1 -- for audio feedback
		set thisAlbum to item k of theAlbums
		set numberOfTracks to 0

		repeat with k from 1 to (disc count of thisTrack)
			try
				get (track count of track 1 whose (album is thisAlbum) and (disc number is k))
				set numberOfTracks to numberOfTracks + result
			end try
		end repeat

		set songRating to 0

		repeat with R in (rating of tracks whose album is thisAlbum)
			if R > 80 then set R to 80
			set songRating to songRating + R
		end repeat

		set thisRating to round (songRating / numberOfTracks)
		set album rating of thisTrack to thisRating
		set theRatings to theRatings & (album artist of thisTrack & tab & thisAlbum & tab & (thisRating as text) & return)

	end repeat

end tell

Sorry, I should have been more specific about the issue. It is that, where two albums (by different artists) have the same name, the script adds the individual track ratings for both albums and divides by the number of tracks in one album, meaning that the album rating is very inflated.

So the script needs to be altered so that the songRating variable is calculated from just the tracks in the album by a single artist.

Hi. Your whose clauses all need to be compound statements, so they target more than just the album—which may share a name—however, even that may not work, as common scenarios have no apparent differentiator to target; e.g. remixes where the editor doesn’t take credit anywhere other than the song name or where multiple songs have no album name. I’m not following the mod statement’s purpose in your code. Given the aforementioned caveat and my understanding, perhaps this:

set traversalPast to {}
set seenArtist to {}

tell application "iTunes"
	repeat with aTrack in (get selection)
		if aTrack's album is not in traversalPast or aTrack's artist is not in seenArtist then
			tell (tracks whose artist is (get aTrack's artist) and album is (get aTrack's album))
				set rating to my limit(my average(rating))
			end tell
			set traversalPast's end to aTrack's album
			set seenArtist's end to aTrack's artist
		else
			display dialog "I saw this combination before and will ignore it."
		end if
	end repeat
end tell

on average(valueList)
	set endvalue to 0
	repeat with aVal in valueList
		set endvalue to endvalue + aVal
	end repeat
	endvalue / (count valueList)
end average

on limit(value)
	if value is greater than 80 then
		80
	else
		value
	end if
end limit

–edited for clarity and to check combination values

Just a question, this album rating your talking about is your own idea he?
As I’m not aware that there was an Album rating available in then iTunes API