2017-08-12 79 views
0

對於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的值?

回答

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 

最後的測試檢查該值已在靜態文本或迴歸「」如果沒有被發現。

相關問題