Endless Loop


my script will run in an endless loop. But there might be rare cases when I would like to stop the script.
Currently I have “repeat while true” around the whole script. But then I need to kill the script process in order to stop the script. Thus, is there a more elegant solution?

Christoph Gartmann

If you’re running it in the script editor, click stop. You don’t give enough info otherwise.

The script is started automatically during system startup. So there is no script editor involved. My question is a more principal one. Instead of “repeat while true” I am looking for some condition to replace “true” that can be triggered from outside the script.
One thing I thought about could be some file. As long as it is not present the loop continues. What else could one do?

Christoph Gartmann

Assuming your script is running as a script application with a name, then run another script to kill it:

do shell script "killall scriptName"

I think you want to be using a stay-open application and an “on idle” handler instead of a repeat loop. With the “on idle” handler you can just right-click on the app in the dock to quit it or you can issue a quit command from another script.

Here’s some links to tutorials about this handler…

I have lots of Stay Open applications that, on the odd occasion I might want to pause. I use a similar solution to your suggestion. The script below looks for a specific file every time it runs and if it finds the file it presents the user with a dialog, allowing them to either cancel the script or continue. Hope this is of some help?

on idle
	set source_folder to (path to desktop as string) & "test folder" as alias
	tell application "Finder"
		set content_List to name of every file of source_folder
		if content_List contains "stop script.txt" then
			set the_query to display dialog "Script has been paused, what would you like to do?" buttons {"Cancel Script", "Continue Script"} default button "Continue Script"
			if button returned of the_query is "Cancel Script" then
				error number -128
				delete (source_folder as string) & "stop script.txt" as alias
			end if
		end if
		display dialog "continuing script" giving up after 3
	end tell
end idle