Reputation: 93
I've run into a bit of roadblock and I could use a suggestion. Part of a script that I'm writing, I'm targeting an extension in the Chrome brower to take a full page screen shot of a site. I've been able to identify the element using the Accessibility inspector but I can't figure out how to click on the element. I've tried several variants of the attached code without much luck.
tell application "System Events"
tell process "Google Chrome"
tell group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
click (first button where its accessibility description = "Full Page Screen Capture")
end tell
end tell
end tell
as well as
tell application "System Events"
delay 1
tell process "Google Chrome"
click UI element "Full Page Screen Capture" of group "Extensions" of group "BBC - Homepage - Google Chrome" of window "BBC - Homepage - Google Chrome"
end tell
end tell
Also: I can target other elements in the Extensions group (i.e. Window Resizer) but this one does not want to cooperate.
Any suggestion would be welcome. Thank you in advance.
Upvotes: 2
Views: 1363
Reputation: 173
Firstly, we open a blank new tab in Google Chrome. Less UI elements is good for analysis. then we run the code(XXX) below:
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
Search "screen" in the text output, we see line like this:
button "Full Page Screen Capture" of group "Extensions" of group "New Tab - Google Chrome" of window "New Tab - Google Chrome" of application process "Google Chrome",
So we know
the button is in group(Extensions),
the group(Ext...) is in group(New Tab...),
the group(New tab...)is in window...
So we test the code below:
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
It works. But we see the name of the active tab "New Tab - Google Chrome" is in the code, so if we go to another tab, the code doesn't work. So we need to know how to get the name of the active tab of the front window. I did a lot of test. Finally I find answer on this page.
osascript -e 'tell app "google chrome" to get the title of the active tab of window 1'
Then I put it in my AppleScript, test the code below:
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
Most of the time, this version of code works, BUT sometimes it doesn't work. I run code(XXX) at this tab, interesting, the button name changed from "Full Page Screen Capture" to "Full Page Screen Capture
Has access to this site" Note it inserts a "\r" after the word "Capture". So we know the Title of Window may change, and the button name may change. So I try to make a handler. Test the code below:
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
Now this version code, I test it more than 20 times, it always work. I do not use this extension too frequently. My Google Chrome version is Version 80.0.3987.149 (Official Build) (64-bit); and MacOS 10.12.6. Please let me know if this code works on your machine.
Upvotes: 2