Applescript to get the URL from Safari - google-chrome

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

Related

Opening Chrome from Automator with unicode in URL fails

I'm using Automator to open a link in Chrome based on on some selected text:
on run {input, parameters}
set kanji to input as string
set link to "https://jisho.org/search/" & kanji
tell application "Google Chrome"
open location link
end tell
end run
This works fine when the input text is ASCII, and a new Chrome tab opens at the URL, but nothing happens (and no error when put in a "try" block) when the input text is kanji (eg 漢字). Open "Safari" always works correctly, so this looks like a Chrome thing?
Chrome 91.0.4472.164, macOS 11.3.
Thanks for any insights!
Update: Thanks row and gurkensaas.
I ended up with this working version:
on run {input, parameters}
set kanji to input as string
set link to "https://jisho.org/search/" & kanji
try
tell application "Google Chrome"
tell its window 1
set URL of (make new tab) to link
end tell
end tell
on error errMsg number errorNumber
display dialog "Error occurred: " & errorNumber as text
end try
end run

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.

Miniaturized Chrome by AppleScript but failed

I have tried to use set "visible" = false
and
tell application "Google Chrome"
set miniaturized of every window to true
end tell
Both methods work for Safari, but not for Chrome. How do I fix this?
You're using the correct AppleScript terms for Safari, but the terms are different for Chrome (minimized instead of miniaturized). You can inspect the AppleScript dictionary using your script editor to find the correct terms. Or you can, as I did, do
tell application "Google Chrome"
properties of window 1
end tell
to return the various properties of a window to give you clues. An example of working AppleScript code is below.
tell application "Google Chrome"
set minimized of every window to true
end tell

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