Quitter Macro Getting an Error

I run a macro with only one action to runs an AppleScript. I get this error the first time the macro is run. The second time I run it, it works. Can anyone tell me why I'm getting this error?

The AppleScript is below:

-- get list of open apps
tell application "System Events"
	set allApps to displayed name of (every process whose background only is false) as list
end tell

-- leave some apps open 
set exclusions to {"Finder", "Keyboard Maestro Engine", "1Password 7"}

-- quit each app
repeat with thisApp in allApps
	set thisApp to thisApp as text
	if thisApp is not in exclusions then
		tell application thisApp to quit
	end if
end repeat

FYI, the script's function is to quit all apps except for the ones specified.

Here is the Macro that everyone can try, remember you must test it on an M1 Mac.

Quitter Macro (v10.0.2)

Quitter.kmmacros (1.8 KB)

Have you tried running it with different app names, in order to determine if the app names have anything to do with the problem? Chances are good that that might be a factor.

I also suggest that before and after the first time you run it, you make a list of all running processes and compare those two lists to see what was closed. Use this version of ps:

ps -ax | sort -n

The reason I suggest this is that whatever processes managed to close the first time may have caused the error, but after they were closed, the script could run with no error. Just an idea.

You need to post an actual, testable macro...

I believe the error you show is from the user cancelling an AppleScript Choose-From-List dialog.

-Chris

I added the Macro to my original post. Thanks for suggesting it.

If you think the error is occurring as a result of the statement inside your nested loop, (and I think that's the likely source of the error) then why not put a "try... on error.. end try" around that statement? It would prevent the script from failing, and it would also give you the chance to insert a statement like this: "display dialog thisApp" which would tell you the name of the app that it was trying to close. You could also append the string "worked" and "failed" to the display statement so you could see which app was causing your problem.