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

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

问题描述

一行总结 - 我正在寻找一种方法,让 AppleScript 本身显示它期望在tell"语句中引用的特定窗口内容(UI 元素)的名称.

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?

例如我可以说告诉当前应用程序将其前窗口的列表1告诉...

我正在尝试为所有窗口内容找出诸如列表 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..

我试过这段代码,但第一行产生了一个错误,说错误"不能将窗口 1 的类 prcs"\iTunes\"的 «class ects» 的名称设为应用程序 \System Events\"输入字符串."从«class prcs»iTunes"窗口1的«class ects»名称的数字-1700到字符串"

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 脚本中,使用 entire contents [of] 在进程窗口的上下文中System Events 应用程序返回活动应用程序最前面窗口的所有 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 "主要"申请流程音乐"应用程序系统事件" - 不直接显示特定信息,但您可以从中至少收集到 UI 元素(button类型(类)>,在这个例子中)和它的同类型兄弟之间的位置(1)

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)

以Music为例,将应用程序进程音乐"替换为(最前面为真的第一个应用程序进程).

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

注意如果你只想要直接子元素,你可以使用UI元素[of]代替整个内容[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 stringstring 来提取这些元素的属性,但您可以在 中提取属性重复循环:

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:

假设 names 指的是 class 名称(例如 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

请注意,as list 莫名其妙地需要(从 macOS 10.12.3 开始)才能使这项工作(对于 UI 元素 [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天全站免登陆