Auto Swoosh to space on a per app basis

keybindingsmission-controlmojavespacesspotlight

There is a lot of questions and articles about the "When switching to an application, switch to a Space with open windows for the application" under the mission control settings pane (also called "Auto Swoosh") such as here and here.

system pref

My issue with this setting is that some applications are used very differently from each other. For example when I want to switch to Spotify I probably also want to move to its space. However, some app (notably web browsers) I'd like the behaviour to be different. Specifically I usually have many safari windows across many spaces and when I switch to safari I probably would want to open a new window in my current space (so I don't want to be switched to a random space).

The best way I see to fix this is some ability to customise the Auto Swoosh setting on a per app basis. I've also been thinking of alternative solutions such as keeping the option off and using apple script or something to make a shortcut to switch to the space with the active window (like a one time manual invocation of Auto Swoosh), or a keeping it the option on and finding a different way of opening safari windows (such as typing in my search into spotlight and using cmd+B to google search it) however nothing works quite as I'd like it to.

What are some options I could use to get to the behaviour I want? My main thing is being able to navigate my computer without using the mouse and without searching through spaces it find the app I want.

Best Answer

I finally figured out a way to do it. I've kept the Auto Swoosh setting on but I've changed the way I open safari. I created a new application using automator with a single applescript block. It has the following code:

tell application "Safari"
    make new document
    activate
end tell

The order is important because its what prevents the spaces switching on activate as there is already a new window in the current space.

To open a new safari window in my current space I simply run this app.

(In case it doesn't make any sense how this is fast, I use spotlight as an app switcher - simply type the first two letters of any app)

EDIT:

I've made a new version that also supports opening links. This is so that when you click a link in e.g. Mail it opens a new safari windows instead of randomly moving to a different space where safari is already open.

Specifically:

  • Opening this app creates a new safari window without moving spaces
  • Clicking a link in any non-safari app will create a new window or a new tab
  • depending on if there is a window already in the space it will open a new tab in that window

Note: the code is modified from here

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

tell application "Safari"
    make new document
    activate
end tell

Save this in AppleScript as application and set it as default browser using this or this.

Copy the following into the app's info.plist to register it as allowing for link opening

 <key>CFBundleURLTypes</key>
 <array>
     <dict>
         <key>CFBundleURLName</key>
         <string>local file</string>
         <key>CFBundleURLSchemes</key>
         <array>
             <string>file</string>
         </array>
     </dict>
     <dict>
         <key>CFBundleURLName</key>
         <string>http URL</string>
         <key>CFBundleURLSchemes</key>
         <array>
             <string>http</string>
         </array>
     </dict>
     <dict>
         <key>CFBundleURLName</key>
         <string>Secure http URL</string>
         <key>CFBundleURLSchemes</key>
         <array>
             <string>https</string>
         </array>
     </dict>
 </array>

Accept app permissions (for some reason the app needs to be in the /Applications folder first). And voila! Now just change the app icon to the safari icon and retain your muscle memory to type the name of this app instead of "safari" when opening new browser windows. (I have put way too much effort into this but after months of using it, I'm so happy with the change)