Terminal command to open new Safari window

macos, objective-c, safari, shell, terminal

Solution

You can place the below function with the osascript code in your .profile

code:

function Safari {
  # Will open a New Safari window with argument 1.

osascript <<EOD
tell application "Safari" to make new document with properties {URL:"$1"}
return
EOD

}

In terminal:

- cd to your home directory.

- Run : nano .profile

If you already have a .profile file it will open and there will be some code there already.

- Enter the code into it below any other code.

If you did not have one, a blank .profile file will be opened.

- Enter the code into it.

------After entering the code ----

Hold down Ctrl and press the x key.

At the bottom of the window you will be asked to save the changes.

Hit the y key for yes.

You will now be asked to save it with the current name of .profile

Just hit the Enter key to save it.

Run :

  . ~/.profile

This will update the environment.

now you can run:

Safari  "http://stackoverflow.com"

Note the Cap "S" in Safari.

Problem

Opening a link in Firefox from Terminal is pretty straightforward and there are many options available to the `firefox` command. E.g you can do `firefox -new-window "http://stackoverflow.com"` to open the link in a new window. I wonder if you can do the same with Safari on a Mac? `open "http://stackoverflow.com"` just opens a new Tab if there's already a window of Safari opened. I know of apps that let you switch between "Open in new tab" and "Open in new window". Is there a special prefix to the URL to tell Safari to open a new window? Or is it just possible through Objective-C code?

Original source