tags:

views:

64

answers:

1

How do I search for every song match certain criteria? E.g., something like:

tell application "iTunes"
    set tracks to (every file track of playlist "Library" whose name contains "Green")
    repeat with t in tracks
        display dialog (t as string)
    end repeat
end tell
+2  A: 

Just like that, except for two things; first, tracks is a reserved word in the iTunes scripting dictionary, so replace it with something else (e.g. ts); second, you can't just convert a track to a string with t as string; you have to choose what information you want. For instance, if you want the name, you can use name of t as string; if you want more detail, you could do name of t & " - " & artist of t; etc. Note also that if you want a more complicated predicate, you can just tack it on to the whose clause; for instance, file tracks whose name contains "Green" and artist contains "Bird".

If you want to golf the script a little, you could replace it with the following:

tell application "iTunes"
    repeat with t in (file tracks whose name contains "Green")
        display dialog (name of t as string)
    end repeat
end tell

Removing the of playlist "Library" might produce slightly different results, but probably not. It didn't in my quick testing.

Antal S-Z
Ah, thanks for pointing out it's a reserved word. Changing it did the trick. Thanks!
Bill