Can't quit out of my applescript script! :(

Hi guys,

I’m trying to quit out of my applescript script when the “No” button is pressed in a dialog box through one of my methods. However, when I hit “No”, the following error occurs everytime: “The document can’t be closed while the script is still running.” Why is that? Here is my code:

on displayEnglish()
set result to button returned of (display dialog “This will uninstall your software. Do you want to continue?” buttons {“Yes”, “No”} default button “Yes” with icon caution)
if result is equal to “No” then
quit
end if
end displayEnglish

I’ve also tried “continue quit”, but nothing works! :frowning: Can you guys help?

Thanks so much and God Bless you all! :slight_smile:

Ben.

Try this instead…

if (result as string) is equal to "No" then

The word ‘result’ is a reserved word, and you shouldn’t get in the habit of using it as a variable name. Use something else, like “theResult”. Also, ‘result’ automatically refers to any object returned from the previous line. For example, if you set a variable in a line, “result” in the following line will return the variable’s value…

set someVariable to "aValue"
result --> returns "aValue"

In your case you don’t really need to explicitly set “result” because it already is automatically set if you want to access it that way in the next line…

on displayEnglish()
	display dialog "This will uninstall your software. Do you want to continue?" buttons {"Yes", "No"} default button "Yes" with icon caution
	if ((button returned of result) as string) is equal to "No" then quit
end displayEnglish

I think the main problem the poster is having is that he’s running the script from Script Editor.

The error dialog he’s getting won’t appear if the code is used in a standalone app.

Peter B.


Unfortunately, both your answers still don’t work for me! :frowning:

Jobu, when I attempted you rmethods, I got the same error stating the script cannot be closed while it’s running, and your last example compiled with an error: Can’t make button returned of “No” into type string.. Peter, I compiled my code into application form, however, when executing the script form my desktop, and clicking “No”, it simply moved onto the next method within my script without quitting it.

Any suggestion guys?

Ben.

Try:

if result is equal to “No” then
return
error -128
end if

… in the app form.

Or:

if result is equal to “No” then
tell me to quit
end if

… if the app is saved as stay open.

But mind jobu’s recommendation about the variable name.

Peter B.


Hey Peter,

I tried that when doing my compiled script application, but this time, it still moves on with the script when I click “No”. Any ideas?

Thanks,

Ben.

Is there a program that’s launched when referencing the document (TextEdit, MS Word, ect) or is AS just saying your script is a document?! If another active program is the reason behind disabling your script from properly quitting then you can always do a force quit on the documents application:

do shell script “killall "program name”"

Hi.

The way to stop a script in its tracks is error number -128. (That’s the error that’s generated when a dialog’s “Cancel” button’s clicked.) Peter’s first script has return in front of that line, which makes the handler exit before the error’s executed. (And he’s omitted ‘number’.) You need:

display dialog "This will uninstall your software. Do you want to continue?" buttons {"Yes", "No"} default button "Yes" with icon caution
if button returned of result is "No" then error number -128

quit is for making applications quit, so tell me to quit is an instruction to the application running the script. (That’s why Script Editor was trying to quit on you.) But once the command’s been received, applications usually finish what they’re doing before actually quitting. Your script application’s running the script and doesn’t quit until it’s finished doing that.

If the script application’s a stay-open app, you can issue the quit and error number -128 commands in quick succession:

if button returned of result is "No" then
	tell me to quit -- Quit when finished.
	error number -128 -- Finish now.
end if

Otherwise error number -128 by itself should be enough. A script saved as an application should then quit anyway. However, if the error’s within the scope of a try block, you’ll need to trap for it to let it go through.

Hi Nigel,

When I tried your method, it said “can’t get button returned of ‘No’” through the sciprt, and "can’t get class of “No” when run through a compiled application. Here is my code snippet:

on displayEnglish()
set btnResult to button returned of (display dialog “This will uninstall your Software. Do you want to continue?” buttons {“Yes”, “No”} default button “Yes” with icon caution)
if button returned of btnResult is “No” then
error number -128
end if
end displayEnglish

Any suggestions?

Thanks,

Ben.

Hi Ben,

In your last code snippet you are setting btnResult to button returned of the dialog, then asking for the button returned of that.

try this:

on displayEnglish()
	set btnResult to button returned of (display dialog "This will uninstall your Software. Do you want to continue?" buttons {"Yes", "No"} default button "Yes" with icon caution)
	if btnResult is "No" then
		error number -128
	end if
end displayEnglish

Or more simply:

on displayEnglish()
	display dialog "This will uninstall your Software. Do you want to continue?" buttons {"Yes", "No"} default button "Yes" with icon caution
	if button returned of result is "No" then error number -128
end displayEnglish()

Best wishes

John Maisey

Thanks so much John, it worked! :slight_smile: And thanks to all who helped.

God bless you all. :slight_smile:

Ben.