Best Way to Use a Browser URL as a Macro Trigger?

Hi,

I am trying to create a web url-based macro. I mean "a macro that I would trigger via a keyword and would be triggered only on a specific website".

For example: when on the Keyboard Maestro forum and only on the Keyboard Maestro forum, typing "nn", would trigger some action.

For this to work, I need to get the url of the browser I am using to make sure I am on the Keyboard Maestro forum website.

I honestly do not know how to make it work. I took a look at this thread ( [How to use Chrome URL as a macro trigger?]), but as I am using the Brave browser 90% if not more of the time, that didn't really help.

What strikes me the most is I can't set the right conditions so Keyboard Maestro triggers the action I want to trigger.

How can Keyboard Maestro return false as the url does really contain "keyboard"?

Reading the documentation didn't help on this particular point as is only a description of the if then else action.

It is certainly more a problem of method than anything else as I am new to Keyboard Maestro (and loving it more every day).

2 Likes

A solution. Using the "tittle contains" condition made it work.

(Please do not mind about the tittle of the macro on the pic. I was so glad to find a solution that I didn't take the time to change it before publishing it on the forum).

1 Like

It was too good to be true.

Using the url that starts with "https://forum.keyboardmaestro.com/t/...", if I use the keyword "forum" the condition is not met. But if I use the the keyword "keyboard" the condition is met. :thinking:


1 Like

The title is not the URL. The title is the HTML-specified title for the page. To see what the title is, use Get Info (command-I) while on the page.

Hitting command-I it did nothing. But with the developer view I got what you meant. Thank you.

How can I use the url of a website as a condition?

I have a macro that I only set to trigger when I'm on the Keyboard Maestro forum website. It works really well using the method you stumbled upon above but prematurely gave up on. Whilst Keyboard Maestro doesn't, as far as I am aware, have a URL-based trigger, the webpage title is often just as good.

It did necessitate creating a specialised macro group just to house a single macro for one single website. Here it is:


Creating a macro group specific to a particular webpage based on the browser's window title

Ignore the name of the macro group, which does allude to using the URL as a discriminator, but it doesn't. It simply ensures the macro is only available in the web browsers I use when the active window's title contains "Keyboard Maestro Discourse".

As I'm typing this reply, I'm in Google Chrome and the title of the window is: "Best way to use a browser url as a macro trigger? - Questions & Suggestions - Keyboard Maestro Discourse", which hopefully makes sense how and why this method works, providing that, no matter where I navigate to on this forum, the words "Keyboard Maestro Discourse" remain a part of the window title, even if the rest of it changes.


Another way of achieving something similar that almost mimics a URL-trigger is to have a macro available on a per-application basis (Brave in your instance), but make the first action an assertion that cancels the macro upon failure, such as when the URL of the front browser doesn't contain "forum.keyboardmaestro.com":


Using an assertion to test the URL in an application-specific macro

I'm not certain whether the %FrontBrowserURL% token will work with Brave, but there will probably be other ways of getting the URL if not. The limitation of this method is that any hotkey I assign to trigger it will be unavailable for general use any time you are using your browser, regardless of what site you are on. That is, a true URL-based trigger would overwrite the functionality of the hotkey just during the instances when you are visiting that website, but restore its original function when you navigate away from it; here, if I decide that I want to use the tab key (which is what I set as my hotkey for my Keyboard Maestro Forum macro above), if I implemented it using this method, I wouldn't have access to the tab key at any other time.

4 Likes

CJK,

Instead of canceling on failure, you can have the conditional type the key you've given up as the trigger. That way, you don't have to give it up. The key will work the way you want it to in either condition.

I believe Peter has stated a few times before that simulating a key combination that is a designated hotkey is unpredictable and generally advises against doing this as one could end up in an infinite loop of self-spawning child instances of your macro.

I can believe that. It's never happened for me, though. I do have a few macros that operate that way.

The reason I've never been worried about that is that it seems fundamentally different to have a "type command T" action in a macro triggered by command-T, compared to having an "activate macro x" action within macro x.

Huh?­­­­

That latter would be a loop nightmare, because the macro calls itself, whereas the former just passes a keystroke. By the bye, as a quick test, I just created a macro that simulates "option-Q", which is what I use as a trigger to bring up the KM editor (via a different macro). When I invoke the macro, it does not have the effect of invoking the other macro. Instead, it just types "option-Q".

Yeah, I've done the same experiment in the past as well. Often, when someone tells me not to do something, I will give it a try anyway. But I'm still not going to set up my macros to work like that, even though I know you can just press Cancel All Macros from the menu bar.

I use an extremely inelegant method to do something similar...I invoke a Cmd-L to select the URL in the address bar, then Cmd-C to copy it to the clipboard, the use Switch/Case to see if the clipboard contains part of the URL I'm looking for. You could also you If/Then/Else.

What browser are you using, @kengler412 ?

Opera.

Sorry for the way too late answer. :man_facepalming:

It exactly answers my question. :+1:

I've just tested @CJK first method and it works very well.

To be honest, I didn't know I could set macro's availability that way.

Thank you!

1 Like

Hey Kevin,

Opera supports AppleScript, so you can easily get the title or URL of a given window/tab:

tell application "Opera"
   set tabName to title of active tab of front window
   set tabURL to URL of active tab of front window
end tell

-Chris

1 Like

This is awesome! It' should speed up my browsing significantly. Thanks!

1 Like

I am trying to implement url as trigger and end up here.

I create a new macro group as below:

image

It works most of time but not reliably all the time. Sometimes when I switched away from a tab from this forum to another tab that doesn't contain this word, it also triggered, then switch back to this forum but it doesn't trigger.

Is this still best way to use chrome title or url as trigger?

Thanks.