'if' in applescript and looping [on idle]

Hey, I want to get Applescript launch iTunes to launch if an phone is detected connected in bluetooth. Heres the applescript for detection of the phone:

BTdevice_connected("iPhone")

on BTdevice_connected(theDevice)
	try
		set theData to do shell script "system_profiler SPBluetoothDataType"
		set {TID, text item delimiters} to {text item delimiters, theDevice}
		set theData to text item 3 of theData
		set text item delimiters to "Connected: "
		set theData to word 1 of text item 2 of theData
		set text item delimiters to TID
		return theData is "Yes"
	on error
		return false
	end try
end BTdevice_connected

I also (sperate script ) want to get bluetooth to scan for a phone every 5 seconds (looping)

Thanks as always

Hi,

save this as stay open application.
There’s also a logic included to execute “do something” only once, if the phone has been detected

property phoneStatus : false

on idle
	if BTdevice_connected("iPhone") then
		if phoneStatus is false then
			set phoneStatus to true
			-- do something 
		end if
	else
		set phoneStatus to false
	end if
	return 5
end idle

on BTdevice_connected(theDevice)
	try
		set theData to do shell script "system_profiler SPBluetoothDataType"
		set {TID, text item delimiters} to {text item delimiters, theDevice}
		set theData to text item 3 of theData
		set text item delimiters to "Connected: "
		set theData to word 1 of text item 2 of theData
		set text item delimiters to TID
		return theData is "Yes"
	on error
		return false
	end try
end BTdevice_connected

Thanks,
Thats brilliant