Terminal command to open new tab in Safari

so heres my issue

while running command "open https://www.google.com" in terminal consecutive times. I have noticed the second time i run the command it just shows the original window of google.com instead of opening in a new tab. It works fine when its a different website being requested

is this a mac os bug? ive tried setting new tabs to always in safari but no luck

Im trying to get safari to ALWAYS open a new tab even if its the same website

Mac mini, macOS 11.5

Posted on Sep 10, 2021 4:40 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 10, 2021 7:19 AM

The Terminal "open" command does not control how you interact with Safari, and cannot direct Safari to create a new tab. However, it can be done with a short Zsh (or Bash) script assigned to a shell as a function, alias, or in this example, a Zsh function that is available to you when you launch the Terminal application.


Put the following code in your newly created ~/.zshenv file. This file is automatically read by Zsh on opening a new Terminal, and the function will then be available for you to run on the Terminal command-line:


~/.zshenv

function new_safari_tab () {
: <<"COMMENT"
Open a specified web address in a new Safari tab. Put this in your
Zsh ~/.zshenv file so the function is available in the Terminal when
the Terminal application is launched.

Usage: new_safari_tab urlstring
Example: new_safari_tab http://www.apple.com
COMMENT

    /usr/bin/osascript <<AS
    use scripting additions

    tell application "Safari"
    activate
        set myURL to "${1}" as text
        if not myURL = "" then
           tell front window
              set current tab to (make new tab with properties {URL:myURL})
           end tell
        else
           return
        end if
    end tell
AS
    return
}


Once you have added this in the ~/.zshenv file, you can make it immediately active by:

source ~/.zshenv


And, to open a new browser tab in Safari, you run the function in the Terminal as:

new_safari_tab https://www.google.com


Tested in macOS 11.5.2 on an M1 mini.

4 replies
Question marked as Top-ranking reply

Sep 10, 2021 7:19 AM in response to kenan201

The Terminal "open" command does not control how you interact with Safari, and cannot direct Safari to create a new tab. However, it can be done with a short Zsh (or Bash) script assigned to a shell as a function, alias, or in this example, a Zsh function that is available to you when you launch the Terminal application.


Put the following code in your newly created ~/.zshenv file. This file is automatically read by Zsh on opening a new Terminal, and the function will then be available for you to run on the Terminal command-line:


~/.zshenv

function new_safari_tab () {
: <<"COMMENT"
Open a specified web address in a new Safari tab. Put this in your
Zsh ~/.zshenv file so the function is available in the Terminal when
the Terminal application is launched.

Usage: new_safari_tab urlstring
Example: new_safari_tab http://www.apple.com
COMMENT

    /usr/bin/osascript <<AS
    use scripting additions

    tell application "Safari"
    activate
        set myURL to "${1}" as text
        if not myURL = "" then
           tell front window
              set current tab to (make new tab with properties {URL:myURL})
           end tell
        else
           return
        end if
    end tell
AS
    return
}


Once you have added this in the ~/.zshenv file, you can make it immediately active by:

source ~/.zshenv


And, to open a new browser tab in Safari, you run the function in the Terminal as:

new_safari_tab https://www.google.com


Tested in macOS 11.5.2 on an M1 mini.

Sep 11, 2021 5:24 PM in response to kenan201

You may be able to make it even simpler by using the "open" command like this:

open  -a  Safari  "<URL>"


I would suggest trying the "-n" option as well if it does not open a new window/tab. The "-n" option allows opening another instance of the app.

open  -an  Safari  "<URL>"


You will want to replace "<URL>" with the proper URL you want Safari to open. I suggest keeping the double quotes.


See this forum post:

https://superuser.com/questions/133748/running-safari-from-the-command-line-adds-current-directory-to-the-url


Sep 10, 2021 7:18 AM in response to kenan201

kenan201 wrote:

so heres my issue
while running command "open https://www.google.com" in terminal consecutive times. I have noticed the second time i run the command it just shows the original window of google.com instead of opening in a new tab. It works fine when its a different website being requested
is this a mac os bug? ive tried setting new tabs to always in safari but no luck
Im trying to get safari to ALWAYS open a new tab even if its the same website



Maybe you can paint a bigger picture what you are trying to accomplish here.


And what about Command T to open a new tab...(?)




Use tabs for webpages in Safari on Mac - Apple Support

Change Tabs preferences in Safari on Mac - Apple Support

Keyboard shortcuts and gestures in Safari on Mac - Apple ...


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Terminal command to open new tab in Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.