Activate/click on last X active windows

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Destroy666
Posts: 4
Joined: 27 Nov 2021, 13:56
Contact:

Activate/click on last X active windows

Post by Destroy666 » 27 Nov 2021, 15:56

Hello,
I'm writing a script to kill hanging processes with a shortcut. It works fine but I'd like to improve it by having X last used windows being activated/clicked on so that Windows refreshes some info about processes that don't report correctly.
How would I do that? I saw only activation for current/least recent Window in the docks.
Thanks.

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

Re: Activate/click on last X active windows

Post by mikeyww » 27 Nov 2021, 18:12

Code: Select all

win := [], win.Push(WinActive("A")), max := 3
Loop {
 WinWaitNotActive, A
 WinGet, style, Style, A
 If (style ~= "0x(9|16)")
  Continue
 SoundBeep, 1800
 If win.Count() >= max
  win.RemoveAt(1)
 win.Push(WinActive("A"))
}
F3::
list =
For each, hWnd in win {
 WinGetTitle, title, ahk_id %hWnd%
 list .= (list ? "`n`n" : "") title
 WinActivate, ahk_id %hWnd%
}
MsgBox, 64, % "Recent windows (N=" win.Count() ")", %list%
Return

Destroy666
Posts: 4
Joined: 27 Nov 2021, 13:56
Contact:

Re: Activate/click on last X active windows

Post by Destroy666 » 28 Nov 2021, 01:22

Makes sense, thanks!

What's 0x16 code for the styles? I found this https://www.autohotkey.com/docs/misc/Styles.htm but no 0x16 there

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

Re: Activate/click on last X active windows

Post by mikeyww » 28 Nov 2021, 06:57

It's an ad hoc solution. In looking at the styles of various windows, I happened to notice that windows with those styles were not visible windows appearing on the taskbar. The approach might not always work; you can test it if you like. It does work here. I don't know what that style number represents, but it's probably listed somewhere on the Internet. I believe that the forum has some other posts with different approaches to identifying taskbar listings, in case you want to try those, too.

Destroy666
Posts: 4
Joined: 27 Nov 2021, 13:56
Contact:

Re: Activate/click on last X active windows

Post by Destroy666 » 28 Nov 2021, 20:03

Hmmm. I tried and changed the script from

Code: Select all

 If win.Count() >= max
     win.Remove(1)
to

Code: Select all

If win.Count() == max
     Break
since it was pretty much infinite otherwise. But I'm not too sure that's it, still. Now I'm clicking on random windows and then hear X beeps, but I would like for this to happen automatically, basically loop through windows starting from most recent to least recent.

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

Re: Activate/click on last X active windows

Post by mikeyww » 28 Nov 2021, 22:22

Here is a recent post that stores all of the HWND values in an array. You can probably adapt it. Feel free to ask questions.

viewtopic.php?p=431802#p431802

The adaptation is below.

Code: Select all

win := [], win.Push(WinActive("A")), max := 3
Loop {
 WinWaitNotActive, A
 WinGetTitle, ttitle, A
 WinGet, proc, ProcessName, A
 SplitPath, proc,,,, proc
 WinGetClass, class, %winTitle%
 If (ttitle = "" || ttitle = "Program Manager" && proc = "Explorer" || class = "#32770")
  Continue
 SoundBeep, 1800
 If win.Count() >= max
  win.RemoveAt(1)
 win.Push(WinActive("A"))
}
F3::
clist =
For each, hWnd in win {
 WinGetTitle, ttitle, ahk_id %hWnd%
 clist .= (clist ? "`n`n" : "") ttitle
 WinActivate, ahk_id %hWnd%
}
MsgBox, 64, % "Recent windows (N=" win.Count() ")", %clist%
Return
This is a demonstration. Pressing F3 will activate the three windows.

What the script does with F3: "having X last used windows being activated/clicked". Before you change it, try it the way it is, to see if the script achieves that functionality.

Destroy666
Posts: 4
Joined: 27 Nov 2021, 13:56
Contact:

Re: Activate/click on last X active windows

Post by Destroy666 » 29 Nov 2021, 01:20

I see what the problem was, I placed the "Loop" in a wrong position and it stopped execution of some things in my script, it is supposed to work infinitely and collect on the fly. I assume there''s no non-infinite way that you can use e.g. on button click rather than having that running at all times?

Also, I noticed this has duplicates problem, but I'll try to work my way around that.

Thanks again!

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

Re: Activate/click on last X active windows

Post by mikeyww » 29 Nov 2021, 05:18

The loop simply tracks the recent windows. You can remove the beep. If you had to activate it on demand, it could not track anything until that time, but you could still do that by adding a hotkey or some other condition.

Post Reply

Return to “Ask for Help (v1)”