Hi Jim,
The every element reference form can be a little tricky at first especially when used with a filter ‘whose’ (or ‘where’).
Generally, it has the form:
every objectClass [of objectContainerReference] whose objectProperty is propertyValue
or you can use the plural form of the class of the object mixed with a reference to the object’s container. What I usually do is get the playlist’s reference (the container) first.
tell application “iTunes”
set lib_ref to first library playlist
end tell
Note that library playlist is an element of application “iTunes” (the target of the statement).
Just for the purpose of this script, I’ll get a tracks database id:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
end tell
Then to get the track’s reference:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
set track_ref to (every track of lib_ref whose database ID is track_id)
end tell
The plural form:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
set track_ref to (tracks of lib_ref whose database ID is track_id)
end tell
Note that the every element refernce form returns a list of 0 or more items. 0 items might cause errors so you should check this with an error handler. Here you know that the result is a one item list. If you just want the reference to your one item you can use relative reference:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
set track_ref to (first track of lib_ref whose database ID is track_id)
end tell
Sometimes, if there are a lot of statements using lib_ref and you don’t want to type ‘of lib_ref’ over and over, you can make it the target by using the tell statement:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
tell lib_ref
set track_ref to (last track whose database ID is track_id)
end tell
end tell
Hope I haven’t forgotten something important.
EDITTED: one last thing. Note that track_ref is a complete reference to the track, so you don’t need to specify the other stuff (lib_ref) when using it:
tell application “iTunes”
set lib_ref to first library playlist
set track_id to (database ID of some track of lib_ref)
tell lib_ref
set track_ref to (last track whose database ID is track_id)
end tell
set track_name to name of track_ref
end tell
Also, you may not need to use th database id unless you’re getting it from somewhere else (like your own database).
gl,