# combining simple math using dialog box responses.

I am trying to work out how to combine some numbers using responses from dialog boxes.

i.e. If the answer to question A is yes, then add 1 if its no, do nothing open dialog box for question B
if answer B is no, do nothing, if its yes add 2, open dialog box for question C
if answer C is yes, add 4, it its no, do nothing, open dialog box for question D
if answer for D is yes, add 8, if its no, do nothing end dialog/show results

A-yes +1
B-yes +2
C-no +0
D-yes +8
Result would be 11

so they can have almost any combination of the numbers.

I can do dialog boxes ok, but I have no idea how to do the if/thens of the math.

any suggestions?

Hi,

the simple solution

``````
set theResult to 0
repeat with i from 1 to 4
set b to button returned of (display dialog "Question " & (ASCII character (64 + i)) buttons {"No", "Yes"})
if b is "Yes" then set theResult to (theResult + (2 ^ (i - 1))) as integer
end repeat
theResult
``````

wow, thanks for the quick reply!

I was a little vague in my request, but how can I customize each question and still have the same process happen?

use a list of strings, or read 4 paragraphs from a text file, or.
for example

``````
property questionList : {"Question A", "Question B", "Question C", "Question D"}

set theResult to 0
repeat with i from 1 to 4
set b to button returned of (display dialog item i of questionList buttons {"No", "Yes"})
if b is "Yes" then set theResult to (theResult + (2 ^ (i - 1))) as integer
end repeat
display dialog "The result is: " & theResult buttons {"Done"} default button 1

``````

new problem…

I have tried many things in the place of the red “then set theColor to red” and it keeps saying that either I can’t make red into a type boolean, or red isn’t defined.

``````tell application "Finder" to display dialog "Ok, I am getting something, does the word 'cherry' mean anything to you?" buttons ["Nope", "Yes, it does"]
if button returned of result is "Yes, it does" then set theColor to red and (display dialog "Great, we can move on.")
if button returned of result is "nope" then set theColor to black and (display dialog "Oh.. well I meant 'Black Cherry', we'll move on.")
``````

I want to have this happen a little after.

``````if theColor is red then display dialog
``````

and also

``````if theColor is black then display dialog
``````

Also, I am crap at applescript, but I am getting better.

and is a boolean operator, nothing else.
If you want to execute multiple commands in an if - end if block, use multiple lines

``````
if button returned of result is "Yes, it does" then
set theColor to red
display dialog "Great, we can move on."
end if
``````

If you want theColor to be a text variable, you need to put the text in quotes.

``````set theColor to "red"
get theColor
--> "red"

``````

Ok, well in that case I am getting an error

“The variable red is not defined”

I don’t know how to define it so that I can use it later.

Also, thanks for being so patient with me.

Is this what you are trying to do?

``````display dialog "Ok, I am getting something, does the word 'cherry' mean anything to you?" buttons ["Nope", "Yes, it does"]
if button returned of result is "Yes, it does" then
set theColor to "red"
display dialog "Great, we can move on."
else
set theColor to "black"
display dialog "Oh.. well I meant 'Black Cherry', we'll move on."
end if

get theColor
``````

And then you would handle your other if statements later like this

``````if theColor is "red" then display dialog "red"

if theColor is "black" then display dialog "black"
``````

Ok, I almost have the whole thing done.

This time I only have one small things and it will be complete!

Also, Hey James, thanks for chiming in.

— I had two things, but I solved the first one I didn’t realize I could just put another “&” after the variable…

when I run it for theColor selection of black, it gives me an error that the variable result is not defined, when the same “result” variable it is highlighting is working if I use theColor selection of red.

``````if theColor is "red" then display dialog "Your card is a Heart correct?" buttons ["No, its not", "Yes, it is"]
if button returned of result is "Yes, it is" then
display dialog "I thought so, you're thinking of the " & theResult buttons {"Have a great day!"} default button 1
else
display dialog "Oh, well I sometimes have trouble with the " & theResult buttons {"Have a great day!"} default button 1
end if

if theColor is "black" then display dialog "Your card is a Spade correct?" buttons ["No, its not", "Yes, it is"]
if button returned of result is "Yes, it is" then
display dialog "I thought so, you're thinking of the " & theResult buttons {"Have a great day!"} default button 1
else
display dialog "Oh, well I sometimes have trouble with the " & theResult buttons {"Have a great day!"} default button 1
end if

``````

When this is finished I will post the full script… and let you guys play with it, maybe find a cleaner, less junked together way of making it.

what is theResult supposed to be because it’s not defined in the code you have provided.

here is the entire script… it is a 'think of a card and it will tell you what it is. based on an effect I have been doing for years, I just decided to write it up for a computer to do it.

if you go through it, and the card you selected was a red one, either a heart or a diamond, then it works fine.
but if it were a club or a spade I get the error.

run it and see what you think. I know it is not very clean, and it probably goes about the process in an inefficient manner.

``````tell application "Finder" to display dialog "Think of any playing card in a normal deck...
And for the moment, just forget about the suit, and the color of your card. Only think of the number or letter."

property questionList : {"Is the rank of your card equal to any of these? 3, Jack, 5, Ace, 9, 7?", "Ok, how about these? 10, 3, 6, 2, 7, Jack?", "Almost done, how about these? 5, Queen, 6, 7, 4?", "Last round: Queen, 9, 8, Jack, 10"}

set theResult to 0
repeat with i from 1 to 4
set b to button returned of (display dialog item i of questionList buttons {"No", "Yes"})
if b is "Yes" then set theResult to (theResult + (2 ^ (i - 1))) as integer
end repeat

--figure out the color
set theColor to 0
tell application "Finder" to display dialog "Great, now I want you to remember the color of your card. imagine it bright and vivid in your mind."
if button returned of result is "OK" then
tell application "Finder" to display dialog "Ok, I am getting something, does the word 'cherry' mean anything to you?" buttons ["Nope", "Yes, it does"]
if button returned of result is "Yes, it does" then
set theColor to "red"
else
set theColor to "black"
display dialog "Oh.. well I meant 'Black Cherry', we'll move on."
end if
if theResult is 1 then set theResult to "Ace"
if theResult is 2 then set theResult to "Two"
if theResult is 3 then set theResult to "Three"
if theResult is 4 then set theResult to "Four"
if theResult is 5 then set theResult to "Five"
if theResult is 6 then set theResult to "Six"
if theResult is 7 then set theResult to "Seven"
if theResult is 8 then set theResult to "Eight"
if theResult is 9 then set theResult to "Nine"
if theResult is 10 then set theResult to "Ten"
if theResult is 11 then set theResult to "Jack"
if theResult is 12 then set theResult to "Queen"
if theResult is 0 then set theResult to "King"
if theResult is 15 then set theResult to "--You cheated, it wasn't in all four!"
end if

if theColor is "red" then display dialog "Your card is a Heart correct?" buttons ["No, its not", "Yes, it is"]
if button returned of result is "Yes, it is" then
display dialog "I thought so, you're thinking of the " & theResult & " of Hearts" buttons {"Have a great day!"} default button 1
else
display dialog "Oh, well I sometimes have trouble with the " & theResult & " of Diamonds" buttons {"Have a great day!"} default button 1

if theColor is "black" then display dialog "Your card is a Spade correct?" buttons ["No, its not", "Yes, it is"]
if button returned of result is "Yes, it is" then
display dialog "I thought so, you're thinking of the " & theResult & " of Spades" buttons {"Have a great day!"} default button 1
else
display dialog "Oh, well I sometimes have trouble with the " & theResult & " of Clubs" buttons {"Have a great day!"} default button 1
end if
end if

``````

Hi,

your structure of the filter to determine the color doesn’t work.
Here’s your script (a bit cleaned up), I hope you like the card color symbols

``````
property cardList : {"King", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "--You cheated, it wasn't in all four!"}
property colorList : {«data utxt2665», «data utxt2666», «data utxt2660», «data utxt2663»}
property questionList : {"Is the rank of your card equal to any of these? 3, Jack, 5, Ace, 9, 7?", "Ok, how about these? 10, 3, 6, 2, 7, Jack?", "Almost done, how about these? 5, Queen, 6, 7, 4?", "Last round: Queen, 9, 8, Jack, 10"}

repeat
tell application "Finder" to display dialog "Think of any playing card in a normal deck...
And for the moment, just forget about the suit, and the color of your card. Only think of the number or letter."

set theResult to 0
repeat with i from 1 to 4
set b to button returned of (display dialog item i of questionList buttons {"No", "Yes"})
if b is "Yes" then set theResult to (theResult + (2 ^ (i - 1))) as integer
end repeat
try
set theResult to item (theResult + 1) of cardList
exit repeat
on error
display dialog "--You cheated, it wasn't in all four!" buttons {"Cancel", "Try Again"} default button 2
end try
end repeat

--figure out the color
tell application "Finder" to display dialog "Great, now I want you to remember the color of your card. imagine it bright and vivid in your mind."
if button returned of result is "OK" then
tell application "Finder" to display dialog "Ok, I am getting something, does the word 'cherry' mean anything to you?" buttons ["Nope", "Yes, it does"]
if button returned of result is "Yes, it does" then
set theColor to 1 -- "red"
else
set theColor to 3 -- "black"
display dialog "Oh.. well I meant 'Black Cherry', we'll move on."
end if
end if
display dialog "Your card is a " & item theColor of colorList & " correct?" buttons ["No, its not", "Yes, it is"]
if button returned of result is "Yes, it is" then
display dialog "I thought so, you're thinking of the " & item theColor of colorList & theResult buttons {"Have a great day!"} default button 1
else
display dialog "Oh, well I sometimes have trouble with the " & item (theColor + 1) of colorList & theResult buttons {"Have a great day!"} default button 1
end if

``````

ha, thanks…

I like the symbols.

I have made vector graphics of an entire deck of cards and I want to use them for the whole thing eventually, but I am glad you got this version working…