autohotkey

Activate most recently active window in a group with 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.


Solution

  • 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 the most recent to the last activated:
        WinGet, myWindows, List, ahk_group TextEditor
        WinActivate, ahk_id %myWindows1% ; activate the most recently active window in this group
    }
    return
    

    https://autohotkey.com/docs/commands/WinGet.htm#List