单击 window 上的特定位置,单击特定按钮并控制 window 是否更改

Click on a specific place on a window, click on a specific button and control if the window changes

实际上我有 3 个关于同一个问题的问题:用 applescript 控制 window。

  1. 如果我要按应用程序 "Google Chrome" 的按钮 "Close Window",我该怎么办?
  2. 是否可以检查 window 是否发生变化?比如看是否出现弹窗之类的...
  3. 点击特定位置进入 window 怎么样?我的意思是,我知道我可以使用
    tell application "System Events"
        click at {x,y}
    end tell

但此命令使用整个屏幕作为参考系统,我希望它仅适用于特定 window。例如,如果在“{x,y}”处输入“{1,1}”,applescript 将点击菜单栏上的第一项。有没有一种方法可以让 "System Events" 单击“{1,1}”,但在 window "Google Chrome"?

在 applescript GUI 脚本中,您可以简单地通过名称或索引引用元素并告诉它单击或执行操作。例如,要在 Chrome 中第一次打开 window 时单击关闭按钮,您可以使用:

tell application "System Events"
    tell process "Google Chrome"
        tell window 1
            tell button 1
                click
            end tell
        end tell
    end tell
end tell

你实际上不需要知道它的物理位置来点击它;您只需要知道 window 中的第一个按钮是关闭按钮即可。

系统事件总是 returns 任何元素在屏幕像素中的位置,所以如果你想根据其 window 获取元素的位置,获取window 的位置,然后做一些加减(例如,如果你想点击一个 window 中的 {5,5},它的位置是 {100, 125},点击 {105, 130})

AppleScript 并非真正设计用于监视 GUI 更改,但如果您想变得棘手并且知道您正在寻找什么更改,您可以执行以下操作:

tell application "System Events"
    tell process "..."
        tell window 1's pop up button 3
            repeat until (exists menu 1)
                delay 0.2
            end repeat
            -- menu 1 now exists, so the pop up button is open
        end tell
    end tell
end tell

...但请注意,这会挂起脚本,直到菜单打开。一种更优雅的处理方式是编写一个带有空闲处理程序的脚本应用程序,如下所示:

on run
    -- whatever initialization is needed
end run

on idle
    tell application "System Events"
        try
            tell process "..."
                tell window 1's pop up button 3
                    if exists menu 1 then
                        -- menu 1 now exists
                        -- the pop up button is open
                        -- do what must be done
                    end if
                end tell
            end tell
        on error errstr
            display alert "Something went wrong" message "The script sent this error: " & errstr
        end try
    end tell
    return 0.2
end idle

您可以让 运行 在后台观察 GUI 中的特定变化('try' 语句是为了防止您正在观察的应用程序退出,window 关闭,或者 GUI 发生意外情况)。

如果您还没有,请在脚本编辑器中打开系统事件脚本定义并查看流程套件。这将向您展示使用 GUI 脚本可以做的所有事情。

这里有三个例子说明如何使用AppleScript:

注意:以下假设 Google Chrome 是 运行 至少有一个 window 在您测试每个 示例 AppleScript 代码 脚本编辑器.

示例一是最直接的方式:

tell application "Google Chrome" to close front window

例子二直接点击关闭按钮:

tell application "System Events" to tell ¬
    application process "Google Chrome" to ¬
    click button 1 of front window

示例三计算 关闭按钮 的中心并单击那里:

activate application "Google Chrome"

delay 0.5

tell application "System Events" to tell ¬
    application process "Google Chrome" to tell ¬
    front window

    set posB1 to (position of button 1)
    set szB1 to (size of button 1)

    set x to (item 1 of posB1) + (item 1 of szB1) / 2 as integer
    set y to (item 2 of posB1) + (item 2 of szB1) / 2 as integer

end tell

tell application "System Events" to click at {x, y}

注意前两个例子中,GoogleChrome的前面window甚至不需要最前面window 在桌面上;然而,对于第三个例子,它确实如此,否则 click at {x, y} 将不会到达预期的 target.

就是说,如果示例三可以像示例一那样以直接的方式完成工作,那么真的不应该使用示例三。示例 3 只是一个概念验证,用于获取要点击的坐标。此方法在某些边缘情况下可能很有用,尤其是在不直接支持 AppleScript.

的应用程序中

注意:示例 AppleScript 代码 就是这样,不包含任何 错误 适当处理。用户有责任根据需要或需要添加任何 错误处理 。看看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.