iTunes problem with selection

Hi,

there seems to be a problem with the selection element of iTunes.
I want to create an AppleScript for playing the currently selected item in iTunes.
This is the code:


tell application "iTunes"
	set this_track_number to database ID of selection
	//display dialog this_track_number
	play (track where database ID is this_track_number)
end tell

Display the track number (the outcommented code) is no problem, when running the script from inside the iTunes script menu. But then - when the “play” code is processed there appears the following error message:
“Can’t get <> whose <> of it = {2037}.”

Can somebody help me make iTunes play my selected track?

Another interesting point is that there seems to be a general AppleScript bug with iTunes and the “selection” element. Running the mentioned script directly in ScriptEditor in Mac OS X 10.3.6 lets ScriptEditor not even get the database ID from selection (in the second code line).

Regards, flooo

Hi,

The ‘database id’ is good for finding duplicates. To play a track you just need a reference to the track.

When using the ‘every element’ reference form you can use:

every track of someplaylist where …

or tracks of someplaylist where …

Note the complete reference with someplaylist where someplaylist is a reference to some playlist. If for some reason you still want to use the database id then here’s an example:

tell application “iTunes”
set browser_window to (first browser window) – a reference
tell browser_window
set the_playlist to view – a reference
set db_id to item 1 of (database ID of (selection)) – an integer
end tell
set selected_tracks to (every track of the_playlist whose database ID is db_id)
play (item 1 of selected_tracks) – item 1 of a list
end tell

Note that you needed to get the playlist for use in the ‘every element’ reference.

Try not to stuff to many coercions in one statement.

gl,