如何在程序中修复未定义的变量?

时间:2019-06-17 02:40:28

标签: applescript syntax-error

关于AppleScript,我是一个初学者。我正在尝试开发一个可以生成实际随机数的程序。当我设置将用于生成随机数的数字的值时,编辑器给我一个错误:

  

未定义变量numberOne。

我知道这意味着什么,但是我不知道为什么它没有定义。有人可以帮忙吗?

我尝试查看是否只有字符串而不是文本可以工作,但这似乎没有用。可能确实很容易解决此问题,但是正如我所说,我是一个初学者。

on variableValue()
    set numberOne to text returned of (display dialog "Variable One:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
    set numberTwo to text returned of (display dialog "Variable Two:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
    set numberThree to text returned of (display dialog "Variable Three:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
    set numberFour to text returned of (display dialog "Variable Four:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
    set numberFive to text returned of (display dialog "Variable Five:" default answer "" with icon note buttons {"Cancel", "Continue"} default button 2 with title "Random Number Generator 3") as string
    set useSameValues to button returned of (display alert "Use same values?" message "If you re-generate a new number, do you want to use the same values?" as critical buttons {"Cancel", "No", "Yes"} default button 3)
    if useSameValues = "No" then
        set useSameValuesTwo to "false"
    else if useSameValues = "Yes" then
        set useSameValuesTwo to "true"
    end if
end variableValue
variableValue()
on randomNumber()
    -- The line of code just beneath this text is where the error shows up (This is my first program that utilizes handlers, so something could be wrong there).
    set numberSix to (numberOne + numberTwo + numberThree + numberFour + numberFive)
    set numberSeven to (numberSix * (random number from 1 to (random number from 2 to 100)))
    set possibleValueOne to (random number from 1 to 5)
    if possibleValueOne = 1 then
        set numberEight to (numberSeven - numberOne)
    else if possibleValueOne = 2 then
        set numberEight to (numberSeven - numberTwo)
    else if possibleValueOne = 3 then
        set numberEight to (numberSeven - numberThree)
    else if possibleValueOne = 4 then
        set numberEight to (numberSeven - numberFour)
    else if possibleValueOne = 5 then
        set numberEight to (numberSeven - numberFive)
    end if
    set numberNine to (numberEight - 100000)
end randomNumber
randomNumber()

1 个答案:

答案 0 :(得分:1)

在AppleScript(与大多数语言一样)中,变量具有scope,在此变量中可以在任何给定脚本对象中识别已声明的标识符。在处理程序内声明的变量是该处理程序的局部变量,并且不在该处理程序之外,因此您需要提供一些方法以使它们在其他地方可用。有几种方法可以做到这一点,例如声明全局变量或 properties ,或让处理程序将值返回给调用方。

在您的示例中,在variableValue()处理程序中声明的变量在处理程序之外不可用,因此,将这些变量声明为 global 可能是最简单的,例如通过在脚本的开头添加以下声明:

global numberOne, numberTwo, numberThree, numberFour, numberFIve