如何使用if命令更改applescript中的变量

时间:2015-12-15 18:58:39

标签: applescript

我已经永远尝试过这个,我做错了什么?

你得到我的变量repeatvar

set repeatvar to the text returned of (display dialog "set repeatvar")

if repeatvar is greater than "1000" then
    set repeatvar to "1"
end if

当我输入任何数字时,无论数字如何,它总是将该数字设置为1。我做错了什么?

2 个答案:

答案 0 :(得分:1)

如果你想对字符串进行数值比较 - 这些字符串的行为与整数不同 - 你必须明确告诉AppleScript考虑numeric strings

set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
considering numeric strings
    if repeatvar is greater than "1000" then
        set repeatvar to "1"
    end if
end considering

答案 1 :(得分:0)

第一行的语法不正确。使用这种语法,你永远不会得到"文本返回"有效值,但只返回有效的"按钮"。 要返回文本,您必须告诉脚本您希望用户输入值。这是通过添加单词"默认答案"":

来完成的
set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")

您的文字的第二个问题是您要将输入的文字与字符串" 1000"进行比较。比较字符串和数字并不总能给出正确的结果。例如,字符串使用字母顺序和" 9"大于" 1000",因为" 9" > " 1"

脚本波纹管将起作用:

set repeatvar to the text returned of (display dialog "set repeatvar" default answer "")
try
set myNumber to repeatvar as integer
on error
set myNumber to 0
end try
if myNumber is greater than 1000 then
set repeatvar to "1"
end if