如何在AppleScript中打开多个Google Chrome窗口时查找特定选项卡


to clickClassName(theClassName, elementnum)
tell application "Google Chrome"
set youtubeTab to (the first tab of window 2 whose URL contains "youtube")
end tell
tell application "Google Chrome"
execute youtubeTab javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end clickClassName
clickClassName("ytp-play-button ytp-button", 0)

我的MacBook打开了两扇窗户。一个在MacBook上,另一个在外部显示器上。我想在MacBook上做笔记的同时播放/暂停在外部显示器上播放的YouTube视频。只有当MacBook上的窗口被激活时,上面的脚本才能工作,因为它通过window 2专门指向另一个窗口。

我想我需要找到所有窗口,并以某种方式使用repeat运算符,这样无论哪个窗口被激活,我都可以播放/暂停视频。但我不知道该怎么办。

感谢您的帮助!

PS。我已经将此脚本添加为服务,并分配了一个特定的组合键。因此,当我在MacBook上做笔记时,我可以快速播放/暂停视频。

下面的示例AppleScript代码将在Google ChromeURL包含YouTube的任何(每个(选项卡中点击YouTube视频上的播放/暂停按钮:

tell application "Google Chrome"
set YouTubeTabs to a reference to ¬
(tabs of windows whose URL contains "youtube")
repeat with aTab in YouTubeTabs
tell aTab to ¬
execute javascript ¬
"document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
end repeat
end tell

假设你在谷歌浏览器中只有一个选项卡,它有一个YouTube视频来播放/暂停

最新更新