【问题标题】:How to click button in AppleScript?如何单击 AppleScript 中的按钮?
【发布时间】:2015-12-17 03:52:19
【问题描述】:

编写 AppleScript 以打开 Image Capture 并单击 Import All 按钮。

tell application "Image Capture"
    activate
        tell application "System Events"
            tell process "Image Capture"
                click button "Import All" of group 1 of splitter group 1 of window 1
            end tell
        end tell
end tell

图像捕获打开,但脚本抛出一条错误消息,指出找不到“全部导入”按钮。

已遵循其他线程上关于如何在 Accessibility Inspector 中检查位置以及如何将其转换为 AppleScript 指令的建议。

缺少什么?

【问题讨论】:

    标签: applescript buttonclick image-capture


    【解决方案1】:

    对于图像捕获版本 6.6,“全部导入”按钮是“窗口 1 的拆分器组 1 的组 2 的按钮 3”。另外,我更喜欢使用按钮编号而不是按钮名称来确保脚本适用于任何语言。

    tell application "Image Capture"
    activate
        tell application "System Events"
            tell process "Image Capture"
                click button 3 of group 2 of group 1 of splitter group 1 of window 1
            end tell
        end tell
    end tell
    

    请注意,Apple 对 Image Capture 所做的任何后续更改都会影响您的脚本。

    【讨论】:

    • 谢谢 pbell,这行得通。也感谢有关使用按钮编号的解释和建议。
    • 你从哪里知道所有这些按钮和组号?
    【解决方案2】:

    要获取按钮和组编号,您有两种方法:使用 Apple 在开发人员工具包“Accessibility Inspector”中提供的实用程序或使用小脚本自行查找编号。

    我更喜欢使用脚本方法。有时会更长一些,但它总是有效的。只需编写一个带有指令的脚本即可获取 UI 元素。这是此类脚本的一个小示例:

    -- return lis of UI elements of active application and paste result in Excel
    property tab : ASCII character 9
    global T
    -- to find last active application
    tell application "System Events"
        set frontmostProcess to first process where it is frontmost
        set visible of frontmostProcess to false
        repeat while (frontmostProcess is frontmost)
            delay 0.2
        end repeat
        set secondFrontmost to name of first process where it is frontmost
        set frontmost of frontmostProcess to true
    end tell
    set ActifProcess to secondFrontmost as text
    
    tell application ActifProcess to activate -- set active the last actived application
    delay 1
    
    -- recursive loop to list all elements of active window
    tell application "System Events" to tell process ActifProcess to set myWindow to front window
    set T to ""
    getUI(myWindow, 1)
    set the clipboard to T
    display dialog "Result is in clipboard. paste in Excel or text document"
    
    
    
    on getUI(UIObjet, myLevel) -- get recursively the list of UI elements
        set AppleScript's text item delimiters to {"//"}
        tell application "System Events" to set UIliste to UI elements of UIObjet
        repeat with oneUI in UIliste
            tell application "System Events"
                set UItext to ("Level=" & myLevel & tab & "Name=" & (name of oneUI) & tab & (description of oneUI) & tab & "Class=" & (class of oneUI) as string) & tab & "Title=" & (title of oneUI) as string
                set UItext to (UItext & tab & "Value=" & (value of oneUI) as string) & tab
                try
                    set UItext to (UItext & "Position=" & (position of oneUI) as text)
                end try
                set UItext to UItext & return
                try
                    set NbSub to count of UI elements of oneUI
                on error
                    set NbSub to 0
                end try
                set T to T & return & UItext
            end tell
            if NbSub > 0 then
                getUI(oneUI, myLevel + 1) -- there are other sub UI elements, get them
            end if
        end repeat
    end getUI
    

    在脚本编辑器中复制此脚本。激活您想要获取 UI 元素的窗口/应用程序。激活此脚本并运行。

    结果有时不容易解释,因为您正在寻找的应用程序/窗口的开发人员可能没有使用 UI 元素明确的名称或标题来描述它们是什么。然后你必须在窗口中查看它们的相对位置。

    【讨论】:

      猜你喜欢
      • 2013-03-11
      • 2011-09-04
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2014-02-27
      相关资源
      最近更新 更多