Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 29, 2011, 02:19 PM   #1
Roadrun3r
macrumors newbie
 
Join Date: May 2011
Help with applescript variable please!

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:

Code:
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

Last edited by Roadrun3r; Jun 2, 2011 at 04:54 PM. Reason: added code tags
Roadrun3r is offline   0 Reply With Quote
Old May 29, 2011, 03:34 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
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:

Code:
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
kainjow is offline   0 Reply With Quote
Old May 29, 2011, 03:37 PM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by kainjow View Post
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...
robbieduncan is offline   0 Reply With Quote
Old May 29, 2011, 03:41 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by robbieduncan View Post
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.
kainjow is offline   0 Reply With Quote
Old May 29, 2011, 04:22 PM   #5
Roadrun3r
Thread Starter
macrumors newbie
 
Join Date: May 2011
Problem Solved!

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.
Roadrun3r is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
String and variable concatenation in AppleScript tekboi Mac Programming 6 Apr 15, 2014 02:37 PM
Applescript newb: declaring a variable in another script. Vanilla Face Mac Programming 2 Jan 5, 2014 08:39 PM
AppleScript: How do I save multiple Excel sheets to one pdf using Applescript sbtaylor1 Mac Applications and Mac App Store 0 Oct 29, 2013 12:32 AM
Applescript "Variable hdg is not defined" Dreamspinner Mac Programming 3 Feb 12, 2013 07:33 PM
AppleScript: Set variable to every XML element of a given name moonman239 Mac Programming 13 Jan 2, 2013 03:55 PM

Forum Jump

All times are GMT -5. The time now is 08:06 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC