PDA

View Full Version : Exit applescript




Unorthodox
Sep 26, 2006, 05:20 PM
How does one go about telling a script to quit, even if it's only half way thorough?

For example:
display dialog "Do you want to quit?" buttons {"Yes","No"}
if the button returned of the result is "Yes" then
--This is where I got stumped
end
beep 5
What command will exit the script immediately if the button "Quit" is pressed and not continue on and beep.
I know if you use "Cancel" as a button that will exit the script.
How do I do that, but with a different word on the button?



kainjow
Sep 26, 2006, 05:36 PM
Use "return"

display dialog "HEY"
return
beep

"beep" never gets called.

Unorthodox
Sep 26, 2006, 06:12 PM
Look at this example:
on potato()
display dialog "Cheese" buttons {"Quit","Play"}
if the button returned of the result is "Quit" then
return
else
potato()
end if
beep
end potato
set cheese to true
potato()

If you hit the button quit. it quits.
But.....
If you hit the button play, it repeats (as it's supposed to).
Now if you hit quit it will beep.
What gives?

kainjow
Sep 26, 2006, 06:18 PM
"return" exits from the current function. It doesn't affect where the function was called from originally.

The code within a function doesn't replace the name of the function where it's called.

Your code will continue to go on forever if you keep pressing Play.

Unorthodox
Sep 26, 2006, 06:34 PM
"return" exits from the current function. It doesn't affect where the function was called from originally.

The code within a function doesn't replace the name of the function where it's called.

Your code will continue to go on forever if you keep pressing Play.
I want it to go on forever when you press play.
But I want it to stop when you press quit.

Do you know anyway to do this?

I'm trying to make a blackjack game just using AppleScript.
The part I'm having trouble on is if you win/lose, it asks you if you want to play again or stop playing.

kainjow
Sep 26, 2006, 06:51 PM
Try a loop instead of a recursive function:

on potato()
set done to false
repeat while done is false
display dialog "Cheese" buttons {"Quit", "Play"}
if the button returned of the result is "Quit" then
set done to true
end if
end repeat

beep
end potato

potato()

Unorthodox
Sep 26, 2006, 07:20 PM
That's a good idea.
One more question though.
Is is there a command that makes the script start from the beginning of a repeat?



Edit: Never mind. Figured it out.
Thanks so much for all your help kainjow! :)
Have a cookie.