AppleScript检查列表项的值是否为整数

时间:2017-01-05 15:10:39

标签: string list integer applescript

在AppleScript中,我试图学习如何检查列表项的值,但是当我尝试检查该项是否为整数时,我得到的结果不准确。首先,我在确定列表是否包含特定项目一节中引用了Apple's Automation scripting guide

property one : 1
property two : 2
property three : 3
property bad : 4

my checkProperty()

on checkProperty()
    tell application "Finder"
        set someList to {one, two, three, bad}
        if someList contains string then
            display dialog "good"
        else
            display dialog "not numerical"
        end if
    end tell
end checkProperty

当我执行此操作时,虽然每个项目都是整数,但我得到not numerical。如果我引用Applescript, converting every item in list to integer并执行:

property one : 1
property two : 2
property three : 3
property bad : "bad"

my checkProperty()

on checkProperty()
    tell application "Finder"
        set someList to {one, two, three, bad}
        repeat with theItem from 1 to length of someList
            set item theItem of someList to (item theItem of someList as integer)
            if theItem is integer then
                display dialog theItem
            else
                display dialog "not numerical"
            end if
        end repeat
    end tell
end checkProperty

它一直有效,直到列表的最后一个条目(bad)和错误,因为theItem无法将字符串转换为整数。在SO上搜索我遇到Check if variable is number: Applescript并尝试使用number接受的答案:

property one : 1
property two : 2
property three : 3
property bad : "bad"

my checkProperty()

on checkProperty()
    tell application "Finder"
        set someList to {one, two, three, bad}
        repeat with theItem from 1 to length of someList
            if class of theItem is number then
                display dialog theItem
            else
                display dialog "not numerical"
            end if
        end repeat
    end tell
end checkProperty

但是当脚本运行时,它会为每个项目返回not numerical。如果我尝试使用integer的第二个答案:

if class of theItem is integer then
    display dialog theItem
else
    display dialog "not numerical"
end if

我收到一个单独的对话框来计算条目。如何在AppleScript中检查列表项的值,以查看它是整数还是字符串而不会抛出错误?

2 个答案:

答案 0 :(得分:0)

我建议玩try / on错误循环。我不是在我的Mac atm,但试试这个:

property one : 1
property two : 2
property three : 3
property bad : "bad"

my checkProperty()

on checkProperty()
    tell application "Finder"
        set someList to {one, two, three, bad}
        repeat with theItem from 1 to length of someList
            try
                set item theItem of someList to (item theItem of someList as integer)
                display dialog theItem
            on error
                display dialog "not numerical"
            end try
        end repeat
    end tell
end checkProperty

try/on error上的文档。

答案 1 :(得分:0)

测试列表中的所有项目是否为特定类型:

to isListOfType(theList, theType)
  return (count {theList} each list) = 1 ¬
      and (count theList each theType) = (count theList)
end isListOfType

isListOfType({1, 2, 3}, integer) --> true
isListOfType({1, 2, 3, 4.5}, number) --> true
isListOfType({1, 2, 3, 4.5, "6"}, number) --> false

也就是说,在使用它们时根据需要强制转换/强制值通常会更快更方便,并且当时处理任何错误。例如"4"可能是一个字符串,但是在预期整数/实数的情况下它是完全可以接受的,AppleScript通常会为你强制它,所以你不必自己做任何额外的事情。

(警告:一些AppleScript强制是有损的,例如4.5 as integer轮甚至(4)和{a:1, b:2} as list返回属性的值({1, 2})而不是单个-item list({{a:1, b:2}}),这可能不是你想要的。但老实说,如果你需要一种非常强大,可靠的语言,那么你不应该使用AppleScript。)

-

P.S。您链接到的Scripting Essentials指南是垃圾,最好避免使用。一半的样本甚至没有错,甚至技术上正确的样本通常都是脆弱的。你需要一些体面的通用值重置库try these,然后在bugreport.apple.com上转向this ticket,要求Apple将它们用于10.13。