Safari is blocking any call to window.open() which is made inside an async call.
The solution that I found to this problem is to call window.open before making an asnyc call and set the location when the promise resolves.
var windowReference = window.open();
myService.getUrl().then(function(url) {
windowReference.location = url;
});
Answer from Jeff Victorino on Stack OverflowSafari is blocking any call to window.open() which is made inside an async call.
The solution that I found to this problem is to call window.open before making an asnyc call and set the location when the promise resolves.
var windowReference = window.open();
myService.getUrl().then(function(url) {
windowReference.location = url;
});
Using setTimeout
EDIT: A few people are reporting that this method doesn't work anymore on the latest Safari.
Wrapping your window.open(url, '_blank') line of code in the async function with a setTimeout works as well,
setTimeout(() => {
window.open(url, '_blank');
})
EDIT 25/04/2025: Some people reported that using _top works, instead of using _blank
setTimeout(() => {
window.open(url, '_blank');
})
setTimeout code runs on the main thread, instead of the asynchronous one. Tested in Chrome and Safari.
Videos
If you simply use the open command, Safari will open your URL in a new tab or new window, depending on the end user's preferences. You can test different experiences in Safari by opening Settings > Tabs and playing with the options for opening pages in tabs instead of windows.
AppleScript and osascript are your only options to control whether Safari opens URLs in new tabs or new windows, and Apple still very much supports them. (I wouldn't put much stock in someone's speculation that AppleScript is going away without solid supporting evidence, which can only come from Apple.) This isn't something you can do with bash alone.
This will open a new tab in the front window of Safari with your URL or open a new Safari window if one doesn't exist:
#!/bin/zsh
/usr/bin/osascript -e 'tell application "Safari" to get front document'
if [[ "$?" -eq 0 ]]; then
# open URL in new tab of a Safari window if one exists
/usr/bin/osascript -e 'tell application "Safari" to make new tab with properties {URL: "https://www.apple.com"} at front window'
else
# open URL in new Safari window
/usr/bin/osascript -e 'tell application "Safari" to make new document with properties {URL: "https://www.apple.com"}'
fi
You can use the second osascript command by itself to always open your URL in a new Safari window.
You don't want to use open -n because that literally opens a new Safari application rather than using the one that's currently running. When you do this, you'll notice two instances of Safari running in your dock or app switcher.
The simplest solution would be to change Safari's settings for tabs to open pages in windows. See the below image taken from Ventura 13.3.1 (a).

However, since you asked for bash, I will offer the following script, which does not use the osascript command. When necessary, this script temporarily changes the setting circled in red, so a new window is created instead of a new tab.
Note: In order for the
defaultscommand to work properly when used in a Terminal window, the Terminal application will need to be given Full Disk Access. See Privacy & Security in the System Setting application (formally known as the System Preferences application).
#!/bin/bash
unset -v TabCreationPolicy ExitStatus
if ! TabCreationPolicy="$(defaults read -app Safari TabCreationPolicy 2>/dev/null)"; then
TabCreationPolicy=1
fi
if [[ $TabCreationPolicy != 0 ]]; then
defaults write -app Safari TabCreationPolicy 0
fi
open -a Safari "${1:-https://www.google.com}"
ExitStatus=$?
if [[ $TabCreationPolicy != 0 ]]; then
defaults write -app Safari TabCreationPolicy "$TabCreationPolicy"
fi
exit $ExitStatus
The relationship between the selections available for the combo box shown in the above image and the value of the TabCreationPolicy key is given in the table below.
| Open pages in tabs instead of windows | Value of TabCreationPolicy key |
|---|---|
| Never | 0 |
| Automatically | 1 or nonexistent |
| Always | 2 |
There are two ways to solve the problem: Note: "window.opener" is not supported by IE if "showModalDialog" is been used.
1) Instead of "window.showModalDialog" use "window.open"
2) If you want to use "window.showModalDialog" then do the following:
<script language="javascript" type="text/javascript">
function YourFunction()
{
var opener = null;
if (window.dialogArguments) // Internet Explorer supports window.dialogArguments
{
opener = window.dialogArguments;
}
else // Firefox, Safari, Google Chrome and Opera supports window.opener
{
if (window.opener)
{
opener = window.opener;
}
}
// write you code and refer "opener"
window.close();
}
</script>
You can pass arguments to showModalDialog function. Simply pass window object as an argument.
window.showModalDialog(theURL, window);
Yo can access the arguments from the modal window using dialogArguments. See: http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx
var openerWindow = window.dialogArguments;
Every time I want a new Safari window, I have to move mouse to dock and right click and select new window.
Is there any keyboard-centric way to open a new window in current desktop?
I searched through Shortcut settings but there was nothing there.
All I want is a new Safari window in my current desktop, as quickly as possible with a few keystrokes. Spotlight works wonderfully to open apps and search things quickly, but it can't open a new window.
EDIT: I don't mean when I'm already in Safari. I know Command-N opens a new window when you're in Safari.