Disable idle handler?

I have a script that uses an idle handler, but I want the user to be able to turn off the idle handler. Is there a way to set the idle handler NOT to run?

kinda!
You can put an if statement inside the handler that check a flag property
set the noIdle property to true when you want it to execute.

property noIdle : false

on idle
	if noIdle then return 20
	-- do your stuff here
end idle

Perfect. Thank you! That should have been obvious to me, but it wasn’t.

You could do something like this where every time the stay-open application is launched, you can choose whether to use the idle handler or not.

on run
	activate
	set idleOption to button returned of (display dialog ¬
		"Use the idle handler?" buttons {"Idle", "No Idle"} ¬
		default button 2 with title "Idle Options")
	
	-- additional commands to run on initial launch of this app
	say "hello hi howdy"
	
	if idleOption = "No Idle" then quit me
end run

on idle
	say "hello"
	delay 5
	beep 3
	delay 2
	activate
	display dialog "Hello" buttons {"OK"} default button "OK" giving up after 3
	return 10 -- in seconds
end idle

on quit
	-- more commands to execute when the script quits
	beep 5
	say "goodbye"
	continue quit -- allows the script to quit
end quit
1 Like