Store a WinTitle as a variable and then compare it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Store a WinTitle as a variable and then compare it

Post by hemsith14_ » 23 Jan 2022, 15:37

Hey, I want to store a WinTitle as a variable and then compare it.

My goal is to get the title of a window that existed at the time where the script started running, and then compare it later at the end of the script. If the wintitle of the new window that has opened up is equal to the one from the beginning, do something.

Code: Select all

If WinExist("ahk_class Window")
{
Bang := True
WinTitle := Store?
}
; Do things that take some time
If (Bang = True) && ;Wintitle is the same
{
Do sometihng
}
Return
Could you please help me with doing that?
Thanks for your help.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Store a WinTitle as a variable and then compare it

Post by amateur+ » 23 Jan 2022, 15:43

Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

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

Re: Store a WinTitle as a variable and then compare it

Post by boiler » 23 Jan 2022, 15:44

WinTitle per se isn't something that is captured. The term WinTitle is different than the window's title, so you should be clear what you are looking for. There are many ways you can identify a window when you are identifying one using a WinTitle parameter. The question is what are you specifically trying to find? The hwnd is probably what you really are looking for if you want to see if the window that is now open is the same as the one from before. You can get that like this:

Code: Select all

MyHwnd := WinExist("ahk_class Window")

You can alternatively get the title of the window (not the same as WinTitle), the class name for the window, the process name for the process that created the window, or the process ID for the process that created the window. Again, you probably only need the hwnd (ID) of the window. If all you need is the actual title, then what amateur+ showed is the answer. But it would seem you still want the hwnd because you would be wanting to check if the title for the window with a particular hwnd has changed.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Store a WinTitle as a variable and then compare it

Post by hemsith14_ » 23 Jan 2022, 15:59

So if I understand correctly:

Code: Select all

If WinExist("ahk_class Window")
{
Bang := True
MyHwnd := WinExist("ahk_class Window")
}
; Do things that take some time
If (Bang = True) && (WinExist = MyHwnd)
{
Do sometihng
}
Return
Is it correct?

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

Re: Store a WinTitle as a variable and then compare it

Post by mikeyww » 23 Jan 2022, 16:05

Here is one way.

Code: Select all

class = Notepad
WinGetTitle, winTitle, ahk_class %class%
Sleep, 3000
If WinExist(winTitle " ahk_class " class)
 MsgBox, 64, Status, Found!`n`n%winTitle%

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

Re: Store a WinTitle as a variable and then compare it

Post by boiler » 23 Jan 2022, 16:09

hemsith14_ wrote:
23 Jan 2022, 15:59
So if I understand correctly:

Code: Select all

If WinExist("ahk_class Window")
{
Bang := True
MyHwnd := WinExist("ahk_class Window")
}
; Do things that take some time
If (Bang = True) && (WinExist = MyHwnd)
{
Do sometihng
}
Return
Is it correct?
No, that is not correct. What are you expecting the variable WinExist to contain in (WinExist = MyHwnd)? If anything, you probably meant that to be another call to the WinExist() function. You can't just put WinExist in that place to do that because then you are just checking the value in MyHwnd against the value in the variable WinExist which hasn't been assigned any value.

You still haven't explained what you are trying to accomplish. Are you trying to see if the same window is there but just that its title has changed? Or are you trying to see if it has created a new window that may have the same title as before?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Store a WinTitle as a variable and then compare it

Post by hemsith14_ » 23 Jan 2022, 16:17

Trying to see if it has created a new window that may have the same title as before.

Thank you very much @mikeyww !

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Store a WinTitle as a variable and then compare it

Post by hemsith14_ » 23 Jan 2022, 16:24

Actually I think I might have found a better way to do this.

I need a function like:

Code: Select all

If WinExist ; and is in a certain range of the screen
{
WinMove
}
Is it possible?

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

Re: Store a WinTitle as a variable and then compare it

Post by boiler » 23 Jan 2022, 16:38

hemsith14_ wrote:
23 Jan 2022, 16:17
Trying to see if it has created a new window that may have the same title as before.
Then you need to be comparing the old hwnd to the new hwnd for the window found with the same title.

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

Re: Store a WinTitle as a variable and then compare it

Post by boiler » 23 Jan 2022, 16:39

hemsith14_ wrote:
23 Jan 2022, 16:24
Actually I think I might have found a better way to do this.

I need a function like:

Code: Select all

If WinExist ; and is in a certain range of the screen
{
WinMove
}
Is it possible?
Use WinGetPos to find out where it is located.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Store a WinTitle as a variable and then compare it

Post by hemsith14_ » 23 Jan 2022, 16:44

Thanks for your help

Post Reply

Return to “Ask for Help (v1)”