My AppleScript applet won't quit, please help.

I’m writing an AppleScript applet that runs in an infinite loop with a 10 seconds delay between each iteration.
The code works as planned and runs every 10 seconds. But… the only way to quit the applet is with a force quit. It’s supposed to keep running silently in the background until the user decides to manually quit it. I’ve tried saving the applet as both normal and stay-open applications and it makes no difference.

I really hope someone can help me finding a solution.
Morten

Can you post the code?

Hi,

AppleScript is single-threaded, if you write code in a handler (in this case the implicit run handler) with a infinite repeat loop the script is blocked.

Save the script as stay open application and put your code in the on idle handler with return 10 as the last line

For reliability implement also the on quit handler

on quit
	continue quit
end quit

You can’t quit a script application while it’s still running ” or rather, it won’t quit until it finishes, which is never with an infinite loop. You should be able to stop it with the keystroke Command-“.”, then it’ll be amenable to quitting (stay-open app) or quit by itself anyway (non-stay-open app).

But it’s better to use a stay-open applet with an idle handler than an infinite repeat:


on run
	-- Do any initial set-up that's necessary.
end run

on idle
	-- Do the action which has to be done periodically. But don't use a repeat for this. The system will tell the script to run this handler again in the number of seconds returned below.

	 -- Ask the system to call again in 10 seconds time.
	return 10
end idle

Thanks StefanK, that did the trick :smiley:
I had to read up on global variables, but now it works.

EDIT: Thanks to you too, Nigel. I didn’t know about on idle handlers before today.