如何在 AppleScript 中打开多个 Google Chrome windows 时查找特定选项卡

How to find a specific tab while multiple Google Chrome windows are open in AppleScript

to clickClassName(theClassName, elementnum)
    tell application "Google Chrome"
        set youtubeTab to (the first tab of window 2 whose URL contains "youtube")
    end tell
    tell application "Google Chrome"
        execute youtubeTab javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
    end tell
end clickClassName

clickClassName("ytp-play-button ytp-button", 0)

我在 MacBook 上打开了两个 windows。一个在 MacBook 本身上,另一个在外部显示器上。我想 play/pause YouTube 视频在外接显示器上播放,同时在我的 MacBook 上做笔记。上面的脚本只有在 MacBook 上的 windows 被激活时才有效,因为它通过 window 2.

专门指向另一个 window

我想我需要找到所有 windows 并以某种方式使用 repeat 运算符,这样我就可以 play/pause 视频,而不管哪个 window 被激活。但是我不知道如何处理。

感谢任何帮助!

PS。我已将此脚本添加为服务并分配了特定的组合键。因此,当我在 MacBook 上做笔记时,我可以快速 play/pause 视频。

下面的示例 AppleScript 代码将点击Play/Pause YouTube 视频中 Google 的任何(每个)标签 上的按钮ChromeURL 包含 youtube:

tell application "Google Chrome"
    set YouTubeTabs to a reference to ¬
        (tabs of windows whose URL contains "youtube")
    repeat with aTab in YouTubeTabs
        tell aTab to ¬
            execute javascript ¬
                "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
    end repeat
end tell

假设您在 Google 中只有一个 tab Chrome 有一个 YouTube 视频到 play/pause,这一定会为您做到。