String AppleScript检查列表项是否为';s值是整数

String AppleScript检查列表项是否为';s值是整数,string,list,integer,applescript,String,List,Integer,Applescript,在AppleScript中,我试图学习如何检查列表项的值,但当我尝试检查该项是否为整数时,我得到的结果不准确。首先,我在确定列表是否包含特定项目的章节中引用: property one : 1 property two : 2 property three : 3 property bad : 4 my checkProperty() on checkProperty() tell application "Finder" set someList to {one,

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

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
当我执行此操作时,我得到的是
而不是数字
,尽管每个项都是整数。如果我引用并执行:

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
)为止,并且由于
item
无法将字符串转换为整数而出错。正在搜索,因此我偶然发现并尝试接受以下答案:

但是当脚本运行时,它会为每个项目返回
而不是数字。如果我尝试使用以下第二个答案:


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

我建议使用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

要测试列表中的所有项目是否都是特定类型,请参阅。

上的文档:

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
这就是说,只要在使用值时根据需要强制转换值,并在此时处理任何错误,通常都会更快、更方便。e、 g.
“4”
可能是一个字符串,但在需要整数/实数的情况下,它是完全可以接受的,并且AppleScript通常会为您强制使用它,因此您不必自己做任何额外的事情

(注意:某些AppleScript强制是有损的,例如,
4.5作为整数
舍入到偶数(
4
)和
{a:1,b:2}作为列表
返回属性值(
{1,2}
),而不是单个项列表(
{a:1,b:2}
),这可能不是您想要的。但老实说,如果您需要一种真正健壮、可靠的语言,那么无论如何都不应该使用AppleScript。)

--

p、 您链接到的脚本编写要点指南是垃圾,最好避免。一半的样本甚至没有错,即使是技术上正确的样本也往往脆弱而缓慢。你想要一些像样的、通用的、追求价值的库,然后在bugreport.apple.com上被愚弄,要求苹果在10.13版中采用它们

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
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