Focusing Browser Tab Using JXA and Hammerspoon

I recently started using Google Hangouts as my main computer calling method. Very quickly, the tab with the (often muted) call gets lost in all my other windows. I used to be able to just focus Zoom.app using hammerspoon with a keypress, but focusing a tab in a browser is a little tricker than focusing an application.

I found this blog post which totally worked, but I thought the JXA javascript could be cleaned up just a touch.

Brave's Scripting Dictionary... mostly equivalent to Chrome.

It’s really hard to find docs for JXA… there’s a ton of broken links to the Apple Developer Docs. The JXA Cookbook is handy, and after I realized that most of the applescript could be camelCased into a JXA equivalent things went more smoothly. If you open the Script Editor and open an app’s dictionary, you can switch the language to Javascript and things make a little more sense.

Here’s what I ended up with:

(function() {
  var brave = Application('Brave');
  brave.activate();
  for (win of brave.windows()) {
    var tabIndex =
      win.tabs().findIndex(tab => tab.url().match(/meet.google.com/));
    if (tabIndex != -1) {
      win.activeTabIndex = (tabIndex + 1);
      win.index = 1;
    }
  }
})();

It’s pretty straightforward. The API for switching tabs is a little limited and frustrating, but it works. By wrapping this in a Hammerspoon function tied to my hyper key layer, I can trigger this at any point (see that here).

References


Changelog
  • 2019-11-14 19:33:21 -0600

    Auditing the tags in the site...

    Many removed, cleaned up, or renamed.

    Tags with only one child got yanked.

  • 2019-10-31 09:31:59 -0500

    New post: focusing browser tab using JXA