【问题标题】:How to get Information of a Selected UI Element?如何获取所选 UI 元素的信息?
【发布时间】:2022-08-15 21:01:49
【问题描述】:

如何识别 AppleScript 中的 UI 元素?

在 AppleScript 中,可以像这样点击 UI 元素:

单击应用程序 \"System Events\" 的应用程序进程 \"Umsatz\" 的窗口 1 的工作表 1 的文本字段 1 的项目 1

但是也可以读取活动元素吗? Screenshot shows an active UI element

  • 它很大程度上取决于特定的 UI 元素,您可以尝试value of ...
  • 当我使用set MyElement to value of the UI element focused 时,我得到\"\"。当我使用set MyElement to the UI element focused 时,我得到:text field 1 of window 1 of application process \"Umsatz\" of application \"System Events\" 为什么答案使用另一种格式的起始帖子?为什么不可能制作一条新线?

标签: user-interface applescript


【解决方案1】:

这是一个处理程序,它将返回属于特定UI element 的所有propertiesattributesactions 的记录:

on info about UIElement
        set my text item delimiters to linefeed & linefeed
        tell application id "com.apple.SystemEvents" to script Object
                property parent : UIElement
                property AXAttributes : a reference to my (the ¬
                        attributes whose name ≠ "AXURL")
                property AXValues : value of AXAttributes
                property AXRecord : a reference to the ¬
                        contents of {«class usrf»:my AXList}
                property AXList : name of AXAttributes
        end script
        
        tell (a reference to the Object's AXList) to set the ¬
                contents to paragraphs of (it as text) & ""
        
        tell the Object to repeat with i from 1 to length of its AXValues
                set item (i * 2) of its AXList to ¬
                        item i of its AXValues
        end repeat
        
        tell application id "com.apple.SystemEvents" to return ¬
                {UI element:the Object's contents} & the properties ¬
                of the Object & (the Object's AXRecord as any) & ¬
                {_AXActions:every action's name of the Object}
end info

要使用它,请将特定 UI element 的引用传递给它,例如由click或者click at命令。在您的情况下,这可能是:

tell application id "com.apple.systemevents" to tell process "Umsatz" 
        return info about window 1's sheet 1's text field 1's ui element 1

它仅适用于单个元素,因此将元素集合传递给它会引发错误。成功后,在运行以下命令时,您将在脚本编辑器中获得类似返回的有关我的窗口的详细信息的结果:

tell application id "com.apple.systemevents" to tell process ¬
        "Script Editor" to return my info about window 1

Result:
        {UI element:window "Untitled 161.scpt" of application 
        process "Script Editor" of application "System Events", 
        minimum value:missing value, orientation:missing value, 
        position:{1000, 25}, class:window, accessibility 
        description:missing value, role description:"standard 
        window", focused:false, title:"Untitled 161.scpt", 
        size:{623, 1095}, help:missing value, entire contents:{}, 
        enabled:missing value, maximum value:missing value, 
        role:"AXWindow", value:missing value, 
        subrole:"AXStandardWindow", selected:missing value, 
        name:"Untitled 161.scpt", description:"standard window", 
        AXFocused:false, AXFullScreen:false, AXTitle:"Untitled 
        161.scpt", AXChildrenInNavigationOrder:{application "System 
        Events", application "System Events", application "System 
        Events", application "System Events", application "System 
        Events", application "System Events", application "System 
        Events", application "System Events", application "System 
        Events", application "System Events", application "System 
        Events"}, AXFrame:{1000, 25, 1623, 1120}, AXPosition:{1000, 
        25}, AXGrowArea:missing value, AXMinimizeButton:button 3 of 
        window "Untitled 161.scpt" of application process "Script 
        Editor" of application "System Events", 
        AXDocument:"file:///Users/CK/Library/Mobile%20Documents/com~
        apple~ScriptEditor2/Documents/Untitled%20161.scpt", 
        AXSections:{{SectionUniqueID:"AXToolbar", 
        SectionDescription:"Toolbar"}, 
        {SectionUniqueID:"AXContent", 
        SectionDescription:"Content"}, 
        {SectionUniqueID:"AXTopLevelNavigator", 
        SectionDescription:"Top Level Navigator"}}, 
        AXCloseButton:button 1 of window "Untitled 161.scpt" of 
        application process "Script Editor" of application "System 
        Events", AXMain:true, AXActivationPoint:{1010, 39}, 
        AXFullScreenButton:button 2 of window "Untitled 161.scpt" 
        of application process "Script Editor" of application 
        "System Events", AXProxy:missing value, 
        AXDefaultButton:missing value, AXMinimized:false, 
        AXChildren:{group 1 of window "Untitled 161.scpt" of 
        application process "Script Editor" of application "System 
        Events", busy indicator 1 of window "Untitled 161.scpt" of 
        application process "Script Editor" of application "System 
        Events", static text "Running…" of window "Untitled 
        161.scpt" of application process "Script Editor" of 
        application "System Events", radio group 1 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", splitter group 1 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", toolbar 1 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", tab group "tab bar" of 
        window "Untitled 161.scpt" of application process "Script 
        Editor" of application "System Events", button 1 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", button 2 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", button 3 of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", static text "Untitled 
        161.scpt" of window "Untitled 161.scpt" of application 
        process "Script Editor" of application "System Events"}, 
        AXRole:"AXWindow", AXParent:application process "Script 
        Editor" of application "System Events", 
        AXTitleUIElement:static text "Untitled 161.scpt" of window 
        "Untitled 161.scpt" of application process "Script Editor" 
        of application "System Events", AXCancelButton:missing 
        value, AXModal:false, AXSubrole:"AXStandardWindow", 
        AXZoomButton:button 2 of window "Untitled 161.scpt" of 
        application process "Script Editor" of application "System 
        Events", AXRoleDescription:"standard window", AXSize:{623, 
        1095}, AXToolbarButton:missing value, 
        AXIdentifier:"_NS:794", _AXActions:{"AXRaise"}}

【讨论】:

    【解决方案2】:

    重点 UI 元素前窗的可能不止1个。也就是说,它可能是聚焦的UI元素列表。

    在这里,我在我们的 Stack Overflow 站点的带有登录窗口的 Safari 中编写并测试了脚本。要像我一样进行测试,请不要忘记在某些表单字段中粘贴以使其集中。例如,在“电子邮件”文本字段中粘贴,然后运行脚本。

    在获取所有焦点 UI 元素的列表(使用此脚本)后,您可以按类、描述、名称等过滤您需要的焦点 UI 元素。

    set focusedElements to {}
    
    tell application "System Events" to tell process "Safari"
        set frontmost to true
        delay 1
        tell window 1 to set entireContents to entire contents
        repeat with i from 1 to count entireContents
            set UIElement to item i of entireContents
            try
                if focused of UIElement then set end of focusedElements to (contents of UIElement)
            end try
        end repeat
    end tell
    
    return focusedElements
    

    【讨论】:

      猜你喜欢
      • 2017-10-07
      • 2014-02-22
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2016-11-27
      相关资源
      最近更新 更多