在 Applescript 的 UI 脚本中使用 AXIdentifier

Sea*_*kin 5 scripting user-interface applescript

10.7.4 OSX Lion 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
Run Code Online (Sandbox Code Playgroud)

会发生的情况是,每次我在应用程序的不同位置(有许多文本字段)单击时,错误元素中的错误文本都会被复制到剪贴板。

我在 UI Accessor/Accessibility Accessor 中注意到,当您将鼠标悬停在应用程序中的每个 UI 元素上时,它们都有一个唯一的 AXIdentifier 值。

无论如何要完成我想要做的事情,使用 AXIdentifier 值来定位该元素并从中复制文本?

感谢所有的帮助,这是我的第一篇文章,我希望它是值得的!~TheLarkInn

小智 3

我认为没有办法直接做你想做的事情。似乎只有在通过选择器拥有元素的句柄后才能访问属性。这是一个非常丑陋的解决方案,它通过迭代所有 UI 元素来完成您所要求的操作,但对于较大的 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
Run Code Online (Sandbox Code Playgroud)

我认为使用Xcode 中的UIElementInspector或 Accessibility Inspector 来构建选择器字符串是正确的方法!