Activate most recently active window in a group with autohotkey - autohotkey

I am making an autohotkey script to switch to a text editor, but depending on what I'm working on I may have different text editors.
I created a group TextEditor with my three text editors:
GroupAdd, TextEditor, ahk_class Notepad++
GroupAdd, TextEditor, ahk_exe Code.exe
GroupAdd, TextEditor, ahk_class Notepad
The group seems to be created properly but whenever I use GroupActivate, TextEditor, R it always opens Nodepad++ and repeated presses switch between the three editors.
I want it to open whatever I last used first, like if I was using Code and switched to another program it should activate Code again.

GroupAdd, TextEditor, ahk_class Notepad++
GroupAdd, TextEditor, ahk_exe Code.exe
GroupAdd, TextEditor, ahk_class Notepad
F1::
IfWinActive ahk_group TextEditor
GroupActivate, TextEditor, R
else
{
list := ""
; windows are retrieved in order from topmost to bottommost:
WinGet, id, list, ahk_group TextEditor
Loop, %id%
{
this_ID := id%A_Index%
WinActivate, ahk_id %this_ID% ; activate the most recently active window in this group
break
}
}
return
https://autohotkey.com/docs/commands/WinGet.htm#List

Related

Autohotkey : Wildcard ahk_class match

I used winspy to grab an ahk_class for my autohotkey macro. Sometimes that application has 2+ ahk_classes associated with that program
Example:
HwndWrapper[program.exe;;1111-1111]
HwndWrapper[program.exe;;2222-2222]
How can I use winNotExist to simply just match both names? Or perhaps use a ||, OR etc?
E.g.
F12::
IfWinNotExist, ahk_class "HwndWrapper.+"
Run, AQ8.exe
GroupAdd, kjexplorers11, ahk_class "HwndWrapper.+" ;You have to make a new group for each application, don't use the same one for all of them!
if WinActive("ahk_exe AQ8.exe")
GroupActivate, kjexplorers11, r
else
WinActivate ahk_class ahk_class "HwndWrapper.+" ;you have to use WinActivatebottom if you didn't create a window group.
Return
I finally figured it out. SetTitleMatchMode, Regex.
This command affects the behavior of all windowing commands, e.g. WinExist and WinActivate
Then write some javascript-like regex statements as arguments.
The full list of window commands is on AHK site
Script, revised
F12::
SetTitleMatchMode,RegEx
IfWinNotExist, ahk_class HwndWrapper.+
Run, AQ8.exe
GroupAdd, kjexplorers11, ahk_class HwndWrapper.+ ;You have to make a new group for each application, don't use the same one for all of them!
if WinActive("ahk_exe AQ8.exe")
GroupActivate, kjexplorers11, r
else
WinActivate ahk_class ahk_class HwndWrapper.+ ;you have to use WinActivatebottom if you didn't create a window group.
Return
So now I can cycle through any application with more than 1 ahk_Class name. Example of what script does

AutoHotKey select pop-up window not work

I write a script to test selecting pop-up windows.
SetTitleMatchMode, 2
winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
IfWinExist,%winTitle%
{
WinActivate
send !{F4}
}
IfWinExist,%popWin%
{
WinActivate
WinWaitActive, %popWin%
WinGetClass, outputvar, %popWin%
MsgBox %outputvar%
}
This script is intended to send ALT-F4 to close an opened R window, and when the confirmation pop-up window occurs, display the pop-up window's class name.
The first if block works fine. However, the send if block sometimes works, sometimes not. Active window info shows the pop-up windows' class info is:
Window Title, Class and Process
Question
ahk_class #32770
ahk_exe Rgui.exe
snapshot of the above info
I don't know why IfWinExist,%popWin% does not work. I tried changing popWin:="ahk_class #32770 ahk_exe Rgui.exe" to popWin:="ahk_class #32770", still it sometimes works, and sometimes not. So what should I do to select the pop-up windows correctly?
I have changed your AutoHotkey code, such that it should give you the functionality you require.
SetTitleMatchMode, 2
winTitle:="RGui (64-bit) ahk_class Rgui Workspace ahk_exe Rgui.exe"
popWin:="ahk_class #32770 ahk_exe Rgui.exe"
if (hWnd := WinExist(winTitle)) ;this assigns hWnd, it does not compare hWnd with WinExist(winTitle)
{
;WinActivate, ahk_id %hWnd%
;send !{F4}
WinClose, ahk_id %hWnd% ;WinClose is more direct than Alt+F4 if it works (Send can potentially send key presses to the wrong window if a new window suddenly appears)
}
WinWait, %popWin%, , 5 ;wait for window to exist, give up after 5 seconds
if !ErrorLevel ;if window found within 5 seconds
{
WinGet, hWnd, ID, %popWin%
WinActivate, ahk_id %hWnd%
WinGetClass, outputvar, ahk_id %hWnd%
MsgBox %outputvar%
}
Note:
In most cases WinActivate requires a window title/hWnd be specified.
The second part of your code works sometimes but not other times, probably because if the popup appears very quickly, then IfWinExist will find the window, but if the popup appears slowly, then the IfWinExist check will occur before the window exists, and will thus not find the window.

