Just out of curiosity, are you triggering this macro while the Scrivener application is at the front? Or when it is not at the front? Because the circled part of your macro indicates that the macro group only works when you are already in Scrivener. Therefore, if you are trying to use this to bring that window to the front while not only it, but the entire application is in the background, then it won’t work. You would need to change the macro group settings to “available when Scrivener is running” or something to that effect.
As far as I know, and I really don't know much about this, there are different kinds of "windows." I just tried this, using Safari, and what it did was return the "tab" in the frontmost window of Safari, but it did not find the tab if it was in the secondmost window.
Try putting these actions at the beginning of your macro. The alert will show you the trigger information which will confirm the macro is actually executing, and the debugger will show you more information as the macro proceeds.
Additionally, ensure the macOS permissions have been enabled, and try resetting them if in doubt.
It is almost certainly the window itself. It is a special floating window from Scrivener that is not even recognized by AltTab. But it's still strange that KM recognizes it when the cursor is in it, so when it's the front window.