Open a web page in Google Chrome without activating it

Sometimes I want to open a web page in Google Chrome, but without switching to the browser. I have an applescript command (osascript -e 'tell application "Google Chrome" to open location "URL"') to open a web page, but it activates the browser. Is there a way to open a web page in the browser without activating it?

The script below opens the link with no errors but activates Google Chrome. I'm not sure why is that the case.

tell application "Google Chrome"
    open location "chrome://settings" without activating
end tell

Solution 1:

There may have been a time when Google Chrome was compliant with AppleScript commands e.g. run or launch, which would keep it in the background, as it does with e.g. Safari and other applications. Or compliant with with the shell open command using either the -g or -j options along with the -a option.

In testing under macOS Catalina I could not get Google Chrome to consistently open in the background and only with the open command with -jga would it open in the background a few different times, but not at all consistently.

Google clearly is not conforming to the macOS norms!

That said, the only way I could consistently open a URL in Google Chrome in the background was is if it was already running with an open window in the background.

I'll post the example AppleScript code just for you to test if it works for you as it did for me.

set myURL to "https://www.example.com"

if running of application "Google Chrome" then
    tell application "Google Chrome"
        if window 1 exists then
            tell window 1
                make new tab
                repeat while (loading of active tab)
                    delay 0.1
                end repeat
                execute active tab javascript "document.location.href = '" & myURL & "';"
            end tell
        end if
    end tell
end if