How to open an new Safari window from Terminal in the current Space?

Now when using the open http://example.com command it opened the URL as last tab on my first Safari window.

I'm looking for a way to open:

  • New Safari window (regardless how many I have opened).
  • In the current Space
  • From Terminal with the provided URL.

It probably will need some osascript script, but my AppleScript knowledge is nearly zero...

The bonus could be open two URLs, in two tabs, in a new window in the current Space.

Could anyone help?

2 Answers 2


Here is an AppleScript that should help you. Open AppleScript Editor and save this as a script. I have modified the source that I found here to support taking arguments on the command line.

Use it like this:

osascript new_window.scpt http://www.google.com http://www.stackoverflow.com

Of course, replace the URLs above with your own URLs.


on run argv
    tell application "Safari"
        if (count argv) = 0 then
            -- If you dont want to open a new window for an empty list, replace the
            -- following line with just "return"
            set {first_url, rest_urls} to {"", {}}
            -- `item 1 of ...` gets the first item of a list, `rest of ...` gets
            -- everything after the first item of a list.  We treat the two
            -- differently because the first item must be placed in a new window, but
            -- everything else must be placed in a new tab.
            set {first_url, rest_urls} to {item 1 of argv, the rest of argv}
        end if

        make new document at end of documents with properties {URL:first_url}
        tell window 1
            repeat with the_url in rest_urls
                make new tab at end of tabs with properties {URL:the_url}
            end repeat
        end tell
    end tell
end run

You could even create an alias for this in Terminal and be able to use it easier. I would add the following to ~/.bash_profile:

alias newwindow='osascript /path/to/new_window.scpt'

Call newwindow whatever you want. Save .bash_profile and restart Terminal for it to work.

In case anyone is looking for a similar solution for Google Chrome, here is a different take on the same idea.


on run argv
    tell application "Google Chrome"
        if (count argv) = 0 then
            make new window
            tell (make new window)
                set URL of active tab to item 1 of argv
                repeat with the_url in the rest of argv
                    open location the_url
                end repeat
            end tell
        end if
        set active tab index of first window to 1
    end tell
end run
  • ABSOLUTELY marvelous. I simple created a script called Safari in my $PATH with a shebang #!/usr/bin/osascript and works like a charm. Thank you!
    – clt60
    Commented Aug 12, 2014 at 5:31
  • 1
    Didn't know you could do that with #!/usr/bin/osascript! Glad to help (and thanks for the new knowledge!)
    – aglasser
    Commented Aug 12, 2014 at 15:05
  • thx bro, amazing
    – Tim
    Commented Dec 18, 2020 at 7:16

What about this answer by user markhunte.

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


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

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


In terminal:

cd to your home directory.


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.

  • Thank you too, I more like vim for editing. ;) Accepting aglasser's answer because solved the two url problem.
    – clt60
    Commented Aug 12, 2014 at 5:42

You must log in to answer this question.

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