cleaning up applescript

Discussion in 'Mac Programming' started by msindt19, Jun 21, 2011.

  1. msindt19, Jun 21, 2011
    Last edited by a moderator: Jun 21, 2011

    macrumors member

    msindt19

    Joined:
    Jun 8, 2011
    Location:
    New York, New York
    #1
    New to macs, and even newer to Applescript/programming in general. So I'm just trying to pick up the basics and play around and I made a rock, paper, scissors game. here is the script:


    Code:
    set againValue to "Yes"
    repeat until againValue is not equal to "Yes"
    	set var1 to "Rock"
    	set var2 to "Paper"
    	set var3 to "Scissors"
    	set question1 to display dialog "Want to play a game?" buttons {var1, var2, var3}
    	set playerChoice to button returned of question1
    	set compChoice to some item of {var1, var2, var3}
    	if playerChoice is var1 and compChoice is var1 then
    		display dialog "Tie game, I picked \"Rock\" too. Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var1 and compChoice is var3 then
    		display dialog "Not fair, I picked \"Scissors\". You win! Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var1 and compChoice is var2 then
    		say "Ha, Ha, I picked paper. You Lose!" using "Zarvox"
    		display dialog "Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var1 and compChoice is var1 then
    		display dialog "Not fair, I picked \"Rock\". You win! Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var2 and compChoice is var2 then
    		display dialog "Tie game, I picked \"Paper\" too. Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var2 and compChoice is var3 then
    		say "Ha, Ha, I picked scissors You Lose!" using "Zarvox"
    		display dialog "Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var3 and compChoice is var1 then
    		say "Ha, Ha, I picked rock. You Lose!" using "Zarvox"
    		display dialog "Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var3 and compChoice is var2 then
    		display dialog "Not fair, I picked \"Paper\" you win! Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	else if playerChoice is var3 and compChoice is var3 then
    		display dialog "Tie game. I picked \"Scissors\" too! Play again?" buttons {"Yes", "No"}
    		set againValue to button returned of result
    	end if
    end repeat



    Now, my question is, can i clean that up? I feel like repeating the "set againValue to button returned of result" seemed monotonous. Is there an easier way to do it, or do I have to type it out every single time? I tried setting it to a variable but got confused when I added in the repeat.

    I'm looking over some PDFs on basic applescript now but any other advice would be much appreciated
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Here's my clean up of it:

    Code:
    [b]set[/b] [color=#408000]againValue[/color] [b]to[/b] "Yes"
    [b]repeat[/b] [b]until[/b] [color=#408000]againValue[/color] [b]is not[/b] [b]equal to[/b] "Yes"
        [b]set[/b] [color=#408000]rockChoice[/color] [b]to[/b] "Rock"
        [b]set[/b] [color=#408000]paperChoice[/color] [b]to[/b] "Paper"
        [b]set[/b] [color=#408000]scissorsChoice[/color] [b]to[/b] "Scissors"
        [b]set[/b] [color=#408000]question1[/color] [b]to[/b] [b][color=#0016b0]display dialog[/color][/b] "Want to play a game?" [color=#0016b0]buttons[/color] {[color=#408000]rockChoice[/color], [color=#408000]paperChoice[/color], [color=#408000]scissorsChoice[/color]}
        [b]set[/b] [color=#408000]playerChoice[/color] [b]to[/b] [color=#4415b0]button returned[/color] [b]of[/b] [color=#408000]question1[/color]
        [b]set[/b] [color=#408000]compChoice[/color] [b]to[/b] [b]some[/b] [i][color=#0000ff]item[/color][/i] [b]of[/b] {[color=#408000]rockChoice[/color], [color=#408000]paperChoice[/color], [color=#408000]scissorsChoice[/color]}
        [b]set[/b] [color=#408000]displayIt[/color] [b]to[/b] [i][color=#0000ff]missing value[/color][/i]
        [b]if[/b] [color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]compChoice[/color] [b]then[/b]
            [b]set[/b] [color=#408000]displayIt[/color] [b]to[/b] "Tie game, I picked \"" & [color=#408000]playerChoice[/color] & "\" too. Play again?"
        [b]else[/b] [b]if[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]rockChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]scissorsChoice[/color]) [b]or[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]paperChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]rockChoice[/color]) [b]or[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]scissorsChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]paperChoice[/color]) [b]then[/b]
            [b]set[/b] [color=#408000]displayIt[/color] [b]to[/b] "Not fair, I picked \"" & [color=#408000]compChoice[/color] & "\". You win! Play again?"
        [b]else[/b] [b]if[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]rockChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]paperChoice[/color]) [b]or[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]paperChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]scissorsChoice[/color]) [b]or[/b] ([color=#408000]playerChoice[/color] [b]is[/b] [color=#408000]scissorsChoice[/color] [b]and[/b] [color=#408000]compChoice[/color] [b]is[/b] [color=#408000]rockChoice[/color]) [b]then[/b]
            [b][color=#0016b0]say[/color][/b] "Ha, Ha, I picked " & [color=#408000]compChoice[/color] & ". You Lose!" [color=#0016b0]using[/color] "Zarvox"
            [b]set[/b] [color=#408000]displayIt[/color] [b]to[/b] "Play again?"
        [b]end[/b] [b]if[/b]
        [b][color=#0016b0]display dialog[/color][/b] [color=#408000]displayIt[/color] [color=#0016b0]buttons[/color] {"Yes", "No"}
        [b]set[/b] [color=#408000]againValue[/color] [b]to[/b] [color=#4415b0]button returned[/color] [b]of[/b] [color=#6c05d3]result[/color]
    [b]end[/b] [b]repeat[/b]
    1. I renamed var1, var2, var3 to something more meaningful and easier to read.
    2. Since there are only 3 outcomes, I combined the if statements.
    3. I set the dialog string to a variable instead of repeating that each time.

    Hope this helps.
     
  3. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #3
    Usually, when you are doing the same thing more than a few times (such as your result dialogs), you might want to consider using a handler. In addition to making your script a little more readable, it is also easier to make changes.

    In this case however, you can also do away with your againValue variable altogether by setting one of the buttons in your dialog to cancel:

    Code:
    set {rock, paper, scissors} to {"Rock", "Paper", "Scissors"}
    display dialog "Want to play a game of Rock, Paper, Scissors?" buttons {"No", "Yes"} cancel button "No" default button "Yes"
    
    repeat -- forever
    	set playerChoice to button returned of (display dialog "Choose your weapon" buttons {rock, paper, scissors})
    	set compChoice to some item of {rock, paper, scissors}
    	log compChoice
    	if playerChoice is equal to compChoice then
    		showResult("Tie", playerChoice)
    		
    	else if playerChoice is rock and compChoice is paper then
    		showResult("Win", compChoice)
    	else if playerChoice is rock and compChoice is scissors then
    		showResult("Lose", compChoice)
    		
    	else if playerChoice is paper and compChoice is scissors then
    		showResult("Win", compChoice)
    	else if playerChoice is paper and compChoice is rock then
    		showResult("Lose", compChoice)
    		
    	else if playerChoice is scissors and compChoice is rock then
    		showResult("Win", compChoice)
    	else if playerChoice is scissors and compChoice is paper then
    		showResult("Lose", compChoice)
    	end if
    end repeat
    
    
    on showResult(theResult, theChoice)
    	if theResult is "Tie" then
    		display dialog "Tie game, I also picked " & quoted form of theChoice & " too. Play again?" buttons {"No", "Yes"} cancel button "No" default button "Yes"
    	else if theResult is "Win" then
    		say "Ha, Ha, I picked " & quoted form of theChoice & ". You. Lose!" using "Zarvox" speaking rate 250
    		display dialog "Play again?" buttons {"No", "Yes"} cancel button "No" default button "Yes"
    	else -- lose
    		display dialog "Not fair, I picked " & quoted form of theChoice & " - you win! Play again?" buttons {"No", "Yes"} cancel button "No" default button "Yes"
    	end if
    end showResult
     
  4. thread starter macrumors member

    msindt19

    Joined:
    Jun 8, 2011
    Location:
    New York, New York
    #4
    Thanks for the replies. Unfortunately I'm in the office for the next "too many" hours and have to wait to play around with applescript.

    the "or" statements seem easy enough, the cancel button seems great rather than deal with the againValue being Yes or No.

    The handler you're talking about, is that:

    xxxxxx (yyyy, zzzz)

    ?

    What does that do, and why/how?

    Thanks again.
     
  5. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    A handler is like a function or subroutine, and in my example I am using the showResult handler to display the various result dialogs - parameters passed to the handler (theResult is used to choose the dialog, and theChoice is used to show the choice in the dialog) determine the particular content. The benefit here is that changing the win/lose/draw messages is easier, since they are in one place.

    More information about handlers is in the AppleScript Language Guide.
     
  6. thread starter macrumors member

    msindt19

    Joined:
    Jun 8, 2011
    Location:
    New York, New York
    #6
    Thanks a lot Red. I checked out the article...definitely helped clear things up for me.
     

Share This Page