I was wondering if anybody knew of a way to accomplish this in Keyboard Maestro with an AppleScript?
In either Safari or Chrome, I’d love to press a keyboard shortcut that would look at all the open tabs (in my frontmost browser window).
As soon as it finds a tab that is already opened to any page from the domain apple.com, I would love to jump to that already-opened tab. (It might be a page from different subdomains -- it might be a page from support.apple.com, store.apple.com, www.apple.com, appleid.apple.com, etc.)
However, if it doesn’t find any tabs that are already opened to the domain name apple.com, then I would love for it to create a brand new tab that opens to the homepage www.apple.com.
Thanks for any insights that you might be able to provide!
AppleScript is a great thing that usually provides optimal answers, but AppleScript is rarely the only way to solve problems. Why do you want AppleScript as your solution?
I think I can see a way to do it with native KM actions, but I don't want to investigate that if you really need or want AppleScript. I can tell you right now that my KM solution will probably cause the "visual effect" of activating each tab in order to be able to "read" the address in the tab. But it should work very quickly. Basically my solution would be using the "Next Tab" action to activate the tab, then use the "FrontBrowserTitle" token to get the token name. And if the returned value meets your requirement, it would stop there but if it kept going and started repeating, it would stop and create a new tab as you desire. It wouldn't be a very long program. I have never used KM to manipulate browsers so I'm in unfamiliar territory, but I think it could work.
Oh, and so sorry for my misleading question — I actually didn't care at all if my solution was based on AppleScript.
I was just ASSUMING that AppleScript was the only way to go, because I couldn't figure out how to do it natively with Keyboard Maestro's native actions.
But it sounds like you might have figured out a way to do it with KM's native actions! That would be incredible, and would be even better than AppleScript because it's all built into KM!
Ok let me take a quick stab at it now. I'm no expert at this, so I'm not 100% certain I will succeed. I am more familiar with other parts of KM, but I'm happy to try this now until I need some sleep.
Okay, I wrote up a routine and it seems to be working. It had no bugs (but I didn't test it thoroughly, so it might have bugs I didn't notice.) Hmm, I just noticed it works only with Safari, but that would take only a minute to fix. I've uploaded the program for you below.
Before I change it or fix the part that it only works with Safari, I'd like you to load this macro and test it out yourself. There's a good chance that you can fix it to work with both Safari and Chrome without my assistance. HINT: You don't need to add any new statements, all you need to do is modify any statement that refers to Safari and replace it with a statement that works with "FrontBrowser". If that's too much for you, I'll fix it tomorrow. But to be honest I like to challenge people to try things. And so this is your challenge: "without adding any statements, modify the program below to work with both Safari and Chrome."
Find an Apple URL in the tabs of the Safari browser Macro (v11.0)
P.S. I didn't mention it, but the group that this macro was stored in has a flag that says the macro is active only when Safari is active. That's probably the case for you anyway, so you may not need to set that flag yourself. It's just a warning for you.
Not only did you perfectly solve this problem for me, but I also undertook your personal challenge, and I was able to edit the macro to work with ANY browser that is the frontmost browser window!
Thank you sooo much for guiding me through this perfectly!!
I've attached the macro below, along with a screenshot of my setup.
Here's another macro you may find useful, which presents a searchable list of all open tabs. I use it with Brave Browser, but you can change the browser name in the two AppleScript actions to suit your needs.