How do make a link open in a specific Safari window in macOS?

As a developer I researched the possibility of extensions being able to achieve what you want by always opening a new window when clicking a link contained outside of Safari itself. I'm sorry to say I don't think this approach is going to be possible.

Unfortunately, Safari does not give extensions a way to determine the origin of a new tab/window. What I mean by this is that a new tab can be created because the user clicks a link in an email, command-clicks a link in Safari, opens a Safari bookmark while holding down Command, clicks a link within a PDF document, or any one of several other actions that will open a page in a new tab. Regardless of the method, Safari does not provide how the window was opened to a Safari extension.

Since all these events look the same, no extension can force links from outside Safari to open in new windows without also affecting links opened in other ways (including while browsing within Safari).


This can be done with an AppleScript app which opens the link in Safari and making this app the default browser.

I've found two versions while testing: either open in new windows or in new tabs if there's already a window in the current space.

  • Open Script Editor.app (or even better Script Debugger)

  • If you want links to open in a new window use this

    on open location theURL    
        tell application "Safari"  
            make new document with properties {URL:theURL}  
            activate  
        end tell  
    end open location  
  • If you want links to open in a new tab (if a window is available in the current space) use this:
    on open location theURL  
        tell application "System Events"  
            tell process "Safari"  
                try  
                    set frontWinName to name of window 1 
                on error  
                    my openNewWindow(theURL)  
                    return  
                end try  
            end tell  
        end tell  

        tell application "Safari"  
            try  
                tell (first window whose name is frontWinName and miniaturized is false)  
                    set current tab to make new tab with properties {URL:theURL}  
                    activate  
                end tell  
            on error  
                my openNewWindow(theURL)  
            end try  
        end tell  
    end open location  


    on openNewWindow(theURL)  
        tell application "Safari"  
            make new document with properties {URL:theURL}  
            activate  
        end tell  
    end openNewWindow  
  • Save as app

  • If you don't want the app shown in dock

    • Right click the app, go into the package and find Info.plist

    • Add this in Info.plist

        <key>LSUIElement</key>  
        <true/>
    
  • Set the app as default browser (I used RCDefaultApp).

If you want to see how annoying the default macOS link behaviour is I suggest switching back to Safari as default browser after some hours ...

I'm still on 10.14.6 and don't know possible restrictions in Catalina.