I am trying to create a macro that allows me to toggle through tabs of a specific type in Google Chrome. For example, let's say I have 3 YouTube tabs that spread across the tab bar on the top of the browser (i.e. the 3 YouTube tabs are not directly next to each other). When I trigger a hotkey, I would like it to continue to go to the next tabs (from left to right) until another YouTube tab is encountered, at which point I want it to stop changing tabs. To attempt this, I used a while loop and set the condition as if the Chrome URL does not contain “youtube.com”, I want you to keep going to the next tab (using the shortcut: option + command + →) and have a 0.2-second pause in between. But what ended up happening was it kept cycling through all the tabs without pausing until I hit the "cancel macro” shortcut manually. What did I do wrong here folks?
When I press the hotkey, I want to toggle through only the YouTube tabs as highlighted by the red rectangles below
One problem I see with your macro is that it wouldn't trigger if you were already on a youtube tab, because the condition won't be met. As for cycling through all, I don't see why that would happen. Here's a version I wrote that seems to work:
The first thing it does is switch to another tab, because that's what you want to do, because you invoked the macro. Then, only after switching, it checks if the new tab is a YouTube tab. If it is, it ends, otherwise it keeps switching.
I found it worked fine without any delays, but you may have to add one after the first two steps (outside the while) and one within the while, after the next tab command.
Though it's odd that when I remake my macro to look just like yours, it still cycles through all. But its all fine when I created a new macro and started from scratch! Thx for that!
Anyway, I would like to go one step further now and be able to toggle between any tabs of the same type based on the current tab I’m on, not just YouTube. For example, if I’m on a Google search page, it will toggle to any other Google search page that exists; if I’m on a YouTube page, it will toggle to other YouTube pages etc.
I have made an attempt to do this but unsurprisingly it did not work! Here’s what I tried to do: Store the current tab's URL as a variable, then search it using regular expression ^([a-zA-Z0-9-]+)(?=.) If I am not mistaken, this will capture the first sequence of characters before the first period (e.g. youtube.com → youtube). Then saved it as a new variable (I named it "regex") (I knew something already went wrong here based on my testing). It then goes on to the next tab (i.e. option + control + →). Then a while loop that will continue to move on to the next tab as long as the chrome URL does not contain the “regex” (variable), meaning it's not the same type of tab as the previous tab before the hotkey was pressed.
Sorry if this is stupid, I have never used regular expressions before. Could you tell me what went wrong here please?
You don't need to save the token into a variable, as you can search/replace on it and save to a variable in one shot. And I just built my regex to find the pattern http(s):// at the front of the URL, then everything up to the first / is captured and stored in the variable.
The loop then checks if the current tab starts with the same text as the captured domain; if it doesn't, it keeps switching.
I tested it on multiple sites. I just noticed that if you have two sites with the exact same URL, it works. But if it's like youtube.com/XXX and youtube.com/YYY, then it doesn't! (i.e. if it's not the exact same webpage even though it's the same website, it won't work?). Same goes for the websites you listed
Oh my, I'm so embarrassed…I uploaded a development version, not the final version that works. Please redownload the macro above—I have replaced it with the proper working version.
At least, this is the version that works on my Mac and that I was talking about :); hopefully it works on your Mac.
You have nothing to be embarrassed about haha I'm the one that should be embarrassed
It worked like Magic!!! U r a magician!
One final question if you don't mind. Is there any way to speed up that tabs switching process? (I have shortcuts that press "command + option + →" two and three times in a row and they switch much faster then this macro)
However, at this point, I can't imagine what else could be done to speed it up: The macro has but one condition to check, and it has to check that condition when switching tabs in order to make sure the one you want is selected. Maybe someone else can come up with a speedier solution, but that's all I got :).
I'm guessing it's the token query that takes the time -- presumably a delay in Chrome's response. And if there's a lot a tabs...
It might be quicker to get all the tabs and then work out in KM which one to jump to. You could certainly do this quicker by using JavaScript directly, but this is reasonably fast and needs only a couple of simple AppleScripts and a bit of KM text manipulation.
I am not quite sure how to do this. Do I type this into the terminal?
I used the “interactive help” on KM to check. No error messages and the macro seems to have been triggered. But it did not toggle to the next related tab as expected
Either navigate to that file in Finder and double-click it, use the KM Editor’s Help→Open Logs Folder menu item to open the folder then double-click the Engine.log file, or open the Console app then ⌘O and paste the path into the Open… dialog.
The most recent entries are at the bottom of the log file.
Do you have Notifications enabled for the KM Engine? You won’t get messages about AppleScript errors, failing regex searches, and so on if you don’t.
Silly question, but… You do have other KM Forum tabs in that window, don’t you? I can other KM icons on the tab bar, but they would be there if those other tabs were eg KM Wiki pages. As far as the macro is concerned a “similar tab” is one with a matching host name in the URL, and if that’s the only Forum tab then the macro will activate it – the macro’s working but it will look like nothing has happened since it’s activated the already active tab.