Ensure AppleScript Stops Running When the Application Quits

How can I make sure my Arc Window Closer app (built with AppleScript via Automator) stops running when Arc quits? I set LSMultipleInstancesProhibited and LSUIElement to true in the Info.plist, but I’m concerned the script keeps running in the background. How can I confirm it’s actually quitting?

Here’s my script:

on run
	my checkEmptyWindows()
	return
end run

on idle
	tell application "System Events"
		if (exists process "Arc") then
			-- Arc is running, so perform the check
			my checkEmptyWindows()
		else
			-- Arc is not running, so quit the script
			quit
		end if
	end tell
	return 10 -- wait 10 seconds before the next idle call
end idle

on checkEmptyWindows()
	tell application "Arc"
		if (count of windows) > 0 then
			repeat with w in windows
				if (count of tabs of w) is 0 then
					close w
				end if
			end repeat
		end if
	end tell
end checkEmptyWindows

Any ideas on how to ensure the script actually stops? Thanks!

Hi zonal-oculars.

The trick use to be (and still seems to be) to put a “User cancelled” error immediately after the quit command.

quit
error number -128

quit tells the application running the script to quit, which it will only do when it’s finished what it’s doing — ie. running the script. error number -128 then stops the execution of the script and allows the application to quit.

An alternative way to do this is to add a quit handler at the end of your current code:

on quit
	continue quit
	error number -128
end quit

This intercepts any quit command the application receives and includes stopping the script in the process. The difference is that it also works with quit commands issued from outside the application, such as from the Dock or the [application name] menu in the menu bar.

Hi Nigel,

Thanks for the help! I added quit with error number -128 and an on quit handler, but neither idle nor quit runs. My Automator-built app closes immediately, and I only see run notifications. Also, when an error occurs, a dialog box appears, which I don’t want—I need the app to run as a background process, completely GUI-less.

  1. Does Automator support periodic idle execution? Saving the app from Script Editor instead of Automator fixed that issue, but now it runs indefinitely in idle status.
    image

  2. The command quit doesn’t stop the script, but tell me to quit does. This, along with using Script Editor, solved the issue. Any other suggestions?

Updated AppleScript with all the fixes:

on run
	my checkEmptyWindows()
	return
end run

on idle
	tell application "System Events"
		if (exists process "Arc") then
			my checkEmptyWindows()
		else
			tell me to quit
		end if
	end tell
	return 10 -- Idle interval in seconds
end idle

on checkEmptyWindows()
	tell application "Arc"
		if (count of windows) > 0 then
			repeat with w in windows
				if (count of tabs of w) is 0 then
					close w
				end if
			end repeat
		end if
	end tell
end checkEmptyWindows

on quit
	continue quit
end quit
  1. The script app quits (It shows that on activity monitor), but how can I ensure the AppleScript also stops running? Any ideas?

Thanks,

Hi zonal-oculars.

Scripts with idle handlers are supposed to be run as stay-open applications in their own right. That is, they should be compiled in Script Editor (or in Script Debugger if you’re using that) and saved from there, selecting “Application” in the “File Format:” pop-up menu in the “Save…” dialog and checking the “Stay open after run handler” option checkbox below that. There doesn’t seem to be a similar option in Automator. But if your script’s the entire process, you don’t need to use Automator anyway.

1 Like

The script app quits (It shows that on activity monitor), but how can I ensure the AppleScript also stops running? Any ideas?
Web script editor application quits does its processes quit also, or is it my responsibility to quit them? ( if yes how to )?

How can I check running Apple scripts in general?

I’m not sure I understand you. If the Script apps quits, then it quits.

I have an application that wraps an Apple Script, when the application quits, does it also quit/terminate the processes of the script?

Yes, when an app quits, all its sub processes quit also.
Unless the sub process is intentionally spun off as a seperate process,

What is this application you keep referring too? A store-bought app, or a custom home grown and compiled app? And with what process are you running this script?

I’m using a custom-built AppleScript application, compiled via Script Editor as a stay-open app with an idle handler. I run the script from Script Editor, so when Arc quits, the entire process (including the script and the wrapper application of the script (Arc-Window-Closer.app)) terminates as expected.