How Do I Get the Front Most Browser?

How Do I Get the name, or activate, the Front Most Browser when it is NOT the Front Most App?

KM 8 provides us with a great set of Actions for accessing the "Front Browser", like this one:

It works great, even using the last Browser that was active, but is NOT active when the Macro is run.
The problem I have is that this action (and none of the Front Bowser Actions) ACTIVATE the Front Browser.
So, also I can set or change the URL of the Front Browser, the Browser is not activated, and could be hidden behind other windows/apps on my desktop.

So, after I have used one of these Front Browser Actions, how do I activate the Browser that it used?
Again, it may NOT be the front most app.

I have reviewed all of the other KM Actions and Tokens, and I haven't found any that will help me with this.

So until/if I can answer that question, I'm using the default Browser like this:

Anyone have a better idea?

Maybe I’m not correctly reading how your workaround macro worksβ€”it looks as though it’s using whatever is your default browser. If that is the case, the Open A URL action (in the Web category) set to the Default Application should work.

Excellent point! I was so focused on identifying the frontmost browser I overlooked the obvious.

This also simplifies my macro since I can use the same action to open non-web URLs as well.

Thanks.

Hey JM,

There are a couple of ways to do this.

First β€” use the Execute a JavaScript in Front Browser action and query the user-agent with JavaScript.

Execute a JavaScript in Safari.kmactions (660 B)

Second β€” get a list of the apps β€” which comes back in INDEX order β€” then search for the nearest browser to the top.

Index-order meaning the current front-to-back ordering of the apps on the system - not the launch-order.

App1 is the frontmost app, App2 is the app used before App1 was brought forward, etcetera β€” this follows the left-to-right order of the apps in the Dock.

The utility of this is that you can test to see how apps are layered.

Get App Names in Index-Order.kmmacros (6.3 KB)

-Chris

2 Likes

Thanks Chris. This put me on the right track.
I found I had to use this JavaScript:
navigator.userAgent.match(/Chrome/i) ? "Chrome" : "Safari";

which will return either "Chrome" or "Safari", OR, if no browser is currently running, it will return "Not Running"

For everyone's benefit, navigator.userAgent returns this in Chrome:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36

Surprisingly enough, it includes the string "Safari", even when run in Chrome.

Fortunately, "Chrome" does NOT appear when run in Safari:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7" = $2

In case someone is wondering why I don't also test explicity for "Safari", it is because only Safari and Chrome support JavaScript in Browser. So, if it is not Chrome, it must be Safari. :wink:

Finally for just a bit of JavaScript tutorial, let's looks at this:
navigator.userAgent.match(/Chrome/i) ? "Chrome" : "Safari";

This is a very powerful JavaScript technique called Conditional (ternary) Operator.

condition ? expr1 : expr2

If condition is true, the operator returns the value of expr1; otherwise, it returns the value of expr2.

In our case, if "Chrome" is NOT found in the string, then it will return a NULL object. And nulls, in this case, are treated as false.

1 Like

I will look to adding a token that names the browser that Keyboard Maestro thinks is the most recently front one.

3 Likes

The FrontBrowser token was added in version 9.