Chris wrote:
There's currently no way to do it, so the usual work-around would be to create a new group.
I've been thinking of solving this, because I needed a "remove" command too.
Although having a Group Remove command etc. in AHK would be easier.
Quote:
The main problem is that we don't have access to the list of criteria of GroupName from GroupAdd.
So it's necessary to keep track of those.
I tried to put it all in the function WinGroup, so I don't have to worry about all this. I hope it can be useful

.
WinGroup commands: WinGroup("add",GroupName,<Window Parameters>) : add Window to GroupName
WinGroup("remove",GroupName,<Window Parameters>) : removes Window from GroupName
WinGroup("clear",GroupName) : deletes all windows from GroupName (group GroupName itselfs still exists)
Limitations and Remarks: -You have to use always WinGroup() to keep track of added windows.
-You have to use a variable for GroupName instead of a string constant like GroupName in AHK GroupAdd.
I.e. you have to use also
"ahk_group %TestGroup%". So don't use variable GroupName for other purpose then function WinGroup.
-There's an absolute max. of 26 different groups (DummyA,DummyB,...,DummyZ) (Current script only 10 groups)
This can be solved too by changing the code, but 26 groups seems a lot already.
-WinGroup remove command is based on making new groups (groupnames) after each remove. Therefore old groups still exist in memory.
Code:
; -------- Testscript for WinGroup() ---------------
; Remark: testscript will close all Calculator and NotePad windows at the end!
Run, Notepad.exe
Run, Calc.exe
TestGroup:= "" ; WinGroup function can take wrong GroupName, if TestGroup accidently contains already e.g. "DummyA"
; Very small chance TestGroup contains e.g. "DummyA", but just to be sure initialize TestGroup.
MsgBox,262144,%TestGroup%, Minimize now NotePad and Calculator, and continue.
WinGroup("add",TestGroup,"ahk_class Notepad")
WinGroup("add",TestGroup,"ahk_class SciCalc")
WinRestore, ahk_group %TestGroup%
MsgBox,262144,%TestGroup%, NotePad and Calculator are restored. `nMinimize now NotePad and Calculator, and continue.
WinGroup("remove",TestGroup,"ahk_class SciCalc")
WinRestore, ahk_group %TestGroup%
MsgBox,262144,%TestGroup%,Only Notepad will be restored. ; variable TestGroup is changed by WinGroup("remove",...)
WinGroup("add",TestGroup,"ahk_class Notepad") ; test that existing criteria will not be added again
WinGroup("add",TestGroup,"ahk_class SciCalc")
WinClose, ahk_group %TestGroup%
MsgBox,262144,%TestGroup%,NotePad and Calculator are closed. ; variable TestGroup is changed by WinGroup("remove",...)
WinGroup("clear",TestGroup) ; variable TestGroup is changed by WinGroup("clear",...)
Run, Notepad.exe
F10:: WinRestore, ahk_group %TestGroup% ; test for clear TestGroup
F11:: WinGroup("add",TestGroup,"ahk_class Notepad") ; test for clear TestGroup
F9::ExitApp
Return
; -------- End Testscript for WinGroup() ---------------
WinGroup(Cmd,ByRef GroupName,WinTitle= "",WinText= "",Label= "",ExcludeTitle= "",ExcludeText= "")
{
Static WinGroupA:= "",WinGroupB:= "",WinGroupC:= "",WinGroupD:= "",WinGroupE:= "" ; list of windows in the group
Static WinGroupF:= "",WinGroupG:= "",WinGroupH:= "",WinGroupI:= "",WinGroupJ:= "" ; list of windows in the group
Static MaxGroups:= 10,FreeGroupIndex:= 1 ; first not used Group number
If (Cmd= "add") {
If (FreeGroupIndex> MaxGroups) {
MsgBox,48,%A_ScriptName% - %A_thisFunc%,MaxGroups= %MaxGroups%!
Return true ; error
}
WinGroupName:= WinGroupName(GroupName)
StringReplace, GroupIndex, WinGroupName, Dummy
If errorlevel { ; ->new group
If (FreeGroupIndex< 27) {
GroupIndex:= Chr(64 + FreeGroupIndex)
GroupName:= "Dummy" GroupIndex "1" ; -> GroupName= DummyA1 (or DummyB1,DummyC1,...)
FreeGroupIndex++
}
else {
MsgBox,48,%A_ScriptName% - %A_thisFunc%,Max. 26 groups allowed!
Return true
}
}
AddParameters:= WinTitle "," WinText "," Label "," ExcludeTitle "," ExcludeText "|"
If InStr(WinGroup%GroupIndex%,AddParameters)
Return false ; don't add already existing windows
else {
WinGroup%GroupIndex% .= AddParameters
GroupAdd, %GroupName%,%WinTitle%,%WinText%,%Label%,%ExcludeTitle%,%ExcludeText%
}
Return false
}
If (Cmd= "remove" || Cmd= "clear") {
WinGroupName:= WinGroupName(GroupName,NewNameIndex) ; e.g. WinGroup("DummyB21",...) ->DummyB and 21
StringReplace, GroupIndex, WinGroupName, Dummy
NewNameIndex++ ; ->GroupName DummyA1 becomes DummyA2 for remove command
GroupName= %WinGroupName%%NewNameIndex% ; assign new content (=name) to GroupName
; I.e. return to ByRef GroupName parameter too
WinGroupTmp:= WinGroup%GroupIndex%
WinGroup%GroupIndex%:= "" ; clear to make new list by WinGroup("add",...)
If (Cmd= "clear")
Return false ; finished re-add nothing to GroupName
RemoveParameters= %WinTitle%,%WinText%,%Label%,%ExcludeTitle%,%ExcludeText%|
StringReplace, WinGroupTmp, WinGroupTmp, %RemoveParameters%
RemoveError:= errorlevel
Loop, parse,WinGroupTmp,|
{
If (A_loopField= "")
Continue
Loop, parse,A_loopField,csv
{
If (A_index= 1)
ReAddTitle:= A_loopField
else
If (A_index= 2)
ReAddText:= A_loopField
else
If (A_index= 3)
ReAddLabel:= A_loopField
else
If (A_index= 4)
ReAddExcludeTitle:= A_loopField
else
If (A_index= 5)
ReAddExcludeText:= A_loopField
}
WinGroup("add",GroupName,ReAddTitle,ReAddText,ReAddLabel,ReAddExcludeTitle,ReAddExcludeText)
}
Return RemoveError
}
}
WinGroupName(GroupName,ByRef NameIndex= "")
{
; Makes from e.g. DummyB23 -> AlphaName= DummyB NameIndex= 23
Loop {
Char:= SubStr(GroupName,1 - A_index,1)
If Char is alpha
{
AlphaName:= SubStr(GroupName,1,StrLen(GroupName) -A_index+1)
NameIndex:= SubStr(GroupName,2 - A_index)
; Here always A_index > 1 (GroupName starts with e.g. DummyB1)
Break
}
}
Return AlphaName
}