Close all window type other than most recently active

Get help with using AutoHotkey and its commands and hotkeys
hannahelappin
Posts: 53
Joined: 01 Jun 2019, 09:02

Close all window type other than most recently active

01 Jun 2019, 09:08

Hi, I’m a beginner but not a lazy one…I have spent hours reading tutorials and looking at scripts but I can’t get the below to work.

1. Close all active windows of a certain type but keep most recently active window.

Copied script Internet script (apologies didn’t write who off to give credit- but thanks loads!)

;Close all explorer windows excepts the most active
^#X::
; List gets all ids starting from the topmost down
WinGet, OutList, List, ahk_classCabinetWClass
Loop, %OutList% {
if A_Index = 1
{ ; skip
Continue
}
WinClose, % "ahk_id " OutList%A_Index%
}
Return

Changed the script so does the same in MS Word and outlook and mozilla.

BUT doesn’t work in excel or sage accountancy software? For both it just closes all windows of excel/sage?
Excel
^#E::
; List gets all ids starting from the topmost down
WinGet, OutList, List, ahk_class XLMAIN
Loop, %OutList% {
if A_Index = 1
{ ; skip
Continue
}
WinClose, % "ahk_id " OutList%A_Index%
}
Return

Sage
The software homescreen window I want to keep active on left monitor and close all other sage windows which generally open on monitor 2 (just by chance which I prefer…..not had a go of resizing & moving positions yet).

Windows to close have class: ahk_class #32770
Homescreen window has different class (cant remember got screenshot on work pc.)
But they both the same ahk_exe, both types of windows.

Many Thanks
Hannah
trust_me
Posts: 83
Joined: 29 Jul 2017, 10:46

Re: Close all window type other than most recently active

02 Jun 2019, 05:56

You can find out if the first in the list is the one you want to keep .Maybe the first listed one is not the correct one.

Code: Select all

WinGet, OutList, List, ahk_class XLMAIN
Loop, %OutList% {
WinGetTitle, title,% "ahk_id " OutList%A_Index%
z .=A_Index "   " title "`n" 
}
msgbox %z%
hannahelappin
Posts: 53
Joined: 01 Jun 2019, 09:02

Re: Close all window type other than most recently active

02 Jun 2019, 15:17

Thank you very much. I will try it tomorrow at work.
hannahelappin
Posts: 53
Joined: 01 Jun 2019, 09:02

Re: Close all window type other than most recently active

03 Jun 2019, 08:12

trust_me
Hi, thanks for the script but dont 100% understand what this scrit shows. When i run it with 2 blank excle docs open I get msg box msg:

1 Book2- Excel
2 Book1-Excel

Many Thanks
trust_me
Posts: 83
Joined: 29 Jul 2017, 10:46

Re: Close all window type other than most recently active

03 Jun 2019, 12:30

Close all active windows of a certain type but keep most recently active window.
The code gives you the list of windows from the code : WinGet, OutList, List, ahk_class XLMAIN
The first one in the list is the last active window . In de code you have been using you skipped the first entry : if A_Index = 1
so this one will not be closed.

You seem to suggest ( if i understood it correct ) that the window you want to stay is closed when you run your code , with the code i posted you can see which is on position 1 .If the window you wanted to stay is not position 1 it will be closed ( maybe it was not the last active window ) and you will need to make it the last active window or use another way of removing it from the list for example by using the exclude option in winclose command.

WinClose [, WinTitle, WinText, SecondsToWait, ExcludeTitle, ExcludeText]

You can use part of the title or text ( Window Spy will give you info for this )
hannahelappin
Posts: 53
Joined: 01 Jun 2019, 09:02

Re: Close all window type other than most recently active

03 Jun 2019, 13:15

Thanks a lot, that definitely makes sense and thanks for the alternative with winclose and the exclude options. I’ll try it tomorrow at work.

Return to “Ask For Help”

Who is online

Users browsing this forum: CarPiotr and 138 guests