PDA

View Full Version : cleaning up applescript




msindt19
Jun 21, 2011, 10:21 PM
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:


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



kainjow
Jun 21, 2011, 11:22 PM
Here's my clean up of it:

set againValue to "Yes"
repeat until againValue is not equal to "Yes"
set rockChoice to "Rock"
set paperChoice to "Paper"
set scissorsChoice to "Scissors"
set question1 to display dialog "Want to play a game?" buttons {rockChoice, paperChoice, scissorsChoice}
set playerChoice to button returned of question1
set compChoice to some item of {rockChoice, paperChoice, scissorsChoice}
set displayIt to missing value
if playerChoice is compChoice then
set displayIt to "Tie game, I picked \"" & playerChoice & "\" too. Play again?"
else if (playerChoice is rockChoice and compChoice is scissorsChoice) or (playerChoice is paperChoice and compChoice is rockChoice) or (playerChoice is scissorsChoice and compChoice is paperChoice) then
set displayIt to "Not fair, I picked \"" & compChoice & "\". You win! Play again?"
else if (playerChoice is rockChoice and compChoice is paperChoice) or (playerChoice is paperChoice and compChoice is scissorsChoice) or (playerChoice is scissorsChoice and compChoice is rockChoice) then
say "Ha, Ha, I picked " & compChoice & ". You Lose!" using "Zarvox"
set displayIt to "Play again?"
end if
display dialog displayIt buttons {"Yes", "No"}
set againValue to button returned of result
end repeat

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.

Red Menace
Jun 22, 2011, 12:20 AM
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:

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

msindt19
Jun 22, 2011, 08:40 AM
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.

Red Menace
Jun 22, 2011, 07:20 PM
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 (http://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html%23//apple_ref/doc/uid/TP40000983-CH206-CJBIDBJH).

msindt19
Jun 27, 2011, 02:14 PM
Thanks a lot Red. I checked out the article...definitely helped clear things up for me.