Exit applescript

Discussion in 'Mac Programming' started by Unorthodox, Sep 26, 2006.

  1. macrumors 65816

    Unorthodox

    #1
    How does one go about telling a script to quit, even if it's only half way thorough?

    For example:
    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?
     
  2. Moderator emeritus

    kainjow

    #2
    Use "return"

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

    Unorthodox

    #3
    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?
     
  4. Moderator emeritus

    kainjow

    #4
    "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.
     
  5. macrumors 65816

    Unorthodox

    #5
    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.
     
  6. Moderator emeritus

    kainjow

    #6
    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()
     
  7. macrumors 65816

    Unorthodox

    #7
    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.
     

Share This Page