【问题标题】:How to find a specific tab while multiple Google Chrome windows are open in AppleScript如何在 AppleScript 中打开多个 Google Chrome 窗口时查找特定选项卡
【发布时间】:2021-03-18 03:50:25
【问题描述】:
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 上打开了两个窗口。一个在 MacBook 上,另一个在外接显示器上。我想在我的 MacBook 上做笔记时播放/暂停在外部显示器上播放的 YouTube 视频。上述脚本仅在 MacBook 上的窗口被激活时才有效,因为它通过window 2 专门指向另一个窗口。

我想我需要找到所有窗口并以某种方式使用repeat 运算符,以便无论激活哪个窗口,我都可以播放/暂停视频。但我不知道如何接近。

感谢任何帮助!

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

【问题讨论】:

  • 我正在观看大量的讲座并且有完全相同的问题。你能利用下面的脚本吗?你有没有想过如何倒带视频?

标签: windows macos youtube tabs applescript


【解决方案1】:

以下示例 AppleScript 代码将点击YouTube上的播放/暂停按钮/strong> Google Chrome 的任何(每个)tab 中的视频,其 URL 包含 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 Chrome 中只有一个 标签 有一个 YouTube 视频可供播放/暂停,这肯定会帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-21
    • 2011-07-29
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多