Run Script when speaker selected in iTunes

Hi,

I want to run a script when a remote airtunes speaker is selected in iTunes.

Is there a way of doing this using by running a script whenever iTunes is clicked on that checks all the settings of itunes looking for the speaker name to be selected?

Thanks

Dave

You will have to poll for a change of the popup button’s title.


(* 
This script is meant to be saved as an Stay Open applet and 
is a demonstration on how to determine wether the iTunes speakers 
selection has changed.

This script requires that Access for Assistive Devices is
enabled in the Universal Access preference pane and that
the iTunes UI uses the English language.
*)


-- Polling interval in seconds
property POLL_INTERVAL : 1

global PREVIOUSLY_SELECTED_ITUNES_SPEAKERS

on run
	-- Store the name of the currently selected speakers on applet launch
	storeCurrentiTunesSpeakers()
end run


on idle
	-- Find out wether the speakers changed
	if didiTunesSpeakersChange() then
		
		-- Store the current speakers
		storeCurrentiTunesSpeakers()
		
		activate
		display dialog "iTunes speakers have been changed." buttons {"OK"} default button 1 giving up after 10
		
		-------------------
		-- Your code here
		-------------------
		
	end if
	
	return POLL_INTERVAL
end idle



on storeCurrentiTunesSpeakers()
	set PREVIOUSLY_SELECTED_ITUNES_SPEAKERS to currentiTunesSpeakers()
end storeCurrentiTunesSpeakers

on didiTunesSpeakersChange()
	return (currentiTunesSpeakers() is not PREVIOUSLY_SELECTED_ITUNES_SPEAKERS)
end didiTunesSpeakersChange

on currentiTunesSpeakers()
	tell application "System Events"
		tell process "iTunes"
			tell window "iTunes"
				return title of first UI element whose help is "Choose which speakers to use."
			end tell
		end tell
	end tell
end currentiTunesSpeakers