Everything you do with window, keyboard or mouse automation is prone to race conditions and conflicts, since forces outside the script, like the user himself, can affect the state of the window.
"A" is no different to any other window specification in that respect.
NOTE: "A" intentionally does not match the foreground window if it is hidden and DetectHiddenWindows is off. Before assuming that "A" is unreliable, be absolutely sure you have DetectHiddenWindows on. It is not sensible for PostMessage or any other function which isn't WinWaitActive to wait for a window to become active
and visible.
For example, if some other AutoHotkey script is showing a Menu, it probably has its main window active, and that window is probably hidden.
iseahound wrote:It's easy to write logic for when ahk_class notepad isn't found, but it's harder to deal with "A"
I don't agree that it's any different, or difficult. It always depends on which window the author or user intended to interact with. For your example, I imagine the user's intention would be to close the window that
he thought was active. If GetForegroundWindow returned NULL because focus is transitioning, whatever window focus is transitioning to is almost definitely not the one the user thought was active. For your example, the only sensible fallback would be to
do nothing. That is easily achieved with three letters (and a space):
try.
Regardless of what value WinTitle has, it doesn't make sense to design an automatic countermeasure for unexpected failure to find the window as the intention of the code and user cannot be known. There are also very many functions which use WinTitle, and the ideal reaction to failure likely differs between them.
Falling back to the taskbar (or probably any window) would be nonsense. What can you even do to the taskbar, that you would want any functions to operate on it as a fallback? If the effect of the function is really so unimportant that the user is happy with it operating on whatever window, surely the function could just do nothing and the user would be just as happy. Probably happier, because he hasn't just closed an important window that just unexpectedly popped up, or
accidentally shut down Windows. (Sending the SCCLOSE command to the taskbar generally shows the Windows Shutdown dialog. If the user happens to be about to press Enter...)
Descolada wrote:which should mean that it is guaranteed that the message is sent to an actual window.
How can it guarantee that, when the last found window might have been
destroyed?
Descolada wrote:Previously it was that the taskbar was returned in such cases
Do you mean that GetForegroundWindow returned the taskbar while a transition was occurring? (I'm not saying that it was so, just trying to guess what you mean.) If it was so, it could have been because the taskbar
actually was activated during certain transitions. "Previously it was" implies that it doesn't happen anymore; was that on older OS versions?
iseahound wrote:I don't see why even catching a TargetError could even possibly be a solution to this problem, when the WinTitle could be an ahk_id, throwing a legitimate TargetError.
Explain how this makes any sense. If you want to know what window criteria the function failed to resolve to a window, you need only look at the values you gave the function in the first place. The discussion was about functions which are being called to operate on the active window. There is
no ambiguity about what a thrown TargetError would mean if PostMessage is the only call within the try-catch.