[Offtopic] Open links in new Safari windows or new tabs

You probably noticed the annoying macOS behaviour when opening a link outside of Safari. It opens in a Safari window but it never, really never opens where I would expect it. It could be a window on the current space, it could be a miniaturized window which will then leave the dock or even worse it could be a window on another space.

I’ve found a nice solution. By setting the default browser to a little AppleScript app it’s possible to make Safari treat external links the way I want.

To not rewrite everything I’ll just paste the upcoming answer to an quite old Ask Different question:

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 …

1 Like