%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.

Yes it works, but how to get the page title, too?

The JavaScript does work,
however to avoid ALL UPPER CASE for the page title, TITLE CASE would do?
/
best regards,
OmarKN

Did you try changing URL to title?

If that works, it's a question of how you want the two items returned -- the AS will return a string so you're going to have to parse the items out somehow. You might like JSON, for example:

URL and Title from Opera.kmmacros (4.4 KB)

The biggest problem is escaping all the quotes when building the string in AS!

The macro is doing all the heavy lifting.

However asked ComplexPoint (see previous post) (and @ https://forum.keyboardmaestro.com/t/frontbrowserurl-doesnt-work-in-opera/31924/2) how to get the MD title as Title Case instead of all letters Upper Case.

Same here - is this the default feature of AppleScript/ Javascript for Markdown titles?

This is the Javascript from @ComplexPoint (https://forum.keyboardmaestro.com/t/frontbrowserurl-doesnt-work-in-opera/31924/2)

Result:
TITLE-IN-UPPER-CASE
http://link

(I removed the MD brackets)

/
best regards,
OmarKN

The AppleScript will return the page title in whatever case the page title is in. If that's upper case and you want title case, use KM's "Filter" action to change it.

The point is that you wanted to get both URL and title from the page. But AS returns a single string. So you need to format that string in a way that you can then parse out the URL and title from it. You could separate them with a tab, a \n, or something else -- but a JSON-format string probably copes best if there might be "strange" characters in there.

IMO you are better off getting the URL and title and then, if you want a Markdown link, building it with further KM actions. But you could do it in the AS if you wanted. Just change the return line to:

return "[" & theTitle & "](" & theURL & ")"

...but it makes further manipulation, eg of page title case, more difficult.

Returning a JSON pair, indexable in a Keyboard Maestro %JSONValue% token:

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

1 Like

This looks very good!

Only - how to set the results from the 2 variables to a clipboard (so it can be pasted)?

(This is something everyone should know of course.)

/okn

Change the final action, in the example above,

  • from Display Text to
  • Set Clipboard to Text

1 Like

Very good,

So you filtered the lable or page title to ‘Title Case’ for the JavaScript, and for the AppleScript this was not necessary.

Then the results are identical.
(Escaping the \“ in the AppleScript is a job itself!)

Which solution would you prefer?

Anyway, thank you @ComplexPoint , and @Nige_S for the assistance.

Now, we can get the webpage title and URL not only for Safari/ G Chrome (%Frontbrowser%), but also for Firefox and Opera!

/okn