Make a Macro Group Available Only When The Front Window Title Contains Certain Text

Thanks for your time!
Yes, the macro itself works fine but what I wanted is to have this macro runs only when the tab is in km. If you remove the string "keyboard maestro" in macro group setting, then this macro will be executed every time when switch a tab, which I believe sometime cause performance issue. Especially when there are many such macros doing different things for different website.

If you were worried about performance, which you didn't state earlier, you wouldn't be using a Find Image action. Instead, you would be reading the URL using a token.

I was afraid to mention this earlier, because I might be wrong, but I think he problem is that you misunderstand how the group attribute works. So let me speculate. That attribute is NOT active when you click on the KM tab, because when you click on the KM tab, the non-KM tab is the tab that's active. Therefore it doesn't trigger. That could be the explanation. I'll leave it to you to test.

If you want to have a series of macros that run automatically using the focused window trigger and limit them to specific websites, you'll probably be better off keeping all these macros in a single macro group that's always active and specifying which website they run on with either an If/Then action or an Assert action that checks the title and/or URL. To use your KM Forum macro as an example:

kmtest 1.1.kmmacros (28.3 KB)

1 Like

However switching from any other applications to Chrome worked as expected, say I switched from Finder to Chrome's KM tab, it works fine using this group setting.

Thanks. This way the actual check is at the beginning of each macros. So if I have 20 such macro in one group, all 20 macros are triggered at any window/tab switch.

I was under the impression that the group setting is supposed to avoid such issue, but somehow it is not working when switching tabs in Chrome (and this works perfect when switching from any other applications).
https://wiki.keyboardmaestro.com/Macro_Groups

Possible a bug?

You're welcome. I don't have Chrome installed on my machine, so I changed the macro group to Safari to test it. If you haven't changed it back, that would explain why it isn't working in Chrome. Sorry, I should have thought to change it back myself before posting.

It's not entirely clear to me from your post, but if you don't want all 20 or so macros like this to be triggered whenever you switch tabs or windows, even if the If/Then or Assert action will prevent the non-applicable ones from doing anything, another way to go about this is to only use one macro and a Switch/Case action to check the title/URL and run the corresponding actions:

This way, you could either manage all the actions for each site in this macro directly, or you could still make 20 website-specific macros without triggers and use an Execute a Macro action to run the macro you want for each site.

1 Like

Well, the group permissions are what I was hoping you could test because I rarely use them. For example, the documentation for groups says:

Notice the part I circled in red, which you are using to "RESTRICT" when the macro runs. But in the next line, which I highlighted in blue, the wording says "YOU CAN ALSO CHOOSE TO ACTIVATE..." which in this case is describing a different option than the one you are using, but the wording indicates that this is an OR function, not an AND function.

If the wording is accurate, this seems to explain your problem. This is the sort of thing I was hoping you would test, and let us know if this is operating as a RESTRICTIVE or an INCLUSIVE condition. I can see why you thought it would be a restrictive condition, but that's not what the wording in the documentation actually says.

There's a substantial chance Im wrong. But if I'm wrong, then the documentation seems to be wrong too, so I can't be faulted too much.

@peternlewis, could you give some clarification for this macro group settings?

The part in red refers to the macro group activation based on the window title.

Eg:

The macro group is active only in Mail when the focussed window exists and the title does not match one that looks like the main message viewer - in other words the palette appears in Mail in a composing window.

The section in blue refers to how the macro group is manually activated, for example activated after pressing a hot key.

I don't know where you get that impression, all the requirements need to be met for activation to happen (plus the macro group needs to be enabled).

1 Like

Thanks @peternlewis. This is inline with my understanding. It seems to me the issue I having could be potential unstable in KM in special case.

So you think the problem is called by instability in KM?

As peter explained the settings, it should work in my macro in the first post but it is not and it can be reproduced.

I showed you how to fix the problem, and you confirmed that the fix worked, so you simply are concerned now with what you called a "performance issue." We agree on that part.

But Peter's message was directly addressed to me, and I think it's probably not correct to assume that he was indirectly giving you his support that KM is unstable and that your macro should work. If you are correct that he is supporting you, then I must cede to the Architect. I rarely win an argument with him.

1 Like

Yes peter's example above is exact same as what I had, the macro should be triggered when it is in Chrome and title contains "keyboard maestro". And it is not working consistently.

Yes I agree your workaround is working. However the issue I reported still exists. The group settings is there just that we don't need the workaround if the group settings are actually working. Hope this is clear :slight_smile:

Have a great weekend!

What I was talking about was macro group activation which has nothing to do with macro triggering (other than being a prerequisite - you can't trigger a macro until the macro group is active).

Your macro will not work sensibly, since the macro group is not active until the window title contains “keyboard maestro”, so the trigger might or might not happen if the window title changes to containing “keyboard maestro” - it would depend on the ordering of things which are not specified.

This is the same sort of issue as the more common issue of a macro group that is active only within a specific application having an application triggered macro for when that specific application launches or activates - the macro is not active until after the application launches/activates and so the trigger generally will not fire.

Do you mean there is race condition between macro group activation and macro triggering? The macro works perfectly when switching from any other app to chrome, but when switching tabs in chrome, sometimes works and sometimes doesn't.
And this would happen in your example too, even the macro group is activated, but macro inside this group may or may not triggering(suppose this is macro triggering by "focused window title changes")?

Did you try removing the "Pause 0.6"? I explained in post #4 why that would cause it to work sometimes and not other times.

I mean that if you want a macro to trigger, it must be, and stay, active from before the trigger event happens until after the trigger event happens.

If the macro group is activated or deactivated based on the same change, then who knows whether the macro group will be active when the trigger event happens, maybe it will, maybe it wont.

Lets take a simple real world trigger of "when the bus arrives at the bus stop", but you only watch the bus stop when there is a bus there. Will you see the bus arrive? No, because you aren't watching until the bus is there.

If you want a trigger to fire, the macro group has to be active. It has to be active before the trigger happens, and it has to stay active after the trigger happens. Otherwise how can you expect any reliability? You can't assume the order Keyboard Maestro will deal with events if its the same event that affects both the triggering and the activation.

2 Likes

Hey Henry,

Testing the macro from post #1 on my Mojave system with Keyboard Maestro 10.0.2.

Once I fix the move to action variables and remove the pause it seems to work quite reliably – as long as the macro group is always active.

image

image

I've tried switching tabs and windows, and switching applications – everything works.

The only hang up is when I load the Keyboard Maestro Forum in an existing or a new tab, and Chrome takes a while to load it – then the image is not surprisingly missed.

Pauses are a pain with this kind of use-case – you're usually much better off testing for a DOM element to appear – although pause-until-image can be good enough to get the job done.

Macro: Pause Until Web Page Element Exists [Example]

It would be nice if you could reliably test for page loaded in Google Chrome – and there is in fact an AppleScript property for tabs (loading) that purports to do this.

It's not perfect, but it might be good enough for a given use and is certainly worth testing:

tell application "Google Chrome"
   tell front window
      tell active tab
         loading
      end tell
   end tell
end tell

Keyboard Maestro has a token for this as well:

%ChromeReadyState%

Keep in mind that the state of things on a computer is not always obvious, and linking macro groups to certain window titles in certain apps has its pitfalls.

Getting this sort of thing to work seamlessly is often a challenge if not impossible.

-Chris

Yes I tested with or without the pause action, same result.

"based on same change" I agree this probably will be the cause. Both group settings and macro triggering are "focused window change"

Thanks for the testing. From my testing, the original move to action variables are working ok, when it triggered, it does move the mouse to the center of the KM logo. Does that not working when you test?

Totally agree, I already using that in my other macro. For this simple testing I just put the pause for quick test.

Good advice. For now I will try to control this inside of the macro, use switch action based on either title or url.

Thanks all.