[SOLVED] Window-Title-Changed Trigger Getting Triggered an Unexpected Number of Times

Hello KM community,

I coded myself a macro that selects a substring of the front browser URL and pastes that into another application.

My problem is that it works very inconsistently. Sometimes the correct string gets pasted but other times it duplicates multiple letters in the string or pastes the result double.

Also I have noticed some interference with the "bring windows to front" action where it triggers application coded hotkeys.

So to understand what I am trying to do I will explain it with pictures here.

When I am on the website (Premarket Movers & News • Benzinga) and click on a hyperlink to a subpage on that website (the tickers displayed in blue in the table: Core Scientific Stock (NASDAQ:CORZ), Quotes and News Summary - Benzinga) I want KM to copy the ending of the URL and paste it into my charting app and then refocus on the browser window. The if:else condition is for either the Tradingview App window on my right or left additional monitor.

The Macro is only available in the Browser hence the trigger.

Following the same macro as for the right window if condition.

Now what I am having trouble with is the inaccuracy of the result of the action as described above.
When the tab is already loaded and I simply switch tabs from another tab everything works fine, you can see the string/ticker it pasted in the watermark or title of the tab (CORZ) in this screenshot.


But when I click on the hyperlink it pastes some combination of the letters in random order and composition. Picture of the Hyperlink and result below.

Like here COCROZRZ instead of CORZ like above.

I tried putting in several delays between the actions of 0.1-1s as I thought it has something to do with the URL not getting loaded completely before the Variable get filled but nothing with success so far.

EDIT:

To test if it is something with the variables not getting filled correctly I built in this check, which shows that even when I click on the hyperlink and it pastes the random string the variables are correct.

The other problem is with strings/tickers that start with a "B".

When the Tradingview window gets focused instead of pasting the ticker (which works by simply typing the the ticker and hitting enter) it triggers an in-app shortcut to open the brokerage tab instead (in app shortcut is shift+B).

Somehow the focus window action is triggering this.

Result of the app shortcut triggering - the rest of the string after the B gets pasted orderly (it should be BNTC) but also only when tab has been preloaded.

This has become a wall of text and pictures, I hope you can follow me and thanks for your help & time in advance.

Hey Daniel,

TLDR...   :sunglasses:

I scanned through, but I'm not going to bother getting specific unless it becomes necessary.

One of your troubles is almost certainly processing text:

image

These settings are available in a number of actions including the insert-text and set variable to text actions.

I suggest that you set them both to “Process Nothing”.

Personally I'd use insert text by pasting instead of typing if possible.

There's nothing to be done about the ShiftB problem unless one of the following options will work:

  • You can paste instead of type.

  • You can transform "B" to "b"

One other observation – I see you're using global variables.

Persistent variables can get you into trouble in very head-scratching ways...

You should either use local Variables or make certain to delete unnecessary global variables at the end of your macro.

Using local variables turns off some of the Keyboard Maestro Editors variable and result displays – so some people build macros with global variables and then transform them to local variables when the job is done. (There's a macro or two on the forum that makes this task a little easier.)

-Chris

1 Like

Reading through the stuff you posted and wiki trying things out.
Thanks for the answer I will report back.

So I finally found the time to read through everything you mentioned and test stuff out until I could localize the problem.

The macro gets triggered 2x when I open the page by clicking on the hyperlink instead of switching to an already fully loaded page, leading to the variable getting inserted 2x.

It seems to be related to the macro trigger but I have not yet found a solution that works.

I tried letting the macro wait for 2-5s after getting triggered so the page is fully loaded as well as waiting after the test gets inserted, the browser window gets focused again etc. with no success.

What makes me think is that the macro is getting re-triggered while the Tradingview window is focused but I thought the macro could only be triggered while Brave is at the front window with how the macro/group is set up.

I changed all the variables in the macro to instance_variable and tried around with the "process nothing" etc. text options but when I select nothing it pastes the variable name.

The problem with the hotkey getting activated was an easy one, I added filtering with lowercase to solve that after your hint.

I'm getting double activations in Safari with this simple test macro...

TEST ⇢ Focused Window Title Changed Trigger ⇢ Safari.kmmacros (3.3 KB)
Keyboard Maestro Export

The same macro configured for Google Chrome gets similar results.

This makes sense when there are redirects in the window, but try clicking on a link in a Google search.

The weird thing is that the title and url tokens end up being the same most of the time.

@peternlewis – this seems like a bug...

1 Like

I couldn't get any double activations clicking around the wiki or the forum.

However don't try to debug this by looking at the SafariTitle token, as that is taking the current value which may have changed since the macro triggered.

Use the TriggerValue token.

That should show you what the title change actually is.

2 Likes

Hey Peter,

This is after searching Google for 'Perl' and either Cmd-Clicking a link to open a new tab and activate it or Left-Click on a link to open normally.

(TriggerValue on top.)

I suspected the untitled issue, but that doesn't explain why I was getting 3 activations in Safari earlier today...

Strangely – I'm only getting two activations now.

The current behavior is definitely according to Hoyle – and Safari's behavior is the cause of the unexpected activation.

I'm not so sure about the triple activations though – if I can coax Safari into doing that again I'll capture the TriggerValue token.

-Chris

1 Like

Hey Daniel,

As you should have read by now – it would appear that the browser is at fault. I've tested with both Google Chrome and Safari.

My workaround is thus:

TEST ⇢ Focused Window Title Changed Trigger ⇢ Safari.kmmacros (5.1 KB)

Display-Macro-Image

Keyboard Maestro Export

I'm using Safari here, because that's what Peter uses.

I'm not just delighted with this solution, but it seems to be working consistently so far.

-Chris

1 Like

Hey Chris & Peter,

I have read your replies and tried to understand what you did.

So Safari is showing the tokens no matter how you open a link while it takes the front browser (any other browser) to fully load the website before the tokens get filled properly which is the problem I have with my macro at the moment.

So the macro gets triggered once when I open the new page and title goes from "existing page" to "untitled" and a second time when the title switches from "untitled" to "loaded page".

I am also trying to understand how you tested what is not working properly.

I have added your macro and tried around with it and I think I understand what you did there so far but I need to read up on the token section of the wiki to fully understand what you did with the "trigger_value" token.

In the meantime I have believed to found a way that fixes my problem with this loop:
The Macro should only trigger when the front browser URL has changed, but it seems this action doesn't process the %FrontBrowserURL% token in the statement and can only process text. Is there a way to make this work?

I will try to fully understand your macro before I get dragged to the next family event but wanted to get this out before.

Thanks for your replies so far!

You are conflating terms (tokens and triggers) here which will not help in clear understanding.

The macro is triggered when the window changes title. The TriggerValue token holds the window title at the time of the change.

The SafariTitle token asks Safari for the title of the front tab.

There is no such concept as “the token get filled properly”. Every time the SafariTitle token is used, Keyboard Maestro will ask Safari for the title of the tab.

So what is happening is:

  • The window title is changing to “untitled”.
  • The macro is executed, with TriggerValue “untitled”
  • The window title is changing to “loaded page”.
  • The macro is executed a second time, with TriggerValue “loaded page”

All of that happens very quickly.

Then both executed macros continue executing.

By the time both macros get to reading the SafariTitle token, and thus asking Safari for the title of the front page, the result is “loaded page”.

I suggest you start your macro with:

  • Assert text %TriggerValue% is not “untitled” (turning off notification).

That way the first instance will simply abort itself.

2 Likes

This solved the problem I had.

Thank you both for your thorough replies which have not only helped fix my macro but more importanty helped me a ton in understanding KM better.

2 Likes