我正在为我的 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/