6

I decide to open a url in a new window with command:

$ open -na 'Google Chrome' 'https://www.etymonline.com/'

It opened a new window in blank.

  • If you need to open a url In an existing tab... I think it's not possible using bash directly. You can with applescript. You can trigger applescript from bash if needed. – Joonas Jan 17 '18 at 5:33
  • I've updated my answer to use an alternate solution that you might find useful, please have a look. – user3439894 Jan 17 '18 at 17:59
7

You need to use the --args option, e.g.:

open -na 'Google Chrome' --args --new-window 'https://www.etymonline.com/'

From the manual page for the open command:

--args
All remaining arguments are passed to the opened application in the argv parameter to main(). These arguments are not opened or interpreted by the open tool.


For more Google Chrome command line switches that could be added to the --args option of the open command, check out the following URL.

From: List of Chromium Command Line Switches

There are lots of command lines which can be used with the Google Chrome browser. Some change behavior of features, others are for debugging or experimenting. This page lists the available switches including their conditions and descriptions.


Another approach would be to use AppleScript from the command line, e.g:

osascript -e 'tell application "Google Chrome" to set URL of active tab of (make new window) to "https://www.etymonline.com/"'

For ease of use, you could wrap this in a function and place it in your ~/.bash_profile or ~/.profile file, e.g:

openurl () 
{ 
    if [ -n "$1" ]; then
        osascript -e "tell application \"Google Chrome\" to set URL of active tab of (make new window) to  \"$1\""
    else
        printf "\n  Missing URL...\n\n    Syntax: openurl 'URL'\n\n    Example: openurl 'http://www.google.com'\n\n"
    fi
}

Then in Terminal, simply use the following, e.g.:

openurl 'https://www.etymonline.com/'

If you execute openurl without an argument, you get the following output:

$ openurl

  Missing URL...

    Syntax: openurl 'URL'

    Example: openurl 'http://www.google.com'

$

Note: The URL is shown wrapped in single quotes and this to ensure no expansion takes place when the URL is passed to the function if it has any shell special characters in it. The single quotes can be omitted for basic/simple URLs.

This same approach can be applied to the open command as well. That said, the reason I'd choose to wrap the AppleScript in a function is at some point the open command used in this manner with Google Chrome may break. This is because when Google Chrome is already open, it's opening a second instance, which is then passed to the first instance and the second instance terminated. This behavior may become problematic as Goggle Chrome is updated, where the AppleScript in a function will not, as it's only talking to the first instance of Google Chrome.

  • It opened one in a new tab. – Algebra Jan 17 '18 at 5:02
  • @Yumi Tada, I've updated my answer with --new-window added to the --args option. It opens the URL in a new window and it worked for me. – user3439894 Jan 17 '18 at 6:10
1

The above answers work from the command line, but if you want Chrome to open things in a new window everywhere by default (and not just from the command-line) you have to change the http/https file handlers to a script that does what you want.

With a default handler set to open a new window, it will work from the command line or when you click a link in Mail or similar, and thus integrate much more nicely with Spaces since you'll get a new window on the current space with your desired link, instead of a new tab in some other Space that you have to hunt for.

Specifically you need a script like this:

on open location theURL
    tell application "/Applications/Google Chrome.app"
        make new window
        activate
        set URL of active tab of first window to theURL
    end tell
end open location

Then you save that from ScriptEditor as an app, run it to register it with LaunchServices, and edit the ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist file to have your app as the default for http/https.

You can find an integrated solution with the script as an app and (in a current comment) a PList editing script to tell the system to use your app here:

https://smoove-operator.blogspot.com/2011/06/open-links-from-external-applications.html

Inspiration from http://pepijndevos.nl/2010/05/open-external-links-in-running-browser/ and https://github.com/primalcurve/macsupportpub/blob/master/scripts/setGoogleChromeAsDefaultBrowser_Scrubbed.sh

You must log in to answer this question.

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