Problem with #IfWinActive vs If WinActive
Problem with #IfWinActive vs If WinActive
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.
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.
Re: Problem with #IfWinActive vs If WinActive
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.
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.
Re: Problem with #IfWinActive vs If WinActive
I read that article, but I'll read it again with your notes in mind.
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.
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:#If directive applies at a time. Only the most recent is active. Conditions can be combined on line if needed.
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.
Re: Problem with #IfWinActive vs If WinActive
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
Re: Problem with #IfWinActive vs If WinActive
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?
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?
Re: Problem with #IfWinActive vs If WinActive
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
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
Re: Problem with #IfWinActive vs If WinActive
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?
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 (28.9 KiB) Viewed 901 times
Re: Problem with #IfWinActive vs If WinActive
Why don't you test that?Should the directive look like this? #IfWinActive ComicRack
A window's leading text will be matched with the default title match mode.
Re: Problem with #IfWinActive vs If WinActive
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.
Re: Problem with #IfWinActive vs If WinActive
You can post your revised script in a reply below. That's the drill: broken script => post it.
Re: Problem with #IfWinActive vs If WinActive
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
Re: Problem with #IfWinActive vs If WinActive
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!
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!
Re: Problem with #IfWinActive vs If WinActive
It's those gremlins that get in there-- like those darn software updates!
Re: Problem with #IfWinActive vs If WinActive
Gotcha. I'll stop feeding my computer after 12am. Thanks again!