How to check if a control or object exists with AutoHotkey v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
veto67
Posts: 1
Joined: 09 Aug 2022, 22:49

How to check if a control or object exists with AutoHotkey v2

Post by veto67 » 10 Aug 2022, 00:03

How to check if a control or object exists with AutoHotkey v2?
In v1 I can find IfWinExist and more, but in v2 not.
could somebody please give me a hint?

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: How to check if a control or object exists with AutoHotkey v2

Post by AHK_user » 10 Aug 2022, 07:13

True, I cannot directly find a native function.
You can build it with ControlGetHwnd so it reacts like WinExist.

Code: Select all

ControlExist(Control, WinTitle:="", WinText:="", ExcludeTitle:="", ExcludeText:=""){
    try 
        return ControlGetHwnd(Control, WinTitle, WinText, ExcludeTitle, ExcludeText)
    catch 
        return false
}

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: How to check if a control or object exists with AutoHotkey v2

Post by boiler » 10 Aug 2022, 12:41

veto67 wrote: In v1 I can find IfWinExist and more, but in v2 not.
To answer this specific part of your post, even the documentation for v1 says not to use IfWinExist anymore and directs you to WinExist. And v2's WinExist is exactly the same. The equivalent of IfWinExist, ... is if WinExist("...")

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: How to check if a control or object exists with AutoHotkey v2

Post by AHK_user » 10 Aug 2022, 13:23

I find it actually quite strange that you have different indications for ControlGetHwnd and WinGetID, as ID and Hwnd are the same things, It would be logically to use everywhere the name ID (shorter and as Hwnd means Handle to a Window, it seems more logically to use ID that is short and can be used for controls and windows)

So ControlGetID, GuiFromID and GuiCtrlFromID would be a more logical function names.
And I would also change the hwnd property to the name ID.


Post Reply

Return to “Ask for Help (v2)”