如何查找变量中的项目数? (AppleScript)

问题描述 投票:0回答:1

我正在尝试查找变量中的项数,我尝试创建一个while循环来测试变量项是否存在,如果确实存在,则转到下一个项并测试其是否存在,这重复该操作,直到该项目不存在,然后显示当前项目编号,该编号应为变量的最后一个项目。这是我的代码:

    set stuff to "123456789"
    set x to "1"
    set num to item x of stuff
    if exists item x of stuff then
    repeat while exists item (x + 1) of stuff
        if exists (item x of stuff) then
            set x to (x + 1)
        else
            set num to x
        end if
    end repeat
    end if
    display dialog num

当前,当我运行此代码时,出现错误:“无法获得“ 123456789”的项目10。”据我所知,该变量的最后一项是10,但该信息对我来说是不好的错误消息。在此先感谢

applescript exists
1个回答
0
投票

AppleScript命令count计算对象中元素的数量,例如count of stuff。另外,诸如listrecordtext之类的类也具有length属性,例如length of stuff

set stuff to "123456789"
repeat with x from 1 to (count stuff)
    log x -- just using x to count
end repeat
display dialog "Count is " & x & return & "Length is " & (length of stuff)

请参阅AppleScript Language Guide了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.