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

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

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.

Can I get a keyboard shortcut to go to homepage of current website in chrome?

I'm looking for a way to set a keyboard shortcut to go to the current homepage of the website I'm browsing in Chrome at that time.
So for example I'm on "https://stackoverflow.com/questions/ask?wizard=1" right now. "Press shortcut" Url "https://stackoverflow.com/" loads.
Could be a chrome plugin or an Alfred workflow.
You can create an Alfred workflow and connect a Hotkey trigger to a Run NSAppleScript action adding the following code to the latter:
on alfred_script(q)
tell application "Google Chrome"
set theURL to URL of active tab of window 1
set AppleScript's text item delimiters to "/"
set theHomeURL to text item 1 of theURL & "//" & text item 3 of theURL
set URL of active tab of window 1 to theHomeURL
activate
end tell
end alfred_script
Don't forget to set a hotkey combination to the trigger.

Applescript to get the URL from Safari

I'm trying to get the URL from Safari, close the tab and open it in Chrome ( TUAW Post ) But I keep getting the error:
error "Safari got an error: Can’t get current tab." number -1728 from current tab
From the code line:
set theURL to URL of current tab of window 1
Any suggestions?
All the code:
property theURL : ""
tell application "Safari"
set t set theURL to URL of current tab of window 1
close current tab
end tell
tell application "Google Chrome"
set URL of active tab of window 1 to theURL
activate
end tell
It works for me...
tell application "Safari"
set theURL to URL of current tab of window 1
end tell
You have to show more code because the error is caused by something else. Let me ask you this, do you have the tell application Safari statement inside of another tell block? Maybe it's inside the tell google chrome block of code? If so remove it.
Thanks a lot guys! Here is the final code.
property theURL : ""
tell application "Safari"
set theURL to URL of current tab of window 1
end tell
tell application "Google Chrome"
tell window 1
make new tab with properties {URL:theURL}
end tell
activate
end tell

Hiding a Chromium window through AppleScript

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