2012-09-21 43 views
2

10.7.4 OSX獅子 AppleScript的在UI腳本使用AXIdentifier爲AppleScript的

我運行一個應用程序(在建房子,也沒有AppleScript字典),有我想要複製到一個靜態文本元素剪貼板併發送到另一個應用程序,但我很難得到它的工作。

我用靶向元素的腳本是這樣的:

Tell application "System Events" to set frontmost of process "*application*" to true 
Tell application "System Events" 
    Tell process "*application*" 
     Tell static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1 
      keystroke "a" using command down 
      delay 0.1 
      keystroke "c" using command down 
      delay 0.1 
     end tell 
    end tell 
end tell 
end tell 

會發生什麼,是從錯誤的元素錯誤的文字是我每次在不同的點點擊時間複製到剪貼板該應用程序(有許多文本字段)。

我注意到在UI訪問器/輔助功能訪問器中,當您將鼠標放在應用程序上時,應用程序中的每個UI元素都有唯一的AXIdentifier值。

無論如何要完成我正在嘗試做的事情,使用AXIdentifier值來定位該元素並從中複製文本?

感謝所有幫助,這是我的第一篇文章,我希望它值得! 〜TheLarkInn

回答

2

我不認爲有一種方法可以直接做你想做什麼。看起來像只有通過選擇器處理元素時才能訪問屬性。這是一個非常醜陋的解決方案,通過遍歷所有UI元素來完成你所要求的工作,但是對於更大的用戶界面來說它確實很慢,並且可能不適合任何生產級代碼。

tell application "System Events" 
    tell process "Some Process" 
    set tElements to entire contents of window "Some Window" 
    repeat with tElement in tElements 
     if (exists attribute "AXIdentifier" of tElement) then 
     if value of attribute "AXIdentifier" of tElement = "Some AXIdentifier" then set tText to value of tElement 
     end if 
    end repeat 
    end tell 
end tell 
tText 

我認爲使用UIElementInspector或Xcode的輔助檢查,以建立一個選擇字符串是要走的路!

+0

這就是我想知道的。我使用的是Accessibility Inspector,這是AXIdentifier的列表。我很好奇,如果這是曾經使用過。 –

0
Tell application "*application*" to activate 

Tell application "System Events" 
    Tell application process "*application*" 
     set textStaticTextValue to value of static text 1 of tab view 1 scroll area 1 of splitter group 1 of splitter group 1 of splitter group 1 of window 1 
    end tell 
end tell 
0

你可以通過使用AppleScript的過濾來做到這一點。例如,要獲得來源:在Apple Mail等郵件撰寫窗口彈出菜單,沒有無障礙設施的描述,你可以匹配,但有如下獨特的AXIdentifier您可以匹配爲:

tell application "System Events" 
    tell application process "Mail" 
     tell window 1 
      get first pop up button whose value of attribute "AXIdentifier" is "popup_from" 
     end tell 
    end tell 
end tell 

這比在AppleScript中循環更有效,因爲它只涉及向系統事件發送一個Apple事件。

相關問題