MacOS – macOS utility that lets me assign a keyboard shortcut for switching to a particular window

macossoftware-recommendation

I usually have a lot of applications open with lots of windows in each. Let's say I'm working on several documents in a word processor, while having several web browser windows open for reference. I need to switch back and forth between my different documents, and a particular web browser window, which is a Wikipedia article.

Currently, to switch to the Wikipedia article, I need to

  1. Tab a random number of times (depending on how many applications I have open) to Chrome, then
  2. Cmd ⌘ ` a number of times (depending on how many Chrome windows I have open) to reach the window with the Wikipedia article.

App switchers like Witch, that can be configured to bring up a list of all open windows from all applications, and lets you switch between them, improves the situation slightly. But it still requires me to browse through a list in order to select a window.

My workflow would be much easier if I could assign a keyboard shortcut to that particular Chrome window, to bring it to front.

Ideally, it would work like this:

  1. Activate the window you want to create a shortcut for ("window A")
  2. Bring up the utility app
  3. Assign a keyboard shortcut, say Ctrl Alt ⌘W, to "window A"
  4. From now on, pressing Ctrl Alt ⌘W at any time would bring "window A" to front
  5. When "window A" is closed, the keyboard shortcut is removed

Is there any utility application for macOS that provides this functionality?

Best Answer

Keyboard Maestro

Keyboard Maestro is a paid-for application that allows the creation of hotkeys (amongst many other types of trigger) and has a builtin function to target tabs within Safari or Google Chrome.

It references tabs by index number, e.g. tab 1 being the first tab, tab 2 being the second, and so on.

Here's a macro that took 2 minutes to create and assigns a group of ten hotkeys CmdAlt+0...9, each of which trigger their respective tab in Google Chrome, if the tab exists, and brings it to the foreground (0 represents tab 10 in this instance).

Keyboard Maestro Macro Screenshot

If you needed to be able to reference tabs by their title rather than a fixed index number (in case you changed the order of the tabs), you'd have to use a bit of AppleScript to achieve a similar result.

This piece of AppleScript is of particular value:

    tell application "Google Chrome" to tell its front window to ¬
        if exists (first tab whose title contains "IMDB") then ¬
            repeat until active tab's title contains "IMDB"
                set active tab index to ¬
                    (active tab index mod (number of tabs)) + 1
            end repeat

Alfred

An identical result can be achieved with Alfred, in a slightly different construction of its workflow to that of the macro used in Keyboard Maestro. Alfred doesn't have a builtin command to switch to a specific browser tab, so a little bit of AppleScript has to fill that gap.

Here's the basic workflow layout:

Alfred Screenshot

On the left, I created just four hotkey triggers, but you can create as many as you wish. I mirrored the CmdAlt+1,2, ... format I used in Keyboard Maestro for a direct comparison.

The superimposed orange-dashed window shows the settings for the second hotkey, specifically in supplying it a numerical parameter—2 in its case—that gets passed as an argument into the subsequent AppleScript. Each hotkey has a similar numerical parameter it will deliver to that one script upon triggering.

Here's the AppleScript that performs the action triggered by these hotkeys:

    on run argv

        set n to argv as number

        tell application "Google Chrome" to ¬
            tell its front window to ¬
                set the active tab index to n

        activate application "Google Chrome"

    end run

It would be easy enough to add a few lines to include Safari as an option too, depending which browser was active last when the hotkey is triggered.

Alfred and Keyboard Maestro play quite well together, too, as each has the ability to trigger workflows/macros from external sources, including from each other. So, an Alfred trigger can initiate a KM macro, and, likewise, a KM trigger can initiate an Alfred workflow.