Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
redrum
Posts: 83
Joined: 25 Jan 2015, 22:57

Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

24 Jun 2021, 20:14

I'm trying to set up a hotkey to toggle an app window. The app is thinkorswim. This code sort of works.

Code: Select all

; #IfWinExist Main@thinkorswim
!+w:: ; ALT+SHIFT+w
SetTitleMatchMode, 1 ; 
If WinActive(wTitle := "Main@thinkorswim [build ")
 WinMinimize
Else WinActivate, %wTitle%
Return
; #IfWinExist
The issue is that I am running multiple instances of the app across multiple virtual desktops (Windows 10) - one instance per virtual desktop. Each instance has a "Main" window, with window title "Main@thinkorswim [build XXXX]" (the build numbers change every week or so as the app auto-updates). The build numbers are the same for each instance of the app.

In virtual desktop #1, when I use the hotkey, the virtual desktop changes to another virtual desktop and the thinkorswim Main window (I'll refer to this as TOS Main) is the active window. If I start in in virtual desktop #1 and click on TOS Main, then press the hotkey, TOS Main gets minimized (good!). But if press the hotkey again, it switches to another virtual desktop and makes TOS Main instance in that desktop active. Once the script has switched me to another desktop, then the hotkey toggles the TOS Main window in that desktop as expected.

So the script can correctly identify the window, but not the desired instance of the window. Is there a way to restrict the script to a single virtual desktop (either the current virtual desktop, or a specified number virtual desktop)?
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

24 Jun 2021, 20:35

Something like the following might work.

Code: Select all

GroupAdd, win, ahk_exe notepad.exe
#IfWinExist ahk_exe notepad.exe
!+w::
If WinActive("ahk_exe notepad.exe")
 WinMinimize
Else GroupActivate, win
Return
#IfWinExist
redrum
Posts: 83
Joined: 25 Jan 2015, 22:57

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

25 Jun 2021, 20:42

Thanks for pointing me to GroupAdd. Here's what I came up with - it's a two hotkey solution. First I have to manually make the desired TOS Main window active (as noted earlier, there are multiple TOS Main windows, each in a different Windows virtual desktop), then run the first hotkey:

Code: Select all

!+.:: ; ALT+SHIFT+.
WinGet, active_id, ID, A
GroupAdd, tos, AHK_id %active_id%
Return
Once the window's ID is captured, this hotkey is no longer needed until the app is restarted (once every couple of days).

To toggle the TOS Main window active or minimized, I use the second hotkey:

Code: Select all

!+w:: ; ALT+SHIFT+w
SetTitleMatchMode, 1 ; 
If WinActive("Main@thinkorswim [build ")
 WinMinimize
Else GroupActivate, tos
Return
Not the prettiest solution, as it does involve manually selecting the window first, but it works. Suggestions on how to make this more elegant are welcome.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?  Topic is solved

25 Jun 2021, 20:58

Another example.

Code: Select all

#IfWinExist ahk_exe notepad.exe
!+w::
If WinExist(wTitle) && wTitle {
 If WinActive(wTitle)
  WinMinimize
 Else WinActivate
} Else {
 active := WinActive(w := "ahk_exe notepad.exe")
 WinActivate, %w%
 wTitle := "ahk_id " WinActive("A")
 If active
  WinMinimize
}
Return
#IfWinExist
redrum
Posts: 83
Joined: 25 Jan 2015, 22:57

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

25 Jun 2021, 21:40

Because the early part of this code is using only wintitle, and not a unique identifier, I think it will have the same issue as the code in the OP - namely that AHK will switch Windows virtual desktops to act on one of the multiple instances of TOS Main, which all share the exact same wintitle. The window ID's are unique, which is why my current solution requires me to manually select the TOS Main window I want, grab its ID using winget, and then I'm golden from there, until the next time I exit and relaunch the TOS app.

Code: Select all

#IfWinExist ahk_exe notepad.exe
!+w::
If WinExist(wTitle) && wTitle {
 If WinActive(wTitle)
  WinMinimize
 Else WinActivate
} Else {
 active := WinActive(w := "ahk_exe notepad.exe")
 WinActivate, %w%
 wTitle := "ahk_id " WinActive("A")
 If active
  WinMinimize
}
Return
#IfWinExist
[Mod edit: from quote to [code][/code] tags.]
redrum
Posts: 83
Joined: 25 Jan 2015, 22:57

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

29 Jun 2021, 14:33

OK, I used the following hotkey script:

Code: Select all

#IfWinExist ahk_exe thinkorswim.exe
!+w::
If WinExist(wTitle) && wTitle {
 If WinActive(wTitle)
  WinMinimize
 Else WinActivate
} Else {
 active := WinActive(w := "Main@thinkorswim")
 WinActivate, %w%
 wTitle := "ahk_id " WinActive("A")
 If active
  WinMinimize
}
Return
#IfWinExist
If I select the TOS Main window (from the instance of TOS I want; running multiple instances of TOS), and then hit the hotkey, this works perfectly. If another TOS window (I have multiple TOS windows for each instance of TOS that is running) or a different app is active at the time I first hit the hotkey, then the script will grab some seemingly random TOS window's ahk_id and that will be the TOS window that gets toggled.

The net effect is that this essentially combines the two earlier scripts, so now with one hotkey, I can retrieve the ahk_id for the TOS Main window and toggle that window. As with the earlier two script solution, the desired TOS Main window has to be selected prior to first usage of the hotkey following each restart of the TOS application. So this is an improvement thanks, and thanks for showing me various techniques in AHK.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

29 Jun 2021, 14:50

OK. It sounds like at least a slight improvement. I'm not sure how else you would save the identity of your particular window, unless it always has a unique WinTitle or some other unique property that distinguishes it from the other windows. If it does, then you could use an INI file to store that information and then retrieve it later.
redrum
Posts: 83
Joined: 25 Jan 2015, 22:57

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

29 Jun 2021, 20:39

mikeyww wrote:
29 Jun 2021, 14:50
OK. It sounds like at least a slight improvement. I'm not sure how else you would save the identity of your particular window, unless it always has a unique WinTitle or some other unique property that distinguishes it from the other windows. If it does, then you could use an INI file to store that information and then retrieve it later.
I did a little more checking on this. The only unique property I'm seeing is the ahk_id, *but* each instance of TOS app has a unique ahk_pid and each instance of TOS has only one window with title Main@thinkorswim. So a combination of ahk_pid and title would identify a unique window.

But the ahk_pid's are different each time an app is launched, so I'd still have to tell the script which instance I want.

One possibility - if I could limit the window search to windows on a particular Windows virtual desktop, and search for title Main@thinkorswim, that would identify a unique window, since the other TOS Main windows are each in their own virtual desktop.
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Window title matching with multiple instances across multiple virtual desktops - restrict search to one desktop?

29 Jun 2021, 21:15

OK. I do not use virtual desktops, but others may know, or you could search the forum.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doanmvu, RSable and 392 guests