使用Apple Remote播放/暂停Soundcloud选项卡的脚本

时间:2016-09-10 17:38:39

标签: javascript macos applescript soundcloud

我正在使用Apple Remote的AppleScript(通过BetterTouchTool),它将在Soundcloud上播放/暂停播放。

它应该与iTunes的原生Apple Remote ▶/❚❚按钮类似。在SoundCloud上,keystroke space播放/暂停播放;

  • 如果不存在Soundcloud选项卡,请打开Soundcloud选项卡并keystroke space)。
  • 如果存在一个Soundcloud选项卡,请选择它并keystroke space(如果已经播放,则,否则)。
  • 如果存在多个Soundcloud选项卡,请选择第一个选项卡keystroke space(如果选项卡正在播放,则选择 标签;如果没有播放标签,则为

到目前为止,我已经能够打开一个新选项卡并开始播放,或者如果已经存在,则选择该选项卡 - 但它不会播放/暂停 - 也不会显示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的新手,所以不幸的是我能做的最好的就是实验。感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

脚本必须退出循环,脚本需要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

答案 1 :(得分:1)

基于以上所述,我想出了一个更短,更脆弱的解决方案。在我的用例中,它假设您已经打开了一个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")

答案 2 :(得分:0)

如果有人绊倒这篇文章也想用他们的Apple Remote遥控器控制Soundcloud,请按照以下方式进行:

假设您已经拥有BetterTouchTool,则可以在播放/暂停下一步 Apple Remote 标签中添加触发器, 上一页曲目:

enter image description here

对于播放,将运行Apple脚本指定为预定义操作。复制/粘贴 @ jackjr300 JavaScript解决方案并点击“保存”。

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

  • .getElementsByTagName('button')[0]代表上一页
  • .getElementsByTagName('button')[2]代表下一步