脚本
对会创建Quick Action的小伙伴来说,有这个脚本就够了,先上代码:
tell application "System Events"
if (exists application process "Google Chrome") then
tell application "Google Chrome"
if (count of windows) > 0 then
set foundBilibiliTab to false
repeat with w from 1 to (count of windows)
repeat with t from 1 to (count of tabs of window w)
if URL of tab t of window w contains "bilibili.com" then
set foundBilibiliTab to true
exit repeat
end if
end repeat
if foundBilibiliTab then
exit repeat
end if
end repeat
if foundBilibiliTab then
tell tab t of window w
-- Bilibili的视频控制命令,这里假设它和YouTube相似
execute javascript "var video = document.querySelector('video'); if(video) { if(video.paused) { video.play(); } else { video.pause(); } }"
end tell
else
display notification "No Bilibili tab found in Google Chrome."
end if
else
display notification "No windows open in Google Chrome."
end if
end tell
else
display notification "Google Chrome is not running."
end if
end tell
创建步骤
command
+space
打开 spotlight 搜索Automator
- Create a
Quick Action
- 将
Run AppleScript
拖到右侧菜单中
- 把开头的那段代码拷贝到代码框中,注意设置
Workflow receives
为no input
- 打开一个bilibili视频,点击上图中的运行按钮,看看是否能暂停/播放视频
command
+S
保存该 Quick Action,记住这个命名,后面会用- 打开
System Settings...
->Keyboard
->Keyboard Shortcuts...
->Services
->General
- 双击快捷键处,绑定快捷键(注意不要与其他快捷键冲突)
完活,Youtube的控制方式与Bilibili类似,把脚本中对应域名修改了就行
补充
这个脚本逻辑只是选择当前第一个bilibili界面执行javascript代码,如果快捷键不生效试试只保留当前播放界面。如果想更精细地控制请自行研究AppleScript和JavaScript相关内容,对我个人而言这个脚本已经够用了。