User input as property?

I have searched up and down and haven’t found anything talking about what I am trying to do so here it goes.


tell application "System Events"
	activate --Get input from user
	set dd to display dialog "Enter a song" default answer "Island In The Sun" buttons {"Song", "Artist", "Cancel"} default button 1
	set songName to text returned of result
end tell

if button returned of dd is "Song" then
	set songList to getInitialList(songName, name)
	--Do stuff to get the songs in a list and play
else if button returned of dd is "Artist" then
	set bandList to getInitialList(songName, artist)
	--Do stuff to get the songs in a list and play
end if

on getInitialList(myName, myType)
	tell application "iTunes" --Get list from iTunes
		set initialList to every track of playlist "Library" whose myType contains myName
	end tell
	return initialList
end getInitialList

Basically I need the myType variable to be interpreted as a property of a Track object. Everything I have tried thus far has gotten me nowhere. I can of course get it to work by hard coding each version with the proper property, but then this script will grow pretty wildly as I add columns you can search by. Is there reflection in AppleScript that would let me do this? I have quite a bit of programming experience, but I am fairly new to AppleScript so don’t be afraid to throw something advanced my way.

Hi,

the only way to do this is second level evaluation, that means to compile and run a script at runtime.
This method is slower than filtering the properties with if - else if


tell application "System Events"
	activate --Get input from user
	set dd to display dialog "Enter a song" default answer "Island In The Sun" buttons {"Song", "Artist", "Cancel"} default button 1
	set songName to text returned of result
end tell

if button returned of dd is "Song" then
	set songList to getInitialList(songName, "name")
	--Do stuff to get the songs in a list and play
else if button returned of dd is "Artist" then
	set bandList to getInitialList(songName, "artist")
	--Do stuff to get the songs in a list and play
end if

on getInitialList(myName, myType)
	tell application "iTunes"
		return run script "get every track of playlist \"Library\" whose " & myType & " contains \"" & myName & quote
	end tell
end getInitialList

Thanks, worked like a charm.

With my library(~5000 songs) I don’t see a noticeable speed hit, is it just the overhead of compiling a new script or is there something more?