Hiding a Chromium window through AppleScript - google-chrome

I recognise that Google Chrome & Chromium aren't highly AppleScript enabled yet. But, I was wondering if there was a way to use the "System Events" to hide a particular window or tab?
Here is what I have so far ...
tell application "System Events"
tell process "Google Chrome"
repeat with theWindow in windows
set thePageName to title of theWindow
if thePageName contains "ABC" then
-- HIDE theWindow command here
end if
end repeat
end tell
end tell
I can access the window I wish to hide but cannot find the command to actually hide it.
Furthermore, if there is a way to repeat through the tabs within the window, that would be even better.
Thanks

System events can type keyboard commands for you. So look through the menu items of the application and see if there are any keyboard shortcuts to do what you want. For example, every application should have a "Window" menu. In the Window menu is a "Minimize" command with the keyboard shortcut "cmd-m". So you can use that shortcut to hide your windows. Just replace "-- HIDE theWindow command here" with...
keystroke "m" using command down
One other thing. For this to work you must make sure the application is frontmost before doing this so add the following to the beginning of your script.
tell application "Google Chrome" to activate

Related

Is there a way to mute a tab in Google Chrome using AppleScript?

So I wanted to make a little script that e.g. mutes all tabs whose URL contains google.com. I wanted to do something like this:
tell application "Google Chrome"
set theWindows to windows of application "Google Chrome"
repeat with theWindow in theWindows
set theTabs to (tabs of theWindow whose URL contains "google.com")
repeat with theTab in theTabs
tell theTab
mute
end tell
end repeat
end repeat
end tell
But if you try to run this, you will soon notice that Applescript thinks mute is a variable. I also tried mute tab but it also doesn't work.
This should allow for the muting of tabs:
tell application "Google Chrome" to activate
tell application "System Events" to ¬
click ¬
menu item "Mute Site" of ¬
menu 1 of ¬
menu bar item "Tab" of ¬
menu bar 1 of ¬
application process "Chrome"
There is no default keyboard shortcut assigned for the Tab Menu Item “Mute Site” in Google Chrome. However, you can assign it a custom keyboard shortcut and use it manually or in an AppleScript with a keystroke or key code command.
This following image shows how I assigned that custom keyboard shortcut.
Once your custom keyboard shortcut is properly set up, this following AppleScript code is an example of how I personally have mine set up.
tell application "Google Chrome" to activate
delay 0.1
tell application "System Events"
key code 49 using {shift down, command down}
end tell

Target a Chrome Extension using AppleScript

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.
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.

How to use applescript to hit enter on system printing dialog box?

I want to print chrome windows automatically (no dialogs) if they fit some sort of URL pattern (e.g. are not of a given set of URLs).
Can you use apple script for that? Could someone share an example? (I do not own a mac so I can't really experiment myself)
set i to 1
tell application "Google Chrome"
activate
tell window 1
repeat with t in tabs
--if title of t starts with "Example" then
if {"http://example.com/", "http://aa.com/"} does not contain URL of t then
set active tab index to i
tell t to print
delay 1
tell application "System Events"
click button "Print" of window 1 of process "Chrome"
--keystroke return
end tell
end if
set i to i + 1
end repeat
end tell
end tell

Taking full page screenshot with a Chrome extension

Is there away to capture a screenshot of the full page, including what is below the fold, in a Chrome Extension?
The captureVisibleTab seems to be limited to what is displayed within the visible area.
The standard approach seems to be to scroll around the page and capture screenshots at each part and then stick them all together. The official google screen capture plugin does this, but I found it to be buggy (at least on Mac OSX), so I wrote my own full page screen capture extension.
Source code here (relevant code in page.js and popup.js).
On Macs, while not a Chrome extension, you can use the following AppleScript to automate the process found here:
https://zapier.com/blog/full-page-screenshots-in-chrome/
tell application "Google Chrome" to activate
tell application "System Events"
keystroke "i" using {option down, command down}
delay 0.3
keystroke "p" using {shift down, command down}
delay 0.3
keystroke "Full"
delay 0.5
key code 76
end tell
Open ScriptEditor and paste that script in. Save it as a file wherever you need it locally. When you run it by pressing play in ScriptEditor, it will automatically save a full screenshot of the active tab to your Downloads folder.
Chrome 59 adds a new feature in DevTools called Capture full-size screenshot. But I don't know whether this API can be called by extensions.
You're limited to capturing the visible page via captureVisibleTab unless you use Flash or NPAPI.

How to trigger "Markup Validation" in Visual Web Developer 2010

After turning on the Error List, the MS Visual Web Developer 2010 can report warning/error in the panel automatically.
However, I would like to know what I can do to trigger this validation manually rather than wait for a unpredictable period until the application starts to check.
If you go to Tools > Settings on the menu and tick Expert Settings, the menus will now have a lot more options, one of which can be found in Edit > Advanced, Validate Document which should provide what you want.
If you want to assign this a keyboard shortcut, go to Tools > Options on the menu, tick Show all Settings in the bottom left of the Options window, then navigate through the tree on the left to Environment > Keyboard. Once you've got the Keyboard Settings showing, type Edit.ValidateDocument into the "Show commands containing" textbox, change the "Use new shortcut in" drop-down to HTML Editor Design View and then click in "Press shortcut keys" and enter the shortcut key you wish to use. I suggest Shift-Alt-V as it's not assigned by default in Visual Web Developer 2010. Finally click "Assign" and then "OK"