Itunes genre list

Hi There :smiley:

I’ve just started playing around with applescript and i love what i can do with it. I’m finding it difficult to find good resources that cover itunes & applescript, so i’m rusty on the itunes db schema and on the applescript syntax in general. I’m learning though and it’s been fun so far!

What i’m trying to do right now is get a unique genre list back from itunes, running applescript in a bash shell.

I’ve tried iterating through the tracks and building a list eg:


tell application "iTunes" 
        set len to count of tracks
        repeat with num from 1 to len
                if num is equal to 1 then set genreList to {}
                set gen to genre of track num
                if gen is not in genreList then copy gen to the end of genreList                                                
        end repeat      
end tell

but that method is too processor intensive, and takes way too long, so I can’t do it like that.

Ideally i’d like to do this:

tell application "iTunes" to (count(tracks whose genre is unique))

as i belive something like this would take be a LOT quicker but ofcourse that errors out with a ‘variable not defined’ error.

does anyone have any suggestions or links to resources where i can find out more about querying itunes? thanks

This goes pretty fast for an iTunes library of 1688 tracks:

tell application "iTunes" to set allGenres to genre of every track
set uniqueGenres to {}
repeat with i in allGenres
	if (i as string) is not in uniqueGenres then if (i as string) is not "" then set end of uniqueGenres to (i as string)
end repeat

get uniqueGenres

Hope it works,
ief2

Hi,

this is a quite fast solution. It takes less than one second for approx. 3500 tracks

script o
	property genres : ""
end script

tell application "iTunes"
	set o's genres to (get genre of tracks of library playlist 1)
end tell
set genreList to {}

repeat with i from 1 to count o's genres
	set g to item i of o's genres
	if g is not in genreList then set end of genreList to g
end repeat

Thanks for the reply ief2, it makes sense!

What i don’t understand is why using a repeat loop through a list takes so long. I have 42555 tracks in my music library, and it takes about 1m40s to complete with applescript running at 90% CPU.
Whereas if I just do:

tell application "iTunes"  to set allGenres to genre of every track

it takes about 1-3 seconds to complete.

I’m going to try passing the ‘allGenres’ list into bash and build the unique list from there and see if it’s any faster. i’ll post my results. thanks for help!

StefanK, that is a lot faster! It’s taking about 2 seconds to complete with 42555 tracks. :smiley:

I’m going to try understand this code better, thank you for helping.

a script object is the fastest solution to hold a hugh list

Ahh a script object. suddenly that code makes perfect sense. thank you!

i don’t understand why using a script object’s property is faster than a list variable, but it’s good to know :slight_smile: