Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Close All but Active Window


  • Please log in to reply
13 replies to this topic
Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009
Hello, I'm trying to set a hotkey to automatically close all windows except the active one and those pre-declared in the existing variable NoEnd. Here is the code I tried:

^sleep::
WinGetActiveTitle, keepThis
MsgBox, 52, keepACTIVE, Close All Open Windows Except Active One?
IfMsgBox Yes
{
WinGet, ID, List, , , Program Manager
Loop, %ID%
   {
      StringTrimRight, This_ID, ID%A_Index%, 0
      WinGetTitle, This_Title, ahk_id %This_ID%
      If This_Title in %NoEnd%
         Continue
	  else If This_Title in %keepThis%
         Continue
      WinClose, %This_Title% 
}
}
IfMsgBox No
Return

This code still closes the active window. How can I make it work?

Thanks,

GP

Leef_me
  • Moderators
  • 8357 posts
  • Last active: Today, 03:41 PM
  • Joined: 08 Apr 2009
Hey PG,

I added two msgbox for diagnostic & changed your winclose to msgbox.
I then tried the code and it worked as expected. "NoEnd" is empty.

Please try the script as I have changed it and report back.
Does it show a msgbox winclose ??? which is the same as the first msgbox?

f1::
^sleep:: 
WinGetActiveTitle, keepThis 
msgbox active:%keepThis%
MsgBox, 52, keepACTIVE, Close All Open Windows Except Active One? 
IfMsgBox Yes 
{ 
WinGet, ID, List, , , Program Manager 
Loop, %ID% 
   { 
      StringTrimRight, This_ID, ID%A_Index%, 0 
      WinGetTitle, This_Title, ahk_id %This_ID% 
msgbox considering:%This_Title%
      If This_Title in %NoEnd% 
         Continue 
     else If This_Title in %keepThis% 
         Continue 
       msgbox WinClose %This_Title% 
   } 
} 
IfMsgBox No 
Return 
btw,I don't know where you got this line
StringTrimRight, This_ID, ID%A_Index%, 0
but this would work the same and be easier to read
This_ID:=ID%A_Index%


SKAN
  • Administrators
  • 9105 posts
  • Last active:
  • Joined: 26 Dec 2005

btw,I don't know where you got this line

StringTrimRight, This_ID, ID%A_Index%, 0


By Chris - when AHK did not support assignment operator.

Leef_me
  • Moderators
  • 8357 posts
  • Last active: Today, 03:41 PM
  • Joined: 08 Apr 2009
Thanks SKAN.

Hey wait a minute! The helpfile is infected! I found more than one line in Minimize Window to Tray Menu like this :shock:
StringTrimRight, IDToRestore, mwt_WindowID%a_index%, 0
And in other examples too :roll:

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009

Does it show a msgbox winclose ??? which is the same as the first msgbox?

Thanks for taking an interest. Yes, it does.

PG

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

... found more than ... other examples too

Please report in "Suggestions on documentation improvements" <!-- m -->http://www.autohotke...opic.php?t=2822<!-- m -->

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009
I had the idea of adding the active window to my list of exceptions and they are not being closed down. I tried this code:

NoEnd = Creative Element Power Tools Startup,Windows Task Manager

^sleep::
WinGetActiveTitle, keepThis
MsgBox %keepThis%
Msgbox %NoEnd%
TempEnd = %NoEnd%`,%keepThis%
Msgbox %TempEnd%
MsgBox, 52, closeOTHERS, Close All Open Windows except the active one?
IfMsgBox Yes
{
WinGet, ID, List, , , Program Manager
Loop, %ID%
   {
      This_ID:=ID%A_Index%
      WinGetTitle, This_Title, ahk_id %This_ID%
      If This_Title in %TempEnd%
         Continue 
      WinClose, %This_Title% 
}
}
IfMsgBox No
Return
The active window appears to get added to the list but it is still being closed. Creative Power Tools Startup is not closed. What's going wrong?

Thanks,

GP

sinkfaze
  • Moderators
  • 6365 posts
  • Last active:
  • Joined: 18 Mar 2008
It'll probably be easier to work from the unique IDs rather than retrieving them just to retrieve the window title(which may or may not match). Slight revision (untested):

NoEnd = Creative Element Power Tools Startup,Windows Task Manager 

^sleep:: 
[color=red]Loop, Parse, NoEnd, `,
  safe.=WinExist(A_LoopField) "`n"
safe.=WinActive("A")[/color]
MsgBox, 52, closeOTHERS, Close All Open Windows except the active one? 
IfMsgBox No
  return 
WinGet, ID, List, , , Program Manager 
Loop, %ID% {
  [color=red]MsgBox % "Current ID: " A_LoopField "`n"
    . "List:`n`n" safe[/color] 
  If !InStr(safe,A_LoopField) 
    [color=red]WinKill, %A_LoopField%[/color]
}
VarSetCapacity(safe,0) 
Return


Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009
Thanks for the code, I tried it but it didn't close anything.

GP

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009
I finally figured it out:

^sleep::
MsgBox, 52, closeOTHERS, Close All Open Windows except the active one?
IfMsgBox No
  return 
WinGetActiveTitle, keepThis
WinGet, ID, List, , , Program Manager
Loop, %ID%
   {
      StringTrimRight, This_ID, ID%A_Index%, 0
      WinGetTitle, This_Title, ahk_id %This_ID%
      If This_Title in %keepThis%
	  {
         Continue
	  }
	  if This_Title in %NoEnd%
	  {
         Continue
	  }
      WinClose, %This_Title%
   }
Return
Thanks to everyone who posted.

GP

Leef_me
  • Moderators
  • 8357 posts
  • Last active: Today, 03:41 PM
  • Joined: 08 Apr 2009

I finally figured it out:

:?: Can you explain what the problem was, I'd like to know for future reference.

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: Sep 30 2013 10:45 PM
  • Joined: 15 Aug 2009
The first code I tried was still closing the active window. It seems I needed to add brackets to get the two if statements in the loop recognised.

GP

Leef_me
  • Moderators
  • 8357 posts
  • Last active: Today, 03:41 PM
  • Joined: 08 Apr 2009
Hmm... Seems strange, but thanks for the answer. I'll hafta ponder that.

loumizhu
  • Members
  • 20 posts
  • Last active: Feb 12 2014 01:18 PM
  • Joined: 02 Oct 2012

hello there

i was looking for similar app (close all but active) , 

i tried doing it with autoit, but it's still ask windows to shutdown.

i tried your code, and it's doing the same. and since i'm not a badass programmer i can't fix it