how to check if toast notification exists

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
redrum
Posts: 95
Joined: 25 Jan 2015, 22:57

how to check if toast notification exists

Post by redrum » 19 Apr 2024, 16:03

I tried the following, but it does not see the toast notification, even though WindowSpy does, which is where I got the title and ahk_exe.

Code: Select all

SetTitleMatchMode 2
if WinExist("New notification ahk_exe ShellExperienceHost.exe")
{ do something }
else MsgBox("window not found")
Also tried using the title by itself, and that's also not found.

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

Re: how to check if toast notification exists

Post by mikeyww » 19 Apr 2024, 16:17

I did not test this, but WinExist does not wait for a window to appear. Instead, this is an instantaneous check. To wait for a window, use :arrow: WinWait.

If SetTitleMatchMode is not used, the default match mode is 2.

Assessing WinExist does not require any If statements. Instead, the value returned by WinExist can be displayed.

redrum
Posts: 95
Joined: 25 Jan 2015, 22:57

Re: how to check if toast notification exists

Post by redrum » 19 Apr 2024, 16:29

mikeyww wrote:
19 Apr 2024, 16:17
I did not test this, but WinExist does not wait for a window to appear. Instead, this is an instantaneous check. To wait for a window, use :arrow: WinWait.
In this particular use case, the window, or rather, toast notification, does already exist.
mikeyww wrote:
19 Apr 2024, 16:17
Assessing WinExist does not require any If statements. Instead, the value returned by WinExist can be displayed.
Value is zero if I use

Code: Select all

MsgBox(WinExist("New notification ahk_exe ShellExperienceHost.exe"))

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

Re: how to check if toast notification exists

Post by mikeyww » 19 Apr 2024, 17:50

Code: Select all

#Requires AutoHotkey v2.0
If !Instr(A_AhkPath, 'UIA') && !((cLine := DllCall('GetCommandLine', 'str')) ~= ' /restart(?!\S)')
 If FileExist(ahk := A_ProgramFiles '\AutoHotkey\v2\AutoHotkey64_UIA.exe') {
  Try Run ahk ' /restart ' RegExReplace(cLine, '.+\.exe.*? ')
  ExitApp
 } Else MsgBox 'File not found.`n`n' ahk, 'Error', 48
TrayTip 'Multiline`nText', 'My Title', 'Iconi Mute'
WinWait 'New notification ahk_exe ShellExperienceHost.exe'
MsgBox

Post Reply

Return to “Ask for Help (v2)”