Modifying the properties of a certain iTunes track

I’m much more of a Perl and C programmer than an Applescripter. I’ve been digging through other peoples scripts, FAQs, and language references but can’t seem to get something working.

Let’s say I have some identifying information about a particular iTunes track based on what is in “iTunes Music Library.xml”. Track ID (database ID) and Location (perhaps converted to a MacOS path) seem the most promising as unique track identifiers.

What I want to do is simple, but I’ve been fighting with AppleScript (or perhaps iTunes), taking different approaches, to get it working. Based on the identifier (say database ID=35), I want to set the ratings to a certain value (say 100). Shouldn’t that be as easy as:


tell application "iTunes"
	set t to (track whose database ID is 35)
	set rating of t to 100
end tell

But I get the error “Can’t get track whose database ID = 35”. I have verifed that the database ID is indeed 35 by displaying the database ID of the current selction.

Where am I going wrong? Is there an easier identifier to use?

Thanks,

Jim

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,

Thanks Kel. Guess I had been assuming the library reference wasn’t needed (though I had tried that once) and that I would be able to use the singular filter form.

This is what I ended up with:


tell application "iTunes"
	set lib_ref to first library playlist
	set track_id to 747
	try
		set track_ref to first track of lib_ref whose database ID is track_id
		set rating of track_ref to 80
	on error
		display dialog "Could not set John Barleycorn (ID 747) to 80"
	end try
end tell

That was the last part of a set of Perl scripts I was writing to combine the ratings from two distinct iTunes libraries. (Used the Mac::AppleScript modue from CPAN to run the Applescript part of it.)

– Jim