Focusing Browser Tab Using JXA and Hammerspoon
Evan TraversI 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.
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
-
2022-06-08 11:31:29 -0500Rename articles
-
2022-02-08 13:48:30 -0600Update syntax highlighting
-
2020-09-28 09:20:43 -0500Change repo name
I decided to change the name of my hammerspoon configuration repo to
`hammerspoon-config` to avoid confusion... it's not the actual
hammerspoon executable, it's a configuration for that executable. -
2020-06-20 15:20:37 -0500Update some tags
-
2020-06-18 14:26:02 -0500Move everything to CST
Don't know why I didn't do that before. It caused _no_ end of
problems. -
2019-11-14 19:33:21 -0600Auditing the tags in the site...
Many removed, cleaned up, or renamed.
Tags with only one child got yanked.
-
2019-10-31 09:31:59 -0500New post: focusing browser tab using JXA