Trying to combine 2 autohotkey scripts

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
brav
Posts: 12
Joined: 16 Apr 2019, 13:08

Trying to combine 2 autohotkey scripts

Post by brav » 08 May 2019, 01:00

I'm trying to combine 2 autohotkey scripts: one that does loops to close popups (I've tried settimers and it seems looping doesn't break as easily when I add extra conditions), and the other script has my hotkeys and hotstrings. Would an ideal way be this order where I can still have my hotstrings work while the loop closes popups? I use WinWaitActive type of functions in both scripts, and want both to function independent of each other.

Within ClosePopups.ahk:

Code: Select all

Loop ;That looks for popup windows and closes them
{
  IfWinExist
  close popup functions
}

#Include Hotkeys.ahk ;This contains hotkey and hotstrings
If I put the #Include Hotkeys.ahk before the popup closing loop, then some of the WinWait functions in Hotkeys.ahk will pause the popup loop too. Thanks for your help!

[Mod note: Topic name and code tags added.]

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trying to combine 2 autohotkey scripts

Post by Osprey » 08 May 2019, 01:33

If your WinWaits in the hotkeys script are just used to determine when the hotkeys trigger, you should use the #If directive, instead. For example:

Code: Select all

#If WinActive("My App")
; Put hotkeys for My App here

#If WinActive("My Other App")
; Put hotkeys for My Other App here
Those can be safely merged into your loop script.

brav
Posts: 12
Joined: 16 Apr 2019, 13:08

Re: Trying to combine 2 autohotkey scripts

Post by brav » 09 May 2019, 11:52

I tried changing most of my script to #IfWinActive, but I'm also using GroupAdd (had to exclude some Wintext in my #IfWinActive clauses, and I think that has to be done through ahk_group) which is in my non loop script.
So I had my non-loop script be the parent so I can add GroupAdd on top, then had the #Include persistent loop script (script that closes out popup boxes) be the first line right under my GroupAdd. The loop works sometimes, but fails on others.

I'm wondering if I have to try using "Critical" inside the persistent loop to make sure it stays active at all times. Will try and report back.

Edit: Nm..that prevents any other script from running.

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trying to combine 2 autohotkey scripts

Post by Osprey » 09 May 2019, 18:24

I, personally, would use #If WinActive() instead of #IfWinActive. It uses expression syntax, which is forward planning, and the WinActive() function supports variables and the ExcludeTitle and ExcludeText parameters.

BTW, when you use #Include, the included script is merged at that place and AHK treats it all as one big script. Therefore, there's no "parent" script and you should think of it as parts of a single script, not as separate scripts.
Last edited by Osprey on 09 May 2019, 23:25, edited 1 time in total.

gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Trying to combine 2 autohotkey scripts

Post by gregster » 09 May 2019, 20:00

Osprey wrote:
09 May 2019, 18:24
I, personally, would use #If WinActive() instead of #IfWinActive. It uses expression syntax, which is forward planning, and the WinActive() function supports ExcludeTitle and ExcludeText parameters.
In AHK v1.1, I would actually still prefer #IfWinActive over #If WinActive(), if the use case allows it - because they work differently, and the expression is a potential disadvantage in simple calls that could have otherwise been done with the #IfWin directives.

Basically, it comes down to a potential performance issue:
#IfWin:
https://autohotkey.com/docs/commands/_IfWinActive.htm#gen wrote:For performance reasons, #IfWin does not continuously monitor the activation or existence of the specified windows. Instead, it checks for a matching window only when you type a hotkey or hotstring. If a matching window is not present, your keystroke or mouse click is allowed to pass through to the active window unaltered.
versus #If:
https://autohotkey.com/docs/commands/_If.htm#General_Remarks wrote:Note: Scripts should not assume that the expression is only evaluated when the key is pressed (see below).
The expression may also be evaluated whenever the program needs to know whether the hotkey is active. For example, the #If expression for a custom combination like a & b:: might be evaluated when the prefix key (a in this example) is pressed, to determine whether it should act as a custom modifier key.
Note: Use of #If in an unresponsive script may cause input lag or break hotkeys (see below).
(There are more caveats for #If mentioned in https://autohotkey.com/docs/commands/_If.htm#General_Remarks)

In many cases it might be irrelevant, but probably not always... of course, #If is much more flexible, no doubt.

In AHK v2 on the other hand, it seems that the only remaining #If-directive is now specifically optimized against such potential performance penalties :thumbup: :
https://lexikos.github.io/v2/docs/commands/_If.htm#optimization wrote:#If is optimized to avoid expression evaluation for simple calls to WinActive or WinExist, thereby reducing the risk of lag or other issues in such cases. Specifically:
The expression must contain exactly one call to WinExist or WinActive.
[...]

Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trying to combine 2 autohotkey scripts

Post by Osprey » 09 May 2019, 21:57

I didn't realize that, gregster. Thanks for pointing that out. Unfortunately, it doesn't do my scripts any good because I use expressions and variables and can't convert my #If WinActive()s to #IfWinActive, but, for simple scripts, it might help (then again, for simple scripts, the performance improvements might be somewhat pointless).

Post Reply

Return to “Ask for Help (v1)”