1

I'm trying to make an Automator Quick Action that will show up in the right click menu under Services that will take the highlighted text and put it in a new tab in Chrome.

Looking at various other posts, I attempted this AppleScript, but it doesn't work:

on run {input, parameters}
tell application "Google Chrome"
    set myTab to make new tab at end of tabs of window 1
    tell application "System Events"
        tell application process "Google Chrome"
            set value to input
            keystroke return
        end tell
    end tell
end tell
end run

I can get it to work for a URL, but I want this to work for non-URLs too (and just perform the default Chrome behavior with that text).

0

2 Answers 2

1

Update:

On the day this answer was originally posted, this worked with the then current version of Google Chrome.

As is typical with software updates they break code that worked in previous versions.

As of the date of this update and testing with the latest release of Google Chrome Version 90.0.4430.93 (Official Build) (x86_64), you need to add a toolbar 1 of ¬ in between the two group 1 of ¬ to update the code to work with this latest release of Google Chrome.



Original Answer

Not sure why you need this when you can select text, then right-click and select Search Google for “$selected_text” which essentially does the same thing as you're trying to do with a Quick Action service.

The following example AppleScript code does what you're looking to do:

on run {input, parameters}
    tell application "Google Chrome"
        activate
        if (count window) is equal to 0 then make new window
        make new tab at end of tabs of window 1
    end tell
    delay 0.5
    tell application "System Events"
        set value of ¬
            text field 1 of ¬
            group 1 of ¬
            group 1 of ¬
            window 1 of ¬
            application process "Google Chrome" to ¬
            input as text
        key code 36
    end tell
end run

Note that in order for this Quick Action service to work, Google Chrome and any other application you trigger the service from needs to be added to: System Preferences > Security & Privacy > Privacy > Accessibility

Additionally, Google Chrome needs to allow System Events under: System Preferences > Security & Privacy > Privacy > Automation

Automator Quick Action


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

8
  • Thanks, I’ll try this out soon. Regarding why I want this, I should have clarified I intent to use this menu item from other apps that don’t have a way to open a Google search from highlighted text. Commented Jul 5, 2020 at 20:28
  • @Liron Yahdav, After reading your comment, that changes things, and I'd code it differently. Are you using Google Chrome as your search engine in Google Chrome, or some other search engine? Also, is Google Chrome your default browser? Commented Jul 5, 2020 at 21:05
  • I just tried the AppleScript above and it almost works, except it triggers the enter key in the current app I have focused instead of in Chrome. I have Chrome as my default browser but I don't have Google as my search engine for the address bar. I'd like the script to use the search engine I have configured in Chrome, so your script would work correctly if it triggered the enter key in Chrome. Commented Jul 6, 2020 at 18:15
  • @Liron Yahdav, Which search engine are you using? Commented Jul 6, 2020 at 20:24
  • 1
    @Liron Yahdav, Testing with the latest, as of today, Google Chrome, Version 90.0.4430.93 (Official Build) (x86_64), you need to add a toolbar 1 of ¬ in between the two: group 1 of ¬ Commented Apr 29, 2021 at 17:37
0

I wanted to do something very similar, prepare a Google search with the currently selected text:

Create a quick action using Automator with two actions

  1. JavaScript action as
function run(input, parameters) {
    return `https://www.google.com?q=${encodeURIComponent(input)}`;
    
    // Or this if you want to actually do the search not just prepare
    // return `https://www.google.com/search?q=${encodeURIComponent(input)}`;
}
  1. and a Display webpages action which opens the given url with the default browser

automator quick action

If you want to assign a global shortcut, you can find the action under Settings > Keyboard > Shortcuts > Services > Text (because it receives text).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .