Problem with #IfWinActive vs If WinActive

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Problem with #IfWinActive vs If WinActive

Post by mrmech » 24 Nov 2022, 17:48

I have one big AHK script that runs a bunch of triggers for several programs. For years I've been using #IfWinActive ahk_exe followed by the exe name found in the Window Spy. But, for some reason, my scripts stopped working a couple of days ago. Anything NOT following a #IfWinActive worked fine.

Now, #IfWinActive works for#IfWinActive ahk_exe chrome.exe but not for #IfWinActive ahk_exe ComicRack.exe. If (WinActive("ComicRack")) does work, but it seems to only work for the trigger directly beneath it. I should note that ComicRack hasn't been developed for years and obvious Chrome is still active.

I read that IfWinActive is depreciated, does the same go for #IfWinActive? Is it possible that #IfWinActive works with some programs, but not others. Should I switch all of my window triggers to If (WinActive("Example"))? If I do that, do I have to group every trigger for an app with braces {} ?

I guess I'm asking how do I run a single AHK script with several triggers per application and multiple applications?
For example, Chrome (Chrome Trigger#1, Chrome Trigger #2, etc) ComicRack (ComicRack Trigger#1, ComicRack Trigger #2, etc).

Thanks for any help and sorry for the rambling post.

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 24 Nov 2022, 17:56

Have you examined any documentation?

https://www.autohotkey.com/docs/commands/_IfWinActive.htm#variant

#If is not If. Feel free to post a script in a reply.

Tips:
1. Programs sometimes change their process names when they are upgraded. It's up to the author (vendor). Running Window Spy will reveal the WinTitle.
2. #If is used only for hotkeys & hotstrings.

A window can have a window title of ComicRack while also having a different process name. A window title is not a process name.

One #If directive applies at a time. Only the most recent one is active. Conditions can be combined on one line if needed.

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 25 Nov 2022, 10:06

I read that article, but I'll read it again with your notes in mind.
#If directive applies at a time. Only the most recent is active. Conditions can be combined on line if needed.
The above quote pinpoints the core issue that I'm struggling with: How do I group functions by the applications that triggers them. I imagine it looking something like this:

Code: Select all

COMICRACK:                   ;{
If (WinActive("ComicRack"))
^+!A:: msgbox ComicRack Only Function A
^+!B:: msgbox ComicRack Only Function B
;}
CHROME:                      ;{
If (WinActive("Chrome"))
^+!A:: msgbox Chrome Only Function A
^+!B:: msgbox Chrome Only Function B
;}
ETC:                      ;{
If (WinActive("ETC"))
^+!A:: msgbox ETC Only Function A
^+!B:: msgbox ETC Only Function B
;}

I use the Comment Braces for code folding in Notepad++ which makes navigating the entire AHK script a lot easier. I want to keep everything in 1 script because it's just easier for me.

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 25 Nov 2022, 10:13

Yep, because the answer is right on that page.

Code: Select all

#IfWinActive ahk_exe ComicRack.exe
^+!a:: msgbox ComicRack Only Function A
^+!b:: msgbox ComicRack Only Function B

#IfWinActive ahk_exe chrome.exe
^+!a:: msgbox Chrome Only Function A
^+!b:: msgbox Chrome Only Function B
#IfWinActive

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 08:53

That's basically what I had before I made the original post except I wasn't using the #IfWinActive at the end, thanks for that tip.

Regardless, it still doesn't work with ComicRack but it works fine with Chrome. I tried reinstalling ComicRack but that didn't help. I've had similar issues with the application but they're usually solved by restarting ComicRack and sometimes by restarting my computer.

if (WinActive("ComicRack")) does work, but, as you explained, that doesn't help me group my functions by application. Do you have any ideas about how else I could troubleshoot this?

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 26 Nov 2022, 08:56

The script that I posted worked when I tried it. A different script may yield different results.

You can run Window Spy to examine the WinTitle closely.

"Basically what I had" => "My script was different" :)

Test the script exactly as posted, with no other code, and no other scripts running.

You can use Comic Rack as a WinTitle if you wish. It would work for any window whose leading text matches that exactly (case-sensitive).

#If is not If. They are never interchangeable.

Explained: #IfWinActive

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 09:57

Sorry, I should have explained that I'm copying the ahk_exe from the Window Spy. I attached a screenshot (I added the right one)
I should have also explained that isolated your version in a script with no other code and I closed my other AHK scripts but it still failed in ComicRack.

I also tried using WinTitle but I think I don't understand the syntax.

I see this in the article: #IfWinActive [WinTitle, WinText]
Should the directive look like this? #IfWinActive ComicRack

Do I need to add a SetTitleMatchMode?
Attachments
AHK Window Spy - ComicRack.png
AHK Window Spy - ComicRack.png (28.9 KiB) Viewed 901 times

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 26 Nov 2022, 10:14

Should the directive look like this? #IfWinActive ComicRack
Why don't you test that?

A window's leading text will be matched with the default title match mode.

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 10:19

That failed also. I also tried ahk_class and ahk_pid
Last edited by mrmech on 26 Nov 2022, 10:20, edited 1 time in total.

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 26 Nov 2022, 10:19

You can post your revised script in a reply below. That's the drill: broken script => post it.

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 10:21

Cool. I'll do that from now on.

Code: Select all

#IfWinActive ComicRack
^+!a:: msgbox ComicRack Only Function A
^+!b:: msgbox ComicRack Only Function B

#IfWinActive ahk_exe chrome.exe
^+!a:: msgbox Chrome Only Function A
^+!b:: msgbox Chrome Only Function B
#IfWinActive


mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 10:56

Nailed it! It's working now AND it fixed another big problem I was having. Thanks for sticking with me on this. I knew it was a bigger problem than the script itself.

Do you have any ideas why it might have stopped working? This happened around the same time I was having some general problems with Windows 10. I thought reinstalling Windows would fix the problem but it didn't.

Thanks again!

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Problem with #IfWinActive vs If WinActive

Post by mikeyww » 26 Nov 2022, 11:01

It's those gremlins that get in there-- like those darn software updates!

mrmech
Posts: 81
Joined: 07 Dec 2015, 17:37

Re: Problem with #IfWinActive vs If WinActive

Post by mrmech » 26 Nov 2022, 12:37

Gotcha. I'll stop feeding my computer after 12am. Thanks again!

Post Reply

Return to “Ask for Help (v1)”