Need help with a script: While a program is running, send a notification every 5 seconds

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
YousufSSyed
Posts: 2
Joined: 15 Jun 2021, 16:49

Need help with a script: While a program is running, send a notification every 5 seconds

15 Jun 2021, 16:55

I'm a noob at AHK, and after some googling, this is what I quickly threw together, but it doesn't seem to work:

Code: Select all

Loop,  ;infinite loop 
{
#IfWinActive obs64.exe ;#IfWinActive 
{
    Sleep, 5000
    TrayTip AutoHotKey, Are you recording?
    Sleep, 5000
}
}
[Mod edit: [code][/code] tags added.]

While I have OBS (obs64.exe) open, it sends a notification saying "Are you recording?"
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Need help with a script: While a program is running, send a notification every 5 seconds

15 Jun 2021, 17:23

#IfWinActive is a directive that affects whether hotkeys will be active. They do not put conditions on other lines of code in your script. Use a regular conditional statement checking whether the window is active like below. Also, you need to use ahk_exe before the process name (unless the title of the window itself is obs64.exe, which I doubt).

Code: Select all

Loop,  ;infinite loop 
{
    If WinActive("ahk_exe obs64.exe")
    {
        Sleep, 5000
        TrayTip AutoHotKey, Are you recording?
        Sleep, 5000
    }
}

By the way, since you have a Sleep, 5000 before and after, it will be approximately 10 seconds between notifications, not 5.

YousufSSyed wrote: While I have OBS (obs64.exe) open, it sends a notification saying "Are you recording?"
If you really mean whenever it’s simply open, as opposed to its window being the active window, change the above WinActive to WinExist.
YousufSSyed
Posts: 2
Joined: 15 Jun 2021, 16:49

Re: Need help with a script: While a program is running, send a notification every 5 seconds

01 Jul 2021, 16:24

This is what I got now

Loop, ;infinite loop

Code: Select all

{
    If WinExist("ahk_exe obs64.exe")
    {
        TrayTip AutoHotKey, Are you recording?
        Sleep, 5000
    }
}
Even with the script running and with OBS open, the notification doesn't show. I have a Windows Insider build of Windows 11 installed, would that cause any issues? Also, is it possible instead of

Code: Select all

("ahk_exe obs64.exe")
, it could be any string that contains the phrase

Code: Select all

obs
? Again I'm not very familiar with AHK.
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Need help with a script: While a program is running, send a notification every 5 seconds

01 Jul 2021, 17:52

YousufSSyed wrote: This is what I got now

Loop, ;infinite loop

Code: Select all

{
    If WinExist("ahk_exe obs64.exe")
    {
        TrayTip AutoHotKey, Are you recording?
        Sleep, 5000
    }
}
Even with the script running and with OBS open, the notification doesn't show.
That exact code (assuming you meant to include the Loop statement inside the code box) works for me when I have OBS running.

YousufSSyed wrote: I have a Windows Insider build of Windows 11 installed, would that cause any issues?
It's possible they no longer have the tray notifications, but I doubt it. I don't know, though, since I have not installed Windows 11. It's easy to find out, however. Just run the following script and see if a the tray notification shows up before the MsgBox does:

Code: Select all

TrayTip, Test, This is a test tray notification
Sleep, 3000
MsgBox, Did you see the tray notification?

YousufSSyed wrote: Also, is it possible instead of

Code: Select all

("ahk_exe obs64.exe")
, it could be any string that contains the phrase

Code: Select all

obs
? Again I'm not very familiar with AHK.
No, that's not how it works. But think about it -- even if it did, that wouldn't prevent the notification from showing up. It would be the opposite. It would make it more likely to show up because it could be triggered by more windows. Run the following script when you have OBS running (and when not running) and tell me what it says:

Code: Select all

MsgBox, % "OBS window was " . (WinExist("ahk_exe obs64.exe") ? "" : "NOT ") . "found."

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 370 guests