如何在 AppleScript 中单击按钮? [英] How to click button in AppleScript?

查看:25
本文介绍了如何在 AppleScript 中单击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写 AppleScript 以打开 Image Capture,然后单击全部导入"按钮.

Writing an AppleScript to open Image Capture and click the Import All button.

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

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

Image Capture opens but the script throws an error message saying it couldn't find the button "Import All".

已遵循其他线程的建议,了解如何检查辅助功能检查器中的位置以及如何将其转换为 AppleScript 指令.

Have followed advice on other threads on how to check the location in Accessibility Inspector and how to translate that to AppleScript instructions.

缺少什么?

推荐答案

要获取按钮和组号,您有两种方法:使用 Apple 在开发人员工具包Accessibility Inspector"中提供的实用程序;或使用小脚本自己查找数字.

To get the button and group numbers, you have 2 ways: use the utility aplication provided by Apple in the developper toolkit "Accessibility Inspector" or use small scripts to find the number yourselves.

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

I prefer using script method. it is a bit longer sometime, but it always works. Just write a script with instruction get UI elements. Here is a small example of such script:

-- 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 元素的窗口/应用程序.激活此脚本并运行.

Copy this script in Script Editor. Make active the window/application you want to get UI elements. Make this script active and run.

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

The result is sometime not easy to interpret because developper of the application/window you're looking for may not have use UI element clear names or titles which describe what they are. Then you will have to look their relative position in the window.

这篇关于如何在 AppleScript 中单击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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