Solution to a "dynamic"/"unstable" ahk_class name?

In AutoHotKey, ahk_class NAME has been a good identifier for programs. Typically, I now use it in the following two ways:
Simply calling the most recently called active instance:
; Pressing Win+Shift+X will bring up (or switch to) the Windows Journal
Application
#+x::
IfWinExist ahk_class JournalApp
WinActivate ahk_class JournalApp
else
Run C:\Program Files\Windows Journal\Journal.exe
return
Grouping windows with the same ahk_class and looping through them:
GroupAdd, FIRE, ahk_class MozillaWindowClass
; Pressing Win+3 will go through all open Firefox windows one by one.
#3::
IfWinExist ahk_class MozillaWindowClass
GroupActivate, FIRE, r
else
Run firefox
return
However, there are certain programs that do not have a stable ahk_class name. For example, the ArcMap.exe should
display the following information in the "Windows Spy" window of AHK:
>>>>>>>>>>( Window Title & Class )<<<<<<<<<<<
Untitled - ArcMap
ahk_class Afx:012F0000:b:00010003:00000006:001C0BB0
Any idea how could I refer to this application that has dynamic ahk_class?
I am aware that there is also something called ahk_exe. However, I did not find it compatible with the method GroupAdd,
GroupActivate or IfWinExist.
You definitely are on the right track trying to incorporate ahk_exe. GroupAdd (and every other command receiving a WinTitle parameter at that) is compatible with ahk_exe in AHK_L; this sample code works perfectly on my machine:
GroupAdd, notepad, ahk_exe notepad.exe
Run, notepad.exe
WinWaitActive, ahk_group notepad
Sleep, 1000
WinClose, ahk_group notepad
I suspect you're not using the most recent version of AHK. Get it here and have another try.

executing a script when at a specific classNN

How shouuld I tweak the following script so that the hotkey is activated not at ANY moment where I'm at the class shown (that is Outlook) but at a specific sub window ( the preview pane of the Inbox box (whose classNN is _WwG1 )) ?
#IfWinActive ahk_class rctrl_renwnd32
+!m::
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You don't seem to be in context.
return
#IfWinActive
Make the hotkey look for active controls once it is activated. This way you can use the same hotkey for multiple commands, each command depending on the control. You can do this with several if/else statements to test for the subcontrols.
The hotkey only works in Outlook.
Each control has its own command
Each command is limited to that particular control
#ifwinactive, ahk_exe outlook.exe
{
+!m::
controlgetfocus, thiscontrol
if(thiscontrol = "_Wwg1"){
ControlFocus, OutlookGrid1, ahk_class rctrl_renwnd32
if ErrorLevel ; i.e. it's not blank or zero.
MsgBox, You seem to focused on %thiscontrol%
}else if(thiscontrol = "_Wsg2){
msgbox, you've discovered the second control!
}
return
}

autohotkey not able to get Bing Desktop control

The Bing Desktop has a Win + Y hotkey to toggle the display of itself. But I want to bring it foreground with my Win + I hotkey.
I met with a problem that I can't set focus to the search box with ControlFocus, Edit1 where Edit1 is the ClassNN reported by Window Spy.
The script is posted here:
#i::
IfWinExist, ahk_exe BingDesktop.exe
{
IfWinNotExist, ahk_class BingToolBand
{
Send #y
Return
}
WinWait, ahk_class BingToolBand
WinActivate
WinWaitActive
ControlFocus, Edit1
Return
}
Run "C:\Program Files (x86)\Microsoft\BingDesktop\BingDesktop.exe"
Return
I figured out that the ahk_class BingToolBand is not the top windowtitle of Bing Desktop.
I should have used 必应Bing 缤纷桌面 instead.