Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Unorthodox

macrumors 65816
Original poster
Mar 3, 2006
1,087
1
Not at the beach...
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

Moderator emeritus
Jun 15, 2000
7,958
7
Use "return"

Code:
display dialog "HEY"
return
beep
"beep" never gets called.
 

Unorthodox

macrumors 65816
Original poster
Mar 3, 2006
1,087
1
Not at the beach...
Look at this example:
Code:
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

Moderator emeritus
Jun 15, 2000
7,958
7
"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

macrumors 65816
Original poster
Mar 3, 2006
1,087
1
Not at the beach...
kainjow said:
"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

Moderator emeritus
Jun 15, 2000
7,958
7
Try a loop instead of a recursive function:

Code:
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

macrumors 65816
Original poster
Mar 3, 2006
1,087
1
Not at the beach...
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.