首先,我们在谷歌浏览器中打开一个空白的新标签。较少的 UI 元素有利于分析。然后我们运行下面的代码(XXX):
tell application "Google Chrome"
activate
delay 0.3
end tell
tell application "System Events"
tell front window of (first application process whose frontmost is true)
set uiElems to entire contents
end tell
end tell
在文本输出中搜索“screen”,我们看到这样一行:
“New”组“Extensions”组的“Full Page Screen Capture”按钮
Tab - Google Chrome”的窗口 “New Tab - Google Chrome”的
应用程序“谷歌浏览器”,
所以我们知道
按钮在组中(扩展),
组(Ext...)在组(新标签...)中,
组(新标签...)在窗口中...
所以我们测试下面的代码:
tell application "Google Chrome"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Google Chrome"
tell window "New Tab - Google Chrome"
tell group "New Tab - Google Chrome"
tell group "Extensions"
tell button "Full Page Screen Capture"
click
--perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
end tell
它有效。但是我们在代码中看到了活动标签“新标签 - 谷歌浏览器”的名称,所以如果我们转到另一个标签,代码就不起作用。
所以我们需要知道如何获取前台窗口的活动标签的名称。我做了很多测试。最后我找到了这个page的答案。
osascript -e 'tell app "google chrome" to get the title of the active tab of window 1'
然后我把它放到我的AppleScript中,测试下面的代码:
tell application "Google Chrome"
activate
delay 0.3
end tell
tell application "System Events"
tell process "Google Chrome"
set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
set theTitle to theTitle & " - Google Chrome"
--note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
tell window theTitle
tell group theTitle
tell group "Extensions"
tell button "Full Page Screen Capture"
--click
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
end tell
大多数时候,这个版本的代码可以工作,但有时它不起作用。我在此选项卡上运行代码(XXX),有趣的是,按钮名称从“全页屏幕捕获”更改为“全页屏幕捕获”
可以访问这个站点”注意它在“Capture”之后插入了一个“\r”。所以我们知道窗口的标题可能会改变,按钮名称可能会改变。所以我尝试制作一个处理程序。测试下面的代码:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Google Chrome"
activate
delay 0.3
end tell
try
set buttonName to "Full Page Screen Capture"
fullPageScreenshot(buttonName)
on error
set buttonName to "Full Page Screen Capture
Has access to this site"
--note here is a "\r"
fullPageScreenshot(buttonName)
end try
on fullPageScreenshot(buttonName)
tell application "System Events"
tell process "Google Chrome"
set theTitle to do shell script "osascript -e 'tell app \"google chrome\" to get the title of the active tab of window 1'" as text
delay 0.2
set theTitle to theTitle & " - Google Chrome"
--note the example title is "New Tab - Google Chrome". Don't forget to attach " - Google Chrome"
tell window theTitle
tell group theTitle
tell group "Extensions"
tell button buttonName
--click
perform action "AXPress"
end tell
end tell
end tell
end tell
end tell
end tell
end fullPageScreenshot
现在这个版本的代码,我测试了20多次,总是可以的。我不经常使用这个扩展。我的谷歌浏览器版本是 80.0.3987.149 (Official Build) (64-bit);和 MacOS 10.12.6。请让我知道此代码是否适用于您的机器。