使用 AppleScript 定位 Chrome 扩展

Target a Chrome Extension using AppleScript

我 运行 遇到了一些障碍,我可以使用建议。作为我正在编写的脚本的一部分,我的目标是 Chrome 浏览器中的扩展程序来获取站点的整页屏幕截图。我已经能够使用辅助功能检查器识别该元素,但我不知道如何单击该元素。我已经尝试了几种附加代码的变体,但运气不佳。

tell application "System Events"
tell process "Google Chrome"
    tell group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
        click (first button where its accessibility description = "Full Page Screen Capture")
    end tell
end tell

结束讲述

以及

tell application "System Events"
delay 1
tell process "Google Chrome"
    click UI element "Full Page Screen Capture" of group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
end tell

结束讲述

此外:我可以针对扩展组中的其他元素(即 Window Resizer),但这个不想合作。

欢迎提出任何建议。提前谢谢你。

首先,我们在 Google Chrome 中打开一个空白的新标签页。少UI个元素有利于分析。然后我们 运行 下面的代码 (XXX):

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell front window of (first application process whose frontmost is true)
        set uiElems to entire contents
    end tell
end tell

在文本输出中搜索"screen",我们看到这样一行:

button "Full Page Screen Capture" of group "Extensions" of group "New Tab - Google Chrome" of window "New Tab - Google Chrome" of application process "Google Chrome",

所以我们知道

按钮在组(扩展)中,

群(分机...)在群(新标签...)中,

群组(新标签...)在window...

所以我们测试下面的代码:

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell process "Google Chrome"
        tell window "New Tab - Google Chrome"
            tell group "New Tab - Google Chrome"
                tell group "Extensions"
                    tell button "Full Page Screen Capture"
                        click
                        --perform action "AXPress"
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

有效。但是我们看到活动选项卡的名称 "New Tab - Google Chrome" 在代码中,所以如果我们转到另一个选项卡,代码将不起作用。 所以我们需要知道如何获取前面window的活动标签页的名称。我做了很多测试。最后我在这个 page.

上找到了答案
osascript -e 'tell app "google chrome" to get the title of the active tab of window 1'

然后我把它放在我的 AppleScript 中,测试下面的代码:

tell application "Google Chrome"
    activate
    delay 0.3
end tell

tell application "System Events"
    tell process "Google Chrome"
        set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
        set theTitle to theTitle & " - Google Chrome"
        --note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
        tell window theTitle
            tell group theTitle
                tell group "Extensions"
                    tell button "Full Page Screen Capture"
                        --click
                        perform action "AXPress"
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

大多数时候,这个版本的代码可以工作,但有时也行不通。我在这个选项卡 运行 代码(XXX),有趣的是,按钮名称从 "Full Page Screen Capture" 更改为“全页屏幕截图

可以访问此站点”请注意,它在 "Capture" 一词后插入了一个“\r”。因此我们知道 Window 的标题可能会更改,按钮名称可能会更改。所以我试着做一个处理程序。测试下面的代码:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Google Chrome"
    activate
    delay 0.3
end tell

try
    set buttonName to "Full Page Screen Capture"
    fullPageScreenshot(buttonName)
on error
    set buttonName to "Full Page Screen Capture
Has access to this site"
    --note here is a "\r"
    fullPageScreenshot(buttonName)
end try

on fullPageScreenshot(buttonName)
    tell application "System Events"
        tell process "Google Chrome"
            set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
            delay 0.2
            set theTitle to theTitle & " - Google Chrome"
            --note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
            tell window theTitle
                tell group theTitle
                    tell group "Extensions"
                        tell button buttonName
                            --click
                            perform action "AXPress"
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end fullPageScreenshot

现在这个版本代码,我测试了20多次,总是可以的。我不经常使用这个扩展。我的 Google Chrome 版本是 Version 80.0.3987.149 (Official Build) (64-bit);和 MacOS 10.12.6。请让我知道此代码是否适用于您的机器。