获取具有特定值的静态文本的索引号(AppleScript)

问题描述:

对于AS来说是新手,但我还没有遇到过类似的问题。我正在使用AppleScript从应用程序窗口的UI中获取信息。该窗口中有一个静态文本x,其值为“Name”,我想获取下一个静态文本的值,它将是静态文本x + 1(“John Smith”)。然而,x的值随每种情况而有所不同,所以我不能简单地引用静态文本9,因为它可能是下一次静态文本47。获取具有特定值的静态文本的索引号(AppleScript)

我现在的解决方案是获取窗口中每个可用静态文本的值,并将其添加到列表中。然后我找到值为“Name”的第一个项目,并参考该项目+ 1以获取我想要的实际名称。但是,由于该窗口可能包含相当多的静态文本,因此此方法需要一些不必要的时间。

set MyList to {} 
tell application "System Events" to tell process "cBK" to set y to count static text of scroll area 2 of splitter group 1 of window 1 
repeat with x from 1 to y 
tell application "System Events" to tell process "cBK" to set end of MyList to value of static text x of scroll area 2 of splitter group 1 of window 1 
end repeat 
on findFirst(lst, val) 
local lst, val, i 
try 
    if lst's class is not list then error "not a list." number -1704 
    if {val} is not in lst then return 0 
    script k 
     property l : lst 
    end script 
    repeat with i from 1 to count of k's l 
     if k's l's item i is val then return i 
    end repeat 
on error eMsg number eNum 
    error "Can't findFirst: " & eMsg number eNum 
end try 
end findFirst 
set Name to item (findFirst(MyList, "Name") + 1) of MyList 

有一个简单的办法让静态文本x的X具有一定的价值,所以我可以得到静态文本X + 1的值?

您可以从您的前窗获得单指令列表中的所有静态文本值。然后在该列表中搜索,找到后即可获取下一个项目。脚本波纹管举了一个例子:

set myTarget to "Name" 
tell application "System Events" 
    tell process "cBK" to set myValues to value of every static text of front window 
    repeat with i from 1 to count of myValues 
     if item i of myValues is myTarget then exit repeat 
    end repeat 
    if i < (count of myValues) then 
     set myNext to item (i + 1) of myValues 
    else 
     set myNext to "" -- not found 
    end if 
end tell 

最后的测试检查该值已在静态文本或回归“”如果没有被发现。