Open url in macro based on the browser in focus?

Hi all

for years i had seperate macro groups for each browser with the same URL's cloned for each browser, ie

then it dawned at me that im an idiot :smiley: ..i should be using 1 macro group with same set of URLs and just open them in the browser i want

now im not sure whats the best strategy to achive this. initially i thought of an if else command where if browser is X open with X, if its Y open in Y..but 1)can one add 3 if else's (i can only see an if and then else option) and 2)that seems repetative?

then i though is there a way to auto get into a variable the application (browser) that is in front, ie the one im actually using and then use that with the open with option in the open url command?

is that even possible?

Happy to hear other approaches for this, since im probably doing something idiotic here :smiley: :smiley:

thx a bunch as always

X

You don't need to get the name of the application/browser that is in front because you can do it this way:

image

There are many KM actions which require hardcoded application names. None of these allow "variables" for the application name. However AppleScript probably has ways to do that sort of thing, so it may be possible to use AppleScript to handle applications by name in a variable.

ahh this is neat but i currently use firefox and this seems to only work with safari/chrome, is that correct or am i missing something (i did try it and it didnt work in ff but rather opens the url in chrome-currenty default on my system)

also: as opposed to the open url which opens the URL in a new tab here it seems to open in the same tab?

thx so much again

Z

In that case I guess you could make a macro (or subroutine) that works like this:

Of course, you would need an IF statement for each browser that you use. That's not too difficult. The IF statements don't have to be nested; they can be sequential.

I didn't know about that difference, but since my latest solution used the same Open URL action that you wanted to use, I guess this isn't a problem.

thx so much!

ive never use subroutines before so this maybe silly bit i created this subroutine

then i created a macro to call upon the subroutine (is that correct) that looks like this

but this dosent seem to work. i assume im missing something? :slight_smile:

thx again

Z

ha this is intersting..seems like this works in safari but, not in firefox but opens a blank page (and not amazon in chrome)

haha weird :slight_smile:

ok super confused..haha

facebook seems to work but other sites dont..is it the naming of the macro? ie

works

does not work

ok problem solved , for those in the future that may be interested i had to use the full url , ie http://XXX.. previously in my macros that wasnt necessary...

awesome!

thx all

Z

You are welcome. I was a little lucky, because I didn't test my macro when I wrote it for you. Good troubleshooting.

It'll work even better if you use https:// wherever possible. Yes, modern browsers should renegotiate the http connection to https -- but why wait for that?

This is because Firefox doesn't have any AppleScript-ability -- so most of KM's browser actions won't work with it.

Hi All,

Re. the "won't work in Firefox" comment, this may help you.

I had a similar challenge. However, I didn't need to open a bunch of URLs, but when you look at my Javascript code snippet below you'll see that it can easily be modified to open whatever URL(s) you want. In my case, I want to reload a google search results page with the old 100 results instead of the new limit of 10 per page loaded. Apparently google has changed what and how they return search results (a real bummer, I think). This snippet runs in an action titled, "Execute JavaScript in Front Browser."

I think all you'd have to do is execute this single line of code however many times you want:

window.location.href = URL;

ORIGINAL CODE SNIPPET WITH THE REGEX REPLACE.

let url = window.location.href;
let regex = /num=\d+/;

if (regex.test(url)) {
url = url.replace(regex, "num=100");
} else {
url += (url.includes('?') ? '&' : '?') + "num=100";
}

window.location.href = url;

The problem is that the "Execute JavaScript in Front Browser" requires the browser supports AppleScript to at least the do javascript (WebKit) or execute (Chromium) -- which Firefox doesn't.

Indeed, if you open Safari or Chrome, make Firefox your frontmost browser, then run this action:

image

...Firefox is completely ignored and Safari or Chrome pops to the front to handle it.

..Firefox is completely ignored and Safari or Chrome pops to the front to handle it.

i can confirm that :slight_smile:

would be great to get alternative support in Keyboard Maestro for FF (even though it dosent support applescript :)). in 2025 extensions are kinda going away in chrome (aAt least the good ones) and in general its always good to support the smaller players :slight_smile:

been using safari for like 8 years but after using chrome (and then FF) lately damm been missing out on some life changing privacy and ad blocking (for the bad ads...) extensions that are great

PS. is there another non applescript way of getting the URL/Title of the current tab in FF (js? magic? :))

best

Z

OCR is very magical and definitely could do some magic here.

But Iโ€™m on vacation as I write this so I canโ€™t write code for you.

Other way round -- you need to press Mozilla to include the relevant hooks in Mac Firefox. Good luck with that -- they've ignored any such requests since day 1...

On a slightly more optimistic note, if they want Firefox to take advantage of Apple Intelligence they'll have to include various "Intents". And you should be able to leverage those Intents through Shortcuts and, therefore, KM. Don't hold your breath waiting for that to happen, though!

For the URL, same was you would do it manually -- make the window active, "Send a Keystroke... โŒ˜L" to activate the address bar, "Send a Keystroke... โŒ˜C" to copy the URL. You can open an URL in the same way, but pasting with โŒ˜V.

For the title, you could macro the UI to show Firefox's Developer Tools, switch to Console, enter your JavaScript code, somehow get the result returned. But, most of the time, the %FrontWindowName% token is a good enough proxy for the actual page title.