有没有办法使用 AppleScript 在 Google Chrome 中暂停 YouTube 视频?

Is there a way to pause a YouTube video in Google Chrome using AppleScript?

我正在尝试的是键入 tell application "Google Chrome" to pause 并在 Google Chrome 中暂停音频或视频。它不会工作,因为它认为暂停是一个变量。

有没有什么方法可以在所有应用程序中暂停音频或视频,或者只是 Google Chrome?

以下 AppleScript 代码将在 Google Chrome 中的任何 window 的任何选项卡中暂停或播放 YouTube 视频的任何实例(无论是否可见)。

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

to pauseOrPlayYoutubeInChrome(theClassName, elementnum)
    tell application "Google Chrome"
        set youtubeTabsRef to a reference to (tabs of windows whose URL contains "youtube")
        repeat with youtubeTabs in youtubeTabsRef
            execute youtubeTabs javascript "document.getElementsByClassName('" & ¬
                theClassName & "')[" & elementnum & "].click();"
        end repeat
    end tell
end pauseOrPlayYoutubeInChrome

来自评论:

The following example AppleScript code will play/pause a YouTube video in the active tab of the front window of Google Chrome: tell application "Google Chrome" to tell active tab of front window to execute javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"

@user3439894 this one works perfectly, since I only want to pause the video in the active tab. Is there any way to check if the video is paused so it doesn't unpause a video? Also could you write this comment as an answer so I can accept it as the correct answer?

使用 JavaScript 可能有更简单的方法来确定 活动选项卡 是否front window in Google Chrome 有一个 YouTube 视频播放,但是,因为我不知道它的 code,这里有一种方法适合我。

以下 示例 AppleScript 代码,如下所示,已在 Script EditormacOS Catalina 10.15.7 和 macOS Big Sur 11.2.3 下使用 Google Chrome(版本 90.0.4430.93(官方构建)(x86_64))语言和地区设置在System Preferences 设置为 English (US) — Primary 并且为我工作没有问题1.

  • 1 假定在 系统偏好设置 > 安全和隐私 > 隐私 已根据需要 set/addressed。

示例 AppleScript 代码:

if not running of ¬
    application "Google Chrome" then return

tell application "Google Chrome"
    if (window count) = 0 then return
    set atx to the active tab index of its front window
end tell

tell application "System Events"
    --  # macOS Big Sur System Events bug issue.
    --  # If running macOS Big Sur, uncomment the next 
    --  # two lines if System Events reports an error.
    -- run
    -- delay 0.5
    tell application process "Chrome"
        if (the value of ¬
            attribute "AXTitle" of ¬
            radio button atx of ¬
            tab group 1 of ¬
            group 1 of ¬
            the front window) ¬
            does not end with "Audio playing" then return
    end tell
end tell

tell application "Google Chrome" to ¬
    tell the active tab of its front window to ¬
        execute javascript ¬
            "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"

备注:

如编码所示,execute javascript 仅当 YouTube 视频在 活动选项卡 中播放时才会发生 front window of Google Chrome,从而暂停它。 代码,作为编码,不能导致暂停的视频播放,无论活动标签或任何标签 任意 window.

example AppleScript code 中的 -- # macOS Big Sur System Events bug issue. 部分是尝试macOS Big Sur 中的 System Events 问题的解决方法,因为完全相同的 code 没有它运行在 macOS Catalina 中没有问题,可能无法工作或需要调整。我仍在尝试解决一般问题。根据我的经验,该问题无法随意重现,因此如果问题继续存在且无法诊断,则可能必须使用替代方法。

某些错误可能部分归咎于使用非精确测试版本,尤其是任何涉及 分层 UI 元素结构 的更改的任何错误UI 使用 系统事件.

编写脚本 场景
  • Is there any way to either pause audio or video in all applications or just Google Chrome?

    • 通常按键盘上的 Play/Pause 应该会在 Google Chrome[=120 中暂停视频=],以及大多数其他应用程序。
  • It wouldn't work, since it thinks pause is a variable.

    • pause 不是 Google ChromeAppleScript 字典的一部分,为什么它显示为 变量.

GoogleChrome,运行AppleScriptcode 包含 JavaScript 需要从 Apple Events 菜单项中选中 Allow JavaScript:View > Developer -- 请注意,这曾经是默认设置,但有时会更改。更多信息:https://support.google.com/chrome/?p=applescript


注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟

我的解决方案包括在特定环境中 运行ning 2 AppleScript。

我使用 1 个 window 的 Brave 浏览器(我认为 google chrome 是一样的)在 youtube 上有 2 个标签。

第一个脚本强制打开 Brave 浏览器,第二个 运行 击键“K”

tell application "Brave Browser"
if it is not running then launch
activate
end tell


tell application "System Events"
keystroke "k"
end tell

我在 Automator 上创建了一个快速操作,然后我遵循了这个答案:https://apple.stackexchange.com/a/401262

并且在使用另一个应用程序时尝试将其作为键盘快捷键进行测试时,我收到了这样的错误“com.automator.runner.xpc 不允许发送击键”,所以我意识到我需要提供可访问性我希望脚本运行的所有应用程序的特权(关于安全和隐私)。

之后,它可以工作,但有时会出现相同的错误,并且在几分钟后再次工作(并且再也不会回来)所以我认为这也可以帮助别人。

这将在 Brave 浏览器的活动选项卡上暂停或播放 YouTube 视频。