can't get first item of list

For my apps type and play feature you type part or all of a song name hit enter and it plays the song if there i only 1 result and brings up all results in a list if there is more. The weird thing is, if you choose the first item in the list it gives an error, otherwise it works properly.
Here’s the code:

on titlesearch()
    set songTitle to content of text field "titlesearch" of drawer "searchdrawer" of window "main"
    set playTrack to ""
    tell application "iTunes"
        set fixedIndexing to fixed indexing
        set fixed indexing to true
        
        repeat while playTrack is ""
            tell (tracks of (first playlist whose special kind is Music) whose name contains songTitle) to if (count) > 1 then
                set playTrack to my chooseTracks(name, artist, album, it)
            else if it is not {} then
                set playTrack to item 1 of it
            else
                try
                    tell me to set songTitle to text returned of (display dialog "Your search turned up 0 results. Try again?." default answer "" buttons {"Cancel", "Go"} default button 2)
                on error -- user canceled
                    set playTrack to false
                end try
            end if
        end repeat
    end tell
    
    if playTrack is not false then
        tell application "iTunes"
            if (get version) as string < "7.1" then
                set Master_Playlist to library playlist 1
            else
                set Master_Playlist to first playlist whose special kind is Music
            end if
            set view of browser window 1 to Master_Playlist
            
            play playTrack
            set fixed indexing to fixedIndexing
        end tell
    end if
    
end titlesearch

and the error if you choose the first item:

Can't get item 0 of {file track id 2112 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2188 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2195 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2204 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2281 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2340 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2377 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2406 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2423 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2438 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2445 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2475 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2509 of user playlist id 2052 of source id 46 of application "iTunes", file track id 2561 of user playlist id 2052 of source id 46 of application "iTunes"}. (-1728)

EDIT: oops, this should be in AS Studio/ XCode, my bad

Hi,

the crucial point seems to be the chooseTracks() handler :wink:

oh yeah i forgot to post that:
here it is,

on chooseTracks(trackNames, trackArtists, trackAlbums, tracksFound)
	set {tracksList, foundCount} to {{}, count tracksFound}
	repeat with i from 1 to foundCount
		if trackArtists's item i is "" then set trackArtists's item i to "Unknown"
		if trackAlbums's item i is "" then set trackAlbums's item i to "Unknown"
		set end of tracksList to trackNames's item i & " - " & trackArtists's item i & " (" & trackAlbums's item i & ")"
	end repeat
	
	set trackChosen to choose from list tracksList with prompt "Your search turned up " & foundCount & " results. Please specify which track you want to play."
	if trackChosen is false then return false
	
	set my text item delimiters to return
	set tracksList to tracksList as Unicode text
	set my text item delimiters to beginning of trackChosen
	set trackNumber to count of paragraphs of first text item of tracksList
	set my text item delimiters to ""
	return item trackNumber of tracksFound
end chooseTracks

if you choose the first item of the list,
then the first text item in this line

 set trackNumber to count of paragraphs of first text item of tracksList

is always “” so the result is 0

try this at the end of the handler


.
set my text item delimiters to ""
    if trackNumber is 0 then
        return item 1 of tracksFound
    else
        return item trackNumber of tracksFound
    end if
end chooseTracks

works like a charm, thanks :smiley: