Error number -128 "User Cancelled" when trying to quit app

Greetings. I have a very simple script to quit an app. When I run the following script I get the error “error “Wirecast got an error: User canceled.” number -128”.

Script is:
tell application “Finder”
quit application “Wirecast”
end tell

Is there a way to cleanly cause Wirecast to quit without generating a User Canceled error?
Thank you for any advice.

This is for a system running High Sierra but I get the exact same error on more recent OS versions also.
Thank you very much.

I found the solution.

Script is now:
try
tell application “Finder”
quit application “Wirecast”
end tell
on error number -128
quit application “Wirecast”
end try

This works.

Why are you telling the “Finder”.

You can tell the application “Wirecast” directly.

deesquared. Normally you quit an application as shown below. This typically works both with apps that are scriptable and that are not scriptable. I don’t have Wirecast and can’t test if this works, but, regardless, I can’t think of a circumstance where I would involve Finder in this.

tell application "Wirecast" to quit

If the above doesn’t work, you might try the following, which should inform you of an error if it occurs.

try
	tell application "Wirecast" to quit
on error errMsg number errNbr
	display alert errMsg message "Error number " & errNbr
end try

@deesquared
It’s also good practice to check wether a particular condition exists, before acting on it.
In this case, it would be wise to check that the “Wirecast” application is actually active before quitting it.
Something like this.

tell application "System Events"
	set openApplicationsList to (name of every process whose background only is false)
	if openApplicationsList contains "Wirecast" then
		try
			tell application "Wirecast" to quit
		on error errMessage number errNumber
			display alert errMessage & return & "Error Number: " & errNumber
		end try
	end if
end tell

Regards Mark

Mark, there is a more convenient syntax

if application "Wirecast" is running then
	try
		quit application "Wirecast"
	on error errMessage number errNumber
		display alert errMessage & return & "Error Number: " & errNumber
	end try
end if

@StefanK Yeah nice one.
I don’t do enough vanilla AppleScript these days.
Makes it so easy to forget stuff you used to do.

I wondered what is achieved by checking to see if the app is running. If I run the following code, Safari quits if it’s running and otherwise nothing happens. Whichever is the case, the code takes less than a millisecond to execute.

tell application "Safari" to quit

I ran the following script with Safari running and not running, and the results were the same as the above. At no point did I get an error message.

if application "Safari" is running then
	try
		quit application "Safari"
	on error errMessage number errNumber
		display alert errMessage & return & "Error Number: " & errNumber
	end try
end if

BTW, in some instances it seems more efficient simply to do something then it is to check if a condition exists and, if so, to do something. Perhaps this is a bad approach to coding, though?

That may be true for Safari, but some programs launch in response to any apple event being sent to them. Those with dynamic libraries that need to be updated at run/compile time.

Thanks Ed. I didn’t know that but it makes sense.