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

I am trying to make a macro only to be triggered when it is in Chrome and title contains "keyboard maestro".

In the macro group setting, I set it only available in google chrome and only when a focused window title contains "keyboard maestro". However it is not working as expected.

In chrome browser, I have two tabs open, one is this km forum and the other is google.com.

If I switched from another application (say finder) to chrome, macro triggers if the active tab is km and doesn't trigger if this active tab is google.com. This works as expected.

However if I just switch tabs within chrome browser, this is not working. when I switched from km tab to google.com tab, this macro triggered and displayed google.com url and title. (screenshot attached).

When I switched from google.com tab to km forum tab, it doesn't trigger at all.

Did I miss something?

Thanks!

kmtest.kmmacros (57 KB)

I'm not very experienced with these features, so I won't tell you how to fix it, but I think I found the problem. In your group's properties you said that macros in this group should only be active when the string "keyboard maestro" is contained in the title. But when you tested it, you were switching tabs from "google.com" to KM. So it isn't active when this switch occurs.

Also, I'd like to point out that your problem says you want to trigger it when the title contains "keyboard maestro" but you aren't testing for that. You are testing for the KM image. This isn't the same thing. In fact, changing zoom levels (and other conditions) will cause your current macro to fail. Why not test the URL instead of testing for a graphic?

I will assume your use of "MidX" works, because someone on another page today explained why it works when the KM documentation contradicts how KM works.

By the way, you don't really need to test if myimg is empty if you want to move the mouse to the position it indicates. Instead, you can omit the IF statement and modify the Move Mouse action to turn off its error flags. The choice is yours.

And you have a pause statement for 0.6 seconds. You can also delete that if you modify your Find Image action to be in a loop with a 0.6 second timeout. That way, if the image is found in under 0.6 seconds, it immediately returns with the correct result, rather than waiting the full 0.6 seconds.

Also, I recommend that you change "in all screens" to "in front window" which could prevent false positive matches that occur elsewhere on the screen.

Thanks for the response and all the suggestions!

The main problem I have is the macro group setting. I think what I have set is to make sure that the macro is only triggered when the front window/tab contains "keyboard maestro", which isn't working for me. When I switch tabs from google.com to KM, it means that active tab is KM and it should trigger it. And this is only not working when I switch tabs from Chrome. If I am switching from any other app to chrome, it worked flawless.

Yes the MidX and MidY stuff worked too.

I am using this to test features that I can use in other real pages that I am interested in. First I want to this macro to only be triggered in Chrome browser with title contains 'xxx', then I test if an image exists (both light and dark theme as we discussed in Find an Image on Both Light and Dark Themes - #12 by gglick), if image found, then click the center of the image and enter some text (the center of image is textbox) and then press enter.

Well, if I'm going to troubleshoot this for you I have to change Chrome to Safari, because I refuse to install that evil beast. Also, I'm going to remove the PAUSE statement because it's functionally useless and it introduces testing problems because I have to pause for at least 0.6 seconds otherwise the macro will not realize I'm switching tabs twice, and I will get the wrong result. You may not have considered that issue.

EDIT: Okay I tested it by removing the PAUSE and by removing the string "keyboard maestro" and it seems to work 100%. It triggers either when I change the app or when I change the tab. Unless I misunderstand your requirements, that solved the problem.

1 Like

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")?