AppleScript控制bilibili视频播放/暂停

脚本

对会创建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

创建步骤

  1. command + space 打开 spotlight 搜索 Automator
  2. Create a Quick Action在这里插入图片描述
  3. Run AppleScript 拖到右侧菜单中
    在这里插入图片描述
  4. 把开头的那段代码拷贝到代码框中,注意设置 Workflow receivesno input
    在这里插入图片描述
  5. 打开一个bilibili视频,点击上图中的运行按钮,看看是否能暂停/播放视频
  6. command + S 保存该 Quick Action,记住这个命名,后面会用
  7. 打开 System Settings... -> Keyboard -> Keyboard Shortcuts... -> Services -> General
    在这里插入图片描述
  8. 双击快捷键处,绑定快捷键(注意不要与其他快捷键冲突)

完活,Youtube的控制方式与Bilibili类似,把脚本中对应域名修改了就行

补充

这个脚本逻辑只是选择当前第一个bilibili界面执行javascript代码,如果快捷键不生效试试只保留当前播放界面。如果想更精细地控制请自行研究AppleScript和JavaScript相关内容,对我个人而言这个脚本已经够用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VelvetQuilt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值