14

I have multiple Safari windows for tab organization. I usually have multiple tabs in each window.

When I click on a link from an app other than Safari, it opens the link in a new tab in Safari, as Safari is my default browser.

The problem is that it often opens in a Safari window that is currently minimized or that I am currently not using. So it doesn't open the link in my main, active, current window (basically the one I have used last). Instead, it chooses its own "default window" to open links in, which may be a window I'm not even using.

However, I would like these links to always open in the window that I used last, and not in a window that I'm not using. Is it possible to make links always open in the last active window you used, or at least set the "default" window which will open new links?

I am currently running macOS Sierra 10.12.1, with the default Safari version that comes with it.

Oion Akif
  • 8,235
  • 14
  • 68
  • 126
  • You will need to provide more info in order for the community to help you. As your question currently stands, we have no idea whether you're talking about Safari in an iOS device or on a Mac? Also, what OS are you using? That is, if it's macOS, is it macOS Sierra, El Capitan, etc? If it's an iPhone, are you running iOS 10, iOS 9, something else? Regardless, please provide the exact version. And, if it's a Mac, also provide the exact version of Safari you're running. – Monomeeth Nov 07 '16 at 07:11
  • You're absolutely right, I forgot about all of that. I'll update the question! – Oion Akif Nov 07 '16 at 17:08
  • You said "_this is the one when all the other windows are closed, and is in the foreground_", did you mean to say _minimized_ not _closed_? – user3439894 Nov 07 '16 at 17:35
  • @user3439894 ugh, updated the question again to be clearer. – Oion Akif Nov 07 '16 at 17:43
  • I believe the answer is going to be no, it is not possible to set the "default" window which will open new links in when initialed by another app. – user3439894 Nov 07 '16 at 17:48
  • @user3439894 unfortunately that seems to be the case. However, maybe someone might know of a tweak that may allow one to do something about it :) – Oion Akif Nov 07 '16 at 18:06
  • I have a few questions. (1) Is Safari your preferred browser, or are you flexible on this? (2) Assuming Safari is your preferred browser, does it also have to be your default browser? (3) If there was a way to force Safari to always open an external link (i.e. one from another app) in a new window, would that suffice? (4) Does it have to be a free solution, or would you be willing to pay for one (assuming the cost was reasonable)? – Monomeeth Nov 08 '16 at 02:55
  • (1) Yes, personally I prefer Safari (though other people might differ on this, I don't know if that was the intention behind the bounty; indeed, I don't know why a bounty was given!) (2) Yes, it should preferably be the default browser because it's the one I want links to open it and my main browser. (3) Yes, that seems to be fine. The main source of annoyance is when it opens the link in an unused tab. (4) Yes, paying a reasonable amount should also be fine :) – Oion Akif Nov 08 '16 at 07:36
  • I think I have a solution. Can you please show us – step by step – how to reproduce Safari's current behavior? I (1) opened three Safari windows, (2) minimized two of them, (3) opened a note in Notes.app with a clickable URL. (4) Safari launched the URL in the open Safari window. ## Were all applications in the same space? – oa- Nov 11 '16 at 07:27
  • @oa- I *think* I might have found out the problem. It seems like if I open the link from an application from a different space, it will open the link in the minimized window. However, if I open it from the current space, it opens it in the window in the foreground. Tested it on the Notes app. – Oion Akif Nov 27 '16 at 20:46
  • @SkeletonBow: Hello from the future! - haha. I know you asked your question 2½ years ago, and I'm curious whether the behavior you described in your question still happens in High Sierra or Mojave. I've been using Mojave since Dec 2018 and my impression is that a link clicked from outside Safari always opens in whatever Safari window is frontmost. I have a three-display setup and usually run it with the "Displays have separate Spaces" setting (in the Mission Control preferences) turned on, but otherwise I don't normally use Spaces. – jdmc Jun 03 '19 at 15:15
  • @jdmc Hey, thanks for the comment! I haven't used Safari in a while (am using Chrome for various reasons), but as far as I remember I still had this issue on Mojave when I was using it. I'll do a bit more testing and see what happens! – Oion Akif Jun 04 '19 at 16:06
  • It seems to me that they / Safari changed this behaviour and that now it opens it in the last used / active window. – Siniša Šašić Jun 22 '20 at 09:54

3 Answers3

7

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.

Pete
  • 71
  • 1
  • 3
  • This works! And it is the only solution I have seen anywhere on the web. To set the default browser, I used SwiftDefaultApps (https://github.com/Lord-Kamina/SwiftDefaultApps/releases/tag/v2.0.1) since RCDefaultApp does not work with Catalina.I did not edit the Info.plist and the app did not show in dock regardless. – phatmann Jun 16 '20 at 16:14
  • Of note, on more recent versions of macOS this also requires Automation permissions in `System Preferences/Settings > Security & Privacy`. I wasn't able to get that to trigger without some combination of editing the script to call System Events on launch (to test), re-generating the app via `Export > App` from Script Editor and launched it manually, and launching the app manually to trigger this. A more systematic approach to getting the proper permissions would be a welcome update to this solution. – jrk Oct 30 '22 at 20:53
3

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).

Monomeeth
  • 63,349
  • 14
  • 147
  • 188
0

Although it doesn't natively open in your desired window, you can always grab the tab and reposition it to another safari window directly after it has opened.

Not sure if this helps or not, just a suggestion.

  • Thanks for the answer! The only thing is, I'm looking for an option which avoids this in the first place :) – Oion Akif Apr 20 '17 at 08:31