Fix unset argument behavior on various functions. #353
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR covers two related changes.
The first pertains to the
WinTitle, WinText, ExcludeTitle, ExcludeText
keys inWinWait
. Both the v1 and v2 docs forWinWait
specify that "At least one of these is required." As far as I can tell this is a unique requirement of this function.The problem is that for the purposes of this function AHK treats empty strings as valid input, which means a call to
WinWait
with blank or missing arguments (which default to blank) and no timeout will result in an infinitely stuck script.My solution is to raise a
ValueError
in the engine code to catch this ahead of time. Alternatively, a fix could be implemented in the daemon files by using theunset
keyword in v2, however v1 does not support this and would instead require (to the best of my limited knowledge) 16 if statements to cover every combination of empty strings being excluded.The second pertains to functions that make use of controls, namely
AHKControlClick
,AHKControlGetText
,AHKControlGetPos
, andAHKControlSend
. These functions accept optional control arguments. A control can be a string, integer, or an object. Objects clearly aren't supported due to the encoding method, but the same goes for integers.Integers become strings during encoding and while AHK can mostly deal with stringified numbers during operations like math, as input for a control it instead interprets it as a string. So if you use the HWND of a window, you're using "12345", not 12345, and the window fails to be found.
For v2 I have fixed this by changing the instances of
ctrl := args[1]
toctrl := IsNumber(args[1]) ? Number(args[1]) : args[1]
which uses the v2 specificIsNumber
function to check if the number is numeric, and if so, type cast it, otherwise it stays as a string.For v1, no changes are necessary. The documentation for the functions specify that to use a HWND you would instead use the
WinText
field with aahk_id
prefix.Additionally,
AHKControlClick
specifically has a bug that is already mitigated inAHKControlSend
, and that is the handling ofctrl
when unset. In v1, a blank or omittedctrl
delegates it to the target window's topmost control, so no changes are needed.However in v2 this only occurs for an omitted control, making a blank control valid input. In
AHKControlSend
you check ifctrl
is blank and if so, call the underlying function without it.AHKControlClick
is missing this. My proposed fix (for both, modifying the current fix) is to instead use the v2 specificunset
keyword by passing the underlying functionctrl || unset
which is equivalent to not providing the value at all if a blank input is provided.