Resolved Help with applescript variable please!

Discussion in 'Mac Programming' started by Roadrun3r, May 29, 2011.

  1. Roadrun3r, May 29, 2011
    Last edited: Jun 2, 2011

    macrumors newbie

    Joined:
    May 29, 2011
    #1
    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 [COLOR="Red"]result[/COLOR] = {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
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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:
    [b][color=#0016b0]display dialog[/color][/b] "Take -The Amazing Riddle Quiz-?" [color=#0016b0]buttons[/color] {"Yes, please!", "No, thank you."} [color=#0016b0]default button[/color] 2
    [b]if[/b] [color=#4415b0]button returned[/color] [b]of[/b] [color=#6c05d3]result[/color] [b]is[/b] "Yes, please!" [b]then[/b]
        [b][color=#0016b0]display dialog[/color][/b] "To answer each riddle, type your ONE WORD answer into the text field with no caps. Check your spelling before submitting!" [color=#0016b0]buttons[/color] {"Okey doke, lets go!", "Nevermind, I don't want to."} [color=#0016b0]default button[/color] 2
        [b]if[/b] [color=#4415b0]button returned[/color] [b]of[/b] [color=#6c05d3]result[/color] [b]is[/b] "Okey doke, lets go!" [b]then[/b]
            
            [b]set[/b] [color=#408000]diagResult[/color] [b]to[/b] [color=#4a1e7f]false[/color]
            [b]repeat[/b] [b]until[/b] [color=#408000]diagResult[/color] = {[color=#4415b0]text returned[/color]:"stapler", [color=#4415b0]button returned[/color]:"Submit answer"}
                [b][color=#0016b0]display dialog[/color][/b] "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)..." [color=#0016b0]default answer[/color] "" [color=#0016b0]buttons[/color] {"Submit answer", "Hint"} [color=#0016b0]default button[/color] 1
                [b]set[/b] [color=#408000]diagResult[/color] [b]to[/b] [color=#6c05d3]result[/color]
                [b]if[/b] [color=#4415b0]button returned[/color] [b]of[/b] [color=#408000]diagResult[/color] [b]is[/b] "Hint" [b]then[/b] [b][color=#0016b0]say[/color][/b] "I live in your office" [color=#0016b0]using[/color] "Alex"
            [b]end[/b] [b]repeat[/b]
        [b]end[/b] [b]if[/b]
    [b]end[/b] [b]if[/b]
    [b]end[/b]
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    result is a kind of funky special variable. It always contains the result of the last statement. if statements are statements...
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Ew, that's ugly, but good to know.
     
  5. thread starter macrumors newbie

    Joined:
    May 29, 2011
    #5
    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. :)
     

Share This Page