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

标签 javascript macos applescript soundcloud

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

它的工作原理应该与 iTunes 的原生 Apple Remote ▶/❚❚ 按钮类似。在 SoundCloud 上,击键空间 () 是播放/暂停播放;

  • 如果不存在 Soundcloud 选项卡,请打开 Soundcloud 选项卡和击键空间 ()。
  • 如果存在一个 Soundcloud 选项卡,请选择它并击键空格(如果已经播放,则为 ,否则为 )。
  • 如果存在多个 Soundcloud 选项卡,请选择第一个和击键空格(如果选项卡正在播放,则为 选项卡(如果正在播放另一个选项卡),或者(如果没有正在播放的选项卡)。

到目前为止,我已经能够让它打开一个新选项卡并开始播放,或者如果一个选项卡已经存在,则选择该选项卡 - 但它不会播放/暂停 - 或者如果 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

关于javascript - 使用 Apple Remote 播放/暂停 Soundcloud 选项卡的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39428827/

相关文章:

javascript - 在 Polymer 2.0 中加载时动态附加子元素

swift - 水平和垂直嵌套 ScrollView

macos - AppleScript 如何在代码中获取 STDIN?

outlook - 在 Microsoft Outlook 中使用 applescript 创建新的外发邮件

email - 发送苹果邮件中的所有草稿

javascript - vue-router 转换突然停止工作

php - 客户端上的php或javascript可以链接到远程服务器数据库吗?

linux - 一种确定进程的 "real"内存使用情况的方法,即私有(private)脏 RSS?

macos - asdf erlang 在 macOS 上编译失败

javascript - sessionStorage不存储原始对象