iTunes radio switcher

An easily customizable script that will change to a specified internet radio strem in iTunes. I combine it with cron to make automated stream switching during the day.

OS version: OS X

-- by mike whybark, [url=http://mike.whybark.com/]http://mike.whybark.com/[/url] 
-- thanks to Doug Adams of Doug's Applescripts for iTunes, [url=http://www.malcolmadams.com/itunes/]http://www.malcolmadams.com/itunes/[/url] 
-- This is a script intended to permit cron-based scheduling of internet radio streams in iTunes.
-- In order not to interrupt disc burning operations, I test for apps that need a burner and ask if the user wnats to continue
-- the iTunes Dictionary does not appear to contain specific events or properties that reflect ripping or burning. Burning appears unaffected by the station switching anyway.
-- copy this code, paste into Script Editor, edit your values for "myPlaylist" and "the Station" to reflect your needs, and save as "application." I name each script with the station's call letters, so this one would be "KUOW"
-- Save the applets into ~/Library/iTunes/Scripts
-- set up a crontab to switch according to your desired schedule. I use the excellent GUI edtor 'Cronnix' - [url=http://www.koch-schmidt.de/cronnix/]http://www.koch-schmidt.de/cronnix/[/url] global theStation, myPlaylist, CDMounted, theDiscApps

global theStation, myPlaylist, CDMounted, theDiscApps, theCallSign

set myPlaylist to "a radio selection"
set theCallSign to "KUOW"
set CDMounted to 0
set theDiscApps to 0
-- I have a subset of internet radio in a custom playlist; you might have one too, with a different name

say "Switching to" & theCallSign

tell application "System Events"
	if ejectable of disks contains true then
		-- assumption being that the CD is DOING something, and you don't want iTunes jumping to a radio station all willy-nilly
		-- inserted blank CDs will not be caught: they are not mounted, see?
		set CDMounted to 1
		-- a CD is mounted
	end if
end tell

tell application "System Events"
	set running_apps to (get name of processes)
	-- what's running?
end tell

-- now we'll test for CD and DVD-oriented replication apps

if running_apps contains "Toast" then
	set theDiscApps to 1
	-- Toast is running
else if running_apps contains "iDVD" then
	set theDiscApps to 1
	-- iDVD is running
	-- add your own tests if you'd like
end if

if CDMounted = 1 or theDiscApps = 1 then
	
	copy (display dialog "A CD or DVD-related process is active. Switch iTunes to " & theCallSign & " anyway?" buttons {"OK", "Cancel"} default button "Cancel") to dialogResults
	
	if the button returned of dialogResults is "OK" then
		tell me to play_stream()
	end if
	
else
	tell me to play_stream()
end if

to play_stream()
	try
		tell application "iTunes"
			set thePlaylist to playlist myPlaylist
			set theStation to the first track of thePlaylist whose name contains theCallSign
			play theStation
		end tell
	on error
		say "iTunes is busy at the moment."
		-- if iTunes is presenting a modal dialog, the script hangs and has to be escaped from.
		-- running the script after "burn disc" is engaged in iTunes does not interrupt the burn.
	end try
end play_stream