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! Can you guys help?
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
Unfortunately, both your answers still don’t work for me!
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.
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:
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.
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
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()