jebrii
jebrii

Reputation: 165

using Mac OSX "open" command to open URL with -n flag

I'm having trouble getting /usr/bin/open to open a browser to a specific URL when using the -n flag.

For example, this will work:

open -a "Google Chrome" "https://stackoverflow.com"

...successfully opening the desired page. However, if my browser is already open, it will open it in an existing window as a tab. The man page for open says this should be remedied with the -n flag, but:

open -n -a "Google Chrome" "https://stackoverflow.com"

...opens a new instance (window) of my browser to the homepage without navigating to the desired URL.

I've also tried moving around the -n flag in the command and messing with other flags (such as -F).

environment: macOS Sierra 10.12.6

UPDATE: Here are some other solutions I've tried...

I've tried adding --args before the URL. This makes it behave like the first command above, essentially ignoring the -n flag....

open -n -a "Google Chrome" --args "https://stackoverflow.com"

...will open the desired URL, but again in a tab and not a new window.

I've tried making '--new-window' an option for --args:

open -a "Google Chrome" --args '--new-window' "https://stackoverflow.com"

...this behaves the same way as the -n flag, opening a new window but not passing it a URL.

I've also tried putting the URL before the --args:

open -n -a "Google Chrome" "https://stackoverflow.com" --args '--new-window'

...this behaves as if I did not put the --args flag, opening the URL in another tab.

Upvotes: 6

Views: 20567

Answers (3)

Yaakov Bressler
Yaakov Bressler

Reputation: 12018

The -n command will start a new instance for your browser – by default, it will open in a new tab in your browser. Do so with the following command:

open -n https://stackoverflow.com

If you want to open in a new window, you must specify so (but must also specify which browser):

open -n -a Google\ Chrome https://stackoverflow.com

Note: for those new to bash commands: the backslash in Google\ Chrome is an escaped space --> spaces aren't recognized in bash commands (they're used to identify arguments in commands...).

Upvotes: 0

jebrii
jebrii

Reputation: 165

After much trial and error, I've found that this is indeed possible and that it can be done in two ways:

...passing the -n flag to open and the --new-window arg to Google Chrome:

open -n -a "Google Chrome" --args '--new-window' "https://stackoverflow.com"

...ordering the parameters correctly such that the --args flag comes after the URL param (note that the -n flag passed to open will break this):

open -a "Google Chrome" "https://stackoverflow.com" --args '--new-window'

These open a new window with the specified URL.

Upvotes: 4

Vasan
Vasan

Reputation: 4956

This works on my OSX El Capitan. The --new-window is a chrome argument, a list of which can be viewed here

open -n -a "Google Chrome" --args "--new-window" "https://stackoverflow.com"

Upvotes: 15

Related Questions