PDA

View Full Version : [Resolved] Help with applescript variable please!




Roadrun3r
May 29, 2011, 01:19 PM
I am trying to make a "Riddle quiz, just for fun, and I keep running into the same problem; I get an error saying that the variable "result" is not defined. The word that is highlighted after I click ok is in red.
The goal of the script will eventually be to have the person using it to have to answer each riddle correctly before moving on, and if a hint is needed it will be given.
If possible, I would like to know whether or not its possible to "grade" the quiz with a dialog box at the end if i made it five questions long.

Here's the script:

display dialog "Take -The Amazing Riddle Quiz-?" buttons {"Yes, please!", "No, thank you."} default button 2
if button returned of result is "Yes, please!" then
display dialog "To answer each riddle, type your ONE WORD answer into the text field with no caps. Check your spelling before submitting!" buttons {"Okey doke, lets go!", "Nevermind, I don't want to."} default button 2
if button returned of result is "Okey doke, lets go!" then

set result to false
repeat until result = {textreturned:"stapler", buttonreturned: "Submit answer"
display dialog "With pointed fangs I sit in wait,
With piercing force I dole out fate,
Over bloodless victims proclaiming my might,
Eternally joining in a single bite.
I am a(n)..." default answer "" buttons {"Submit answer", "Hint"} default button 1
if button returned of result is "Hint" then say "I live in your office" using "Alex"

end repeat
end if
end if
end



kainjow
May 29, 2011, 02:34 PM
I'm not 100% sure why, but somehow after the if-statement that checks for "Hint", the result variable becomes invalid. I think it might only be valid immediately after the display dialog call.

I was able to fix it by storing it in another variable:

display dialog "Take -The Amazing Riddle Quiz-?" buttons {"Yes, please!", "No, thank you."} default button 2
if button returned of result is "Yes, please!" then
display dialog "To answer each riddle, type your ONE WORD answer into the text field with no caps. Check your spelling before submitting!" buttons {"Okey doke, lets go!", "Nevermind, I don't want to."} default button 2
if button returned of result is "Okey doke, lets go!" then

set diagResult to false
repeat until diagResult = {text returned:"stapler", button returned:"Submit answer"}
display dialog "With pointed fangs I sit in wait,
With piercing force I dole out fate,
Over bloodless victims proclaiming my might,
Eternally joining in a single bite.
I am a(n)..." default answer "" buttons {"Submit answer", "Hint"} default button 1
set diagResult to result
if button returned of diagResult is "Hint" then say "I live in your office" using "Alex"
end repeat
end if
end if
end

robbieduncan
May 29, 2011, 02:37 PM
I'm not 100% sure why, but somehow after the if-statement that checks for "Hint", the result variable becomes invalid.

result is a kind of funky special variable. It always contains the result of the last statement. if statements are statements...

kainjow
May 29, 2011, 02:41 PM
result is a kind of funky special variable. It always contains the result of the last statement. if statements are statements...

Ew, that's ugly, but good to know.

Roadrun3r
May 29, 2011, 03:22 PM
Thanks for your help! I wouldn't of thought of that, as I am new to applescript (and programming as a whole). I was trying to move the set result to a bunch of different places and rephrasing what I want, but that didnt turn out so well. :)