1

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?

1

2 Answers 2

2

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.

7
  • I should add that you can set up a similar hotkey system as shown here, but in Alfred. You have to create the hotkeys one-by-one and employ a tiny bit of AppleScript, but the result is the identical. I'll update my answer with a demonstration of a workflow I just made that does this.
    – CJK
    Jan 19, 2018 at 10:32
  • Great answer, thanks! Didn't know you could do that in Alfred. Keyboard maestro seems even more powerful, so I'll go with that. Jan 19, 2018 at 12:02
  • The code at the end of your answer has no error handling and fails under at least 3 conditions. 1. Google Chrome is not running. 2. Google Chrome running without any windows open. 3. Not enough tabs to support the n argv passed. Why be so verbose when tell application "Google Chrome" to set active tab index of front window to n does the same thing in less code, is more direct and to the point. However, if error handling is in place that line will change anyway. Have a look at this example as a suggestion and feel free to use any of it to improve the code. Jan 20, 2018 at 19:48
  • @user3439894 Thanks for this, although—in this instance—I was aware. Everything I post script-wise on here is almost always just a "template" or "example" of how a script might look in its basic form to achieve what wants it to achieve under ideal conditions. I occasionally include error-handling clauses in non-trivial instances that are likely to arise, but otherwise I expect (possibly unduly with non-coders) that error-handling and additional features that make a script "robust" are for the user to tend to. Cont'd...
    – CJK
    Jan 21, 2018 at 1:11
  • ...Cont'd: Perhaps I ought to write a disclaimer to this effect every time, but I don't always know where the line falls between offering some friendly help that doesn't nor shouldn't necessarily result in perfection; and listing a set of T's & C's as if I worry I might be sued for not producing commercial-standard solutions ready for deployment. I'll work on trying to inform OPs more on this, because I don't want error-handling of the trivial to get in the way of presenting a bit of functional and simple code. (Also, Alfred fails silently, so the error-handling isn't actually needed.)
    – CJK
    Jan 21, 2018 at 1:19
-1

Have you considered giving a try to Mosaic ? it could be a step towards speeding up your workflow.

2
  • The website does not mention Mosaic letting me specify a keyboard shortcut for a window. Does it? Jan 18, 2018 at 14:11
  • Not sure, apparently keyboard shortcuts are for layouts only yes.
    – Michael
    Jan 19, 2018 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .