%FrontBrowserURL% doesn't work in Opera

Hello,

I have recently made the switch from Brave to Opera but I can't get my URL based macros to work.

I have a bunch of Macros that work based on the website I'm on with if-else loops using %FrontBrowserURL%.

In all of my other browsers Safari, Chrome, Brave they work flawlessly but since switching to Opera I can't get them to work.

When I have Keyboard Maestro open with the evaluation for the %FrontBrowserURL% it shows false while having the URL in Opera in the front.

It seems Keyboard Maestro can't read the Opera URLs.

Testing in Script Editor with the language selector at top left set to JavaScript (rather than AppleScript) suggests that you could use an Execute JavaScript for Automation action.

(() => {
    "use strict";

    const operaWin = Application("Opera").windows.at(0);

    return operaWin.exists()
        ? (() => {
            const tab = operaWin.tabs.at(0);

            return tab.exists()
                ? `[${tab.name()}](${tab.url()})`
                : "No front tab found in Opera";
        })()
        : "No window open in Opera.";
})();
2 Likes

Thanks, I will test that out tomorrow :slight_smile:

Update to that snippet – activeTab is more likely (than the first tab) to be what you want, so:

(() => {
    "use strict";

    const operaWin = Application("Opera").windows.at(0);

    return operaWin.exists()
        ? (() => {
            const tab = operaWin.activeTab();

            return tab.exists()
                ? `[${tab.name()}](${tab.url()})`
                : "No active tab found in Opera";
        })()
        : "No window open in Opera.";
})();
1 Like

Thanks, but I don't quite understand what I need to insert for it to work the way the %FrontBrowserURL% evaluation works.

I have inserted the script like this and it returns true:

Obviously you left blank spaces for tab.name or tab.url, I tried replacing tab.url with "notion.so" with no results. Could you please elaborate what I need to do for the script to work?

I append a simple macro I am using in the Browser, maybe you can use this for example.

Thanks so far for your help :slight_smile:

Notion | Database Up.kmmacros (4.0 KB)

for the script to work

I may have missed your definition of what "working" would be here.

What's the value / result that you are trying to obtain ?

To get an MD link here, for example, you could write:

Reading label and-or url from active Opera tab.kmmacros (2.0 KB)

1 Like

Instead of using it like the %FrontBowserURL% token, use a variation on @ComplexPoint's action to collect Opera's active tab's URL into a KM variable, then test that variable.

For example:

At the BBC?.kmmacros (7.0 KB)

Image

...which will give different results depending on whether the active tab is at the BBC website.

Note:
For me, a similar "Execute AppleScript" action containing

tell application "Opera"
	return URL of active tab of front window
end tell

...will also work.

3 Likes

That works thanks.
The Apple Script seems to be the easiest solution.

But still stands why is Keyboard Maestro not able to read the Opera URLs?
@peternlewis Can you chime in maybe?

Partly because it isn't as featureful as @ComplexPoint's JXA. A more complete version would be:

tell application "Opera"
	if (count of windows) > 0 then
		return URL of active tab of front window
	else
		return "No active tab found in Opera"
	end if
end tell

See the "Add additional Browsers to the Front Browser Actions, Tokens, and Functions." part of the "Preferences Set by Command Line" section of the manual for how to add Opera -- the full command is probably:

defaults write com.stairways.keyboardmaestro.engine AdditionalWebBrowserBundleIDs -string "com.operasoftware.Opera"

...as in "it worked for me and I'm assuming your Opera bundle ID is the same".

1 Like

Keyboard Maestro supports Safari and Chrome as browsers, since they both offer full AppleScript support, including executing JavaScript via AppleScript.

It also supports any other browser that behaves the same way as either of them (so Safari Technology Preview, and all the variants of Chrome such as Edge, Brave, etc).

If Opera behaves like either of them, then Keyboard Maestro will support it automatically (with a minor config change that you can apply to tell Keyboard Maestro about it, and which would be incorporated in future versions of Keyboard Maestro).

But if it has its own syntax and/or incomplete support, then Keyboard Maestro wont support it and you can use whatever support it does have via AppleScript.

Bingo! Thank you so much for the ApppleScript. I've been wanting a solution for Arc web browser for quite some time. Dang, wish I had time to learn coding and scripting.