“将foo设为”为数字返回的文本?

时间:2014-05-28 20:25:10

标签: string integer applescript automator

我知道你可以做set foo to text returned of (display dialog "Dialog" default answer "default")。 我尝试了set foo to number returned ofset foo to integer returned of的所有内容,但我似乎无法找到返回数字的关键字。有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

您需要返回文本,然后将其强制转换为整数:

set foo to (text returned of (display dialog "Enter a number" default answer "42")) as integer

或者,分解更多来说明:

display dialog "Enter a number" default answer "42"
set foo to text returned of the result
set foo to foo as integer

我会把它分开,所以如果它不是一个数字,你可以围绕强制包装一个try块。

set foo to text returned of (display dialog "Enter a number" default answer "42")
try
    set foo to foo as integer
on error err
    set foo to 0
end

而且,这将是一种反复询问的方式,直到他们提供有效数字:

set msg to "Enter a number"
repeat
    set foo to text returned of (display dialog msg default answer "42")
    try
        set foo to foo as integer
        exit repeat
    on error err
        set msg to "That's not a valid number. Please enter a number."
    end try
end repeat
return foo