使用AppleScript列出窗口中所有UI元素的名称(GUI脚本) [英] Use AppleScript to list the names of all UI elements in a window (GUI scripting)

查看:1542
本文介绍了使用AppleScript列出窗口中所有UI元素的名称(GUI脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一行摘要-我正在寻找一种让AppleScript本身公开其期望特定窗口内容(UI元素)在"tell"语句中引用的名称的方法.

One line summary - I'm looking for a way to get AppleScript itself to reveal the name it expects a specific piece of window content (UI element) to be referred to as in a "tell" statement.

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

How do I get AppleScript to list the name it wants me to use to refer to a window's contents?

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

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

I'm trying to find out the term like "list 1" for all of the window's contents so I can cross-reference it with the list from Accessibility Inspector..

我尝试了此代码,但是第一行生成了一个错误,提示错误"无法将应用程序系统事件"的"class prcs" \"iTunes \"窗口1的"class ects"命名为类型字符串."编号-1700,从"class prcs""iTunes"窗口1的"class ects"的名称转换为字符串"

I tried this code but the first line generates an error saying "error "Can’t make names of «class ects» of window 1 of «class prcs» \"iTunes\" of application \"System Events\" into type string." number -1700 from names of «class ects» of window 1 of «class prcs» "iTunes" to string"

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

推荐答案

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

In GUI scripting, using entire contents [of] on a process's window in the context of the System Events application returns a list of all the UI elements (GUI controls) of the frontmost window in the active application (the entire hierarchy flattened to a 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

如果在Script Editor中运行上述操作,则结果"窗格将显示对象说明符(例如button 1 of window "Main" of application process "Music" of application "System Events")的列表,这些说明符不直接显示特定信息,而是显示您可以在同类型(1)的同级元素中至少收集UI元素的 type (类)(在本示例中为button)和 position )

If you run the above in Script Editor, the Result pane will show a list of object specifiers - e.g., button 1 of window "Main" of application process "Music" of application "System Events" - which do not directly reveal specific information, but from which you can glean least the type (class) of UI element (button, in this example) and the position among its siblings of the same type (1)

例如,要定位音乐,请用application process "Music"替换(first application process whose frontmost is true).

To target Music, for instance, substitute application process "Music" for (first application process whose frontmost is true) .

请注意,如果只希望使用立即子元素,则可以使用UI elements [of]代替entire contents [of] ,并且不仅可以将其应用于窗口(以获取顶级UI元素),但要获取其包含的任何UI元素以获取子元素.

Note that if you only want the immediate child elements, you can use UI elements [of] instead of entire contents [of], and you can apply it not just to a window (to get the top-level UI elements), but to any of the UI elements it contains to get their children.

您无法通过尝试使用as string将枚举转换为 string 来提取这些元素的属性,但是可以在repeat循环中提取属性:

You cannot extract properties of these elements by converting the enumeration to a string with as string as you've tried, but you can extract properties in a repeat loop:

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

Assuming that by names you mean the class names (such as table and button), try this:

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

请注意(从macOS 10.12.3版本开始)莫名其妙地需要 as list (对于UI elements [of]则不需要).

Note that as list is inexplicably needed (as of macOS 10.12.3) to make this work (not necessary with UI elements [of]).

这将返回诸如{"splitter group", "text field", "button", "scroll area", ... }之类的类名列表,但是,由于层次结构已被扁平化,因此其本身不足以定位特定元素.

This will return a list of class names such as {"splitter group", "text field", "button", "scroll area", ... }, which in itself is not enough to target a specific element, however, because the hierarchy has been flattened.

这篇关于使用AppleScript列出窗口中所有UI元素的名称(GUI脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