How to use #If or #IfWinActive with programs and websites in the same time. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ahmed142
Posts: 19
Joined: 19 May 2022, 08:55

How to use #If or #IfWinActive with programs and websites in the same time.

Post by Ahmed142 » 27 Nov 2022, 05:00

Hello everyone,

I asked previously a question in this topic viewtopic.php?f=76&t=110906 and thanks for mikeyww he answered my question, but today I want to ask similar question but with more details, how to use #If or #IfWinActive with programs, websites, and Program Manager for the case that I'm in the desktop, so I tried to create a script that show MsgBox If one of the following programs or websites is activated:

1. Word
2. Excel
3. https://drive.google.com/
4. https://www.autohotkey.com/
5. Program Manager

For this, it could be the first line of the script is somehow long, but here is in the following what I tried:

Code: Select all

#If WinActive("ahk_exe WINWORD.exe") || WinActive("ahk_exe EXCEL.exe") || WinActive("https://drive.google.com/") || WinActive ("https://www.autohotkey.com/") || WinActive Program Manager

F4::
MsgBox, Hello
Return

#IfWinActive
I found the problem with this code that it works not only for the above programs and websites but it works also for other websites and programs so it seems this code work at everywhere.

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

Re: How to use #If or #IfWinActive with programs and websites in the same time.  Topic is solved

Post by mikeyww » 27 Nov 2022, 07:29

WinActive is a function. That means three things.

1. The function name is always followed by parentheses.
2. The function name itself should always be immediately followed by ( rather than a space.
3. Whenever you are using literal strings, such as for the leading text of a window title, quote the string inside the parentheses.

Explained: Expressions

Many Web browsers use the page title rather than the URL in the window title. Running Window Spy can help you to confirm your window titles when needed. Just because the URL appears on the page does not mean that it is the window title.

Ahmed142
Posts: 19
Joined: 19 May 2022, 08:55

Re: How to use #If or #IfWinActive with programs and websites in the same time.

Post by Ahmed142 » 27 Nov 2022, 12:00

mikeyww wrote:
27 Nov 2022, 07:29
WinActive is a function. That means three things.

1. The function name is always followed by parentheses.
2. The function name itself should always be immediately followed by ( rather than a space.
3. Whenever you are using literal strings, such as for the leading text of a window title, quote the string inside the parentheses.

Explained: Expressions

Many Web browsers use the page title rather than the URL in the window title. Running Window Spy can help you to confirm your window titles when needed. Just because the URL appears on the page does not mean that it is the window title.
Thank you so much mister mikeyww

Really that is useful information for me

After this, maybe the following code con be converted to something useful:

Code: Select all

#If WinActive("ahk_class Notepad++") || WinActive("Program Manager") || WinActive("My Drive - Google Drive")
F8::
MsgBox, Hello
Return
#IfWinActive


Ahmed142
Posts: 19
Joined: 19 May 2022, 08:55

Re: How to use #If or #IfWinActive with programs and websites in the same time.

Post by Ahmed142 » 27 Nov 2022, 13:37

mikeyww wrote:
27 Nov 2022, 13:16
Maybe. Is it working?
Yes, I tired that and it works for me

I use AutoHotKey version 1.1.35.00

Post Reply

Return to “Ask for Help (v1)”