Shortcut to an opened tab on browser

When you use "Open URL / Open URL with selection", If the given URL is already opened in the browser, it would be awesome if BTT could switch us to the given tab instead of systematically creating a new tab.

This would open a new range of possibilities by allowing us to associate shortcuts with specific websites. For instance, I would love to be able to set a shortcut to access my "open.spotify.com" tab in one shortcut, but I don't want to end up with 50 Spotify tabs at the end of the day.

For inspiration, Slapdash manage to offer such functionality thanks to their chrome extension. (If the page you want to open is already opened in the browser, it will bring it to the front instead of opening the page in a new tab)

This feature would be really awesome !!
Especially for pages such as Netflix etc. where you are only allowed to have one tab open.
Also it would probably reduce loading time ...

If it won't be implemented directly into BTT, you could write or re-use a script that does this.
Maybe something like this open url in chrome on macOS and reuse already open tab if possible. · GitHub ?
(In this script they also reload the page, I think that is not so good for Netflix etc, I would probably skip that step)

Okay nice, I got the script working :tada::tada::tada:

Create file "open-chrome-tab.js" and save it locally
Then use "Execute Terminal Command Synchronous blocking"

osascript -l JavaScript /Users/lto/open-chrome-tab.js "https://www.bild.de"

Important: For some urls, you need to add the "www", for some not.

open-chrome-tab.js:

#!/usr/bin/env osascript -l JavaScript
const Chrome = Application('Chrome');

// although there is ES6 support , but "run" function
// can't use fat arrow functions.
// https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/ES6-Features-in-JXA#arrow-functions
function run(args) {
  const url = args[0];
  const tabData = findTabForUrl(url);
  console.log(args);
  if (tabData) {
    // set the specified window to be the active window
    Chrome.windows[tabData.windowKey].activeTabIndex = tabData.tabKey + 1;

    // reload the tab
    // tabData.tab.reload();

    // set the specified tab to be active
    Chrome.windows[tabData.windowKey].index = 1;
  } else {
    // create tab
    const tab = Chrome.Tab({url});

    // add tab to front window, making it the active tab
    Chrome.windows[0].tabs.push(tab);
  }
  // bring the window to front
  Chrome.activate();
}

function findTabForUrl(url) {
  const urlPattern = new RegExp(`^${url}.*`);

  // Chrome.windows is array like and can be looped
  for (let i = 0; i < Chrome.windows.length; i++) {
    const currentWindow = Chrome.windows[i];
    // same as chrome.windows, currentWindow.tabs is array like and can be looped.
    for (let j = 0; j < currentWindow.tabs.length; j++) {
      const currentTab = currentWindow.tabs[j];

      if (urlPattern.test(currentTab.url())) {
        return {
          windowKey: i,
          tabKey: j,
          tab: currentTab
        };
      }
    }
  }
}
2 Likes

Ok, after using it for a couple of hours, it's actually life saving for someone like me who has > 50 chrome tabs open :sweat_smile:
In combination eg. with my stream deck it's awesome

1 Like