使用 Apple Remote 为 Play/Pause Soundcloud 选项卡编写脚本

Script to Play/Pause Soundcloud tab with Apple Remote

我正在为我的 Apple Remote(通过 BetterTouchTool)开发 AppleScript,它将 Play/Pause 在 Soundcloud 上播放。

它的工作方式应该类似于 iTunes 的原生 Apple Remote ▶/❚❚ 按钮。在SoundCloud上,keystroke space()是Play/Pause回放;

到目前为止,我已经能够让它打开一个新标签并开始播放,或者如果已经存在 select 该标签 – 但它不会 play/pause – 也不如果 Soundcloud 选项卡已经处于活动状态。

tell application "Google Chrome"
    repeat with w in windows
        set i to 1
        repeat with t in tabs of w
            if URL of t starts with "https://soundcloud.com" then
                set active tab index of w to i
                set index of w to 1
                return
            end if
            set i to i + 1
        end repeat
    end repeat
    open location "https://soundcloud.com/you/likes"
    activate
    delay 3
    tell application "System Events" to keystroke space
end tell

我是 AppleScript 的新手,所以不幸的是我能做的最好的事情就是试验。感谢您的帮助。

脚本必须退出循环,脚本需要一个if条件,像这样:

set soundcloudTab to false
tell application "Google Chrome"
    repeat with w in windows
        set i to 1
        repeat with t in tabs of w
            if URL of t starts with "https://soundcloud.com" then
                set active tab index of w to i
                set index of w to 1
                set soundcloudTab to true
                exit repeat
            end if
            set i to i + 1
        end repeat
        if soundcloudTab then exit repeat -- exit windows loop
    end repeat
    activate
    if not soundcloudTab then -- no Soundcloud tab exist
        open location "https://soundcloud.com/you/likes"
        delay 3
    end if
end tell
tell application "System Events" to keystroke space

另一个解决方案是使用JavaScript点击playPause按钮(不需要激活选项卡和“Google Chrome" 不需要在前台):

set soundcloudTab to missing value
tell application "Google Chrome"
    repeat with w in windows
        tell (first tab of w whose URL starts with "https://soundcloud.com") to if exists then
            set soundcloudTab to it
            exit repeat
        end if
    end repeat
    if soundcloudTab is missing value then
        open location "https://soundcloud.com/you/likes"
        delay 3
        set soundcloudTab to active tab of front window
    end if
    tell soundcloudTab to execute javascript "document.getElementsByClassName('playControls__playPauseSkip')[0].getElementsByTagName('button')[1].click()"
end tell

如果有人遇到这个问题 post 也想用他们的 Apple Remote 控制 Soundcloud,方法如下:

假设您已经有 BetterTouchTool,您可以为 Play/Pause 添加触发器到 Apple Remote 选项卡, 下一个, 上一个 曲目:

对于 Play,将 运行 Apple Script 指定为 Predefined Action。 Copy/Paste @jackjr300JavaScript 解决方案并点击保存。

对于下一个和上一个,粘贴相同的 JavaScript,但只需将 .getElementsByTagName('button')[1] 更改为:

  • .getElementsByTagName('button')[0]上一个
  • .getElementsByTagName('button')[2] for 下一个.

基于以上所述,我想出了一个更短、更不脆弱的解决方案。在我的用例中很难假设您已经打开了一个 Soundcloud 选项卡。这是主要例程:

on controlSoundcloud(cmd)
    tell application "Google Chrome"
        repeat with theTab in every tab in every window
            if URL of theTab starts with "https://soundcloud.com" then
                execute theTab javascript "var el = document.querySelector('button.playControls__" & cmd & "'); el && el.click()"
                return
            end if
        end repeat
    end tell
end controlSoundcloud

这本身不会做任何事情。你仍然需要调用它。根据您要执行的操作,将其中一行添加到脚本末尾。

-- play/pause
controlSoundcloud("play")
-- prev
controlSoundcloud("prev")
-- next
controlSoundcloud("next")