使用AppleScript列出窗口中所有UI元素的名称(GUI脚本)

Bry*_*phy 4 user-interface applescript ui-automation

一行摘要-我正在寻找一种使AppleScript本身公开的名称,该名称期望特定的窗口内容(UI元素)在“ tell”语句中被提及。

如何使AppleScript列出希望我用来引用窗口内容的名称?

例如我可以说 tell current application to tell its front window's list 1 to ...

我正在尝试为窗口的所有内容查找类似于“列表1”的术语,以便可以将其与Accessibility Inspector中的列表进行交叉引用。

我尝试了这段代码,但是第一行生成了一个错误,提示“错误”无法将应用程序“系统事件”的“ class prcs” \“ iTunes”的窗口1的“ class ects”的名称键入字符串。 “从«class prcs»窗口1的«class ects»的名称到iTunes的数字-1700到字符串”

tell application "System Events" to tell process "iTunes" to set elementNames to the names of the entire contents of its front window as string
tell application "TextEdit"
    activate
    make new document at the front
    set the text of the front document to elementNames
    set WrapToWindow to text 2 thru -1 of (localized string "&Wrap to Window")
end tell
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 7

在GUI脚本编制中,entire contents [of]System Events应用程序上下文中的进程窗口上使用将返回活动应用程序中最前面窗口的所有UI元素(GUI控件)列表(将整个层次结构扁平化为list):

tell application "System Events"
  tell front window of (first application process whose frontmost is true)
    set uiElems to entire contents
  end tell
end tell
Run Code Online (Sandbox Code Playgroud)

如果您在中运行上述命令Script Editor,那么“结果”窗格将显示对象说明符的列表-例如,button 1 of window "Main" of application process "iTunes" of application "System Events"-不直接显示特定信息,但是可以从中至少收集UI元素的类型(类)(button在此示例中)以及同类型兄弟姐妹之间的位置1

为了目标的iTunes,例如,替代"iTunes"whose frontmost is true

请注意,如果只希望使用直接子元素,则可以使用UI elements [of]代替entire contents [of],并且不仅可以将其应用于窗口(以获取顶级UI元素),还可以将其应用于其中包含的任何UI元素以获取其子元素。孩子们。


您无法通过枚举转换为提取这些元素的属性字符串,as string作为你试过,但你可以提取在属性repeat循环:

假设用名称表示名(例如tablebutton),请尝试以下操作:

set classNames to {}
tell application "System Events"
    tell front window of (first application process whose frontmost is true)
        repeat with uiElem in entire contents as list
            set classNames to classNames & (class of uiElem as string)
        end repeat
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

请注意as list(从macOS 10.12.3版本开始)莫名其妙地需要此功能(使用时不需要UI elements [of])。

这将返回一个类名列表,例如{"splitter group", "text field", "button", "scroll area", ... },但其本身不足以定位特定元素,因为层次结构已被扁平化。