GroupAdd, GroupDelete, GroupTranspose
Posted: 24 Dec 2018, 04:35
I needed a way to create groups i could modify on the fly,and i ended up with this...
EDIT1: I had posted a version with a bad GroupDelete() function.
EDIT2: refactored some of the functions for less overhead. Courtesy of a well made point by @swagfag.
EDIT1: I had posted a version with a bad GroupDelete() function.
EDIT2: refactored some of the functions for less overhead. Courtesy of a well made point by @swagfag.
Code: Select all
;Uses a combined list & array Groups index,for ease of use & efficiency,respectively.
GroupAdd("mediaPlayers", "ahk_exe vlc.exe", "ahk_exe mpc-hc.exe")
;GroupDelete("mediaPlayers")
;create new group and make it a 'real' group...
GroupAdd("browsers", "ahk_exe chrome.exe", "ahk_exe firefox.exe")GroupTranspose("browsers")
SetTimer, listGroups, 300
;toogle group member
x::IsInGroup("mediaPlayers","vlc.exe") ? GroupDelete("mediaPlayers", "ahk_exe vlc.exe") : GroupAdd("mediaPlayers", "ahk_exe vlc.exe")
;(de)activate a group... of course group members can't be removed from the transposed 'real' group,only from the custom group they were inherited from...
a::GroupActivate % GroupTranspose("browsers")
d::GroupDeactivate % GroupTranspose("browsers")
#If GroupActive("mediaPlayers")
g::MsgBox 0x40040,, MEDIA PLAYER GROUP ACTIVE
#If
#IfWinActive ahk_group browsers ;Custom Group transformed to real group...
g::MsgBox 0x40040,, BROWSER GROUP ACTIVE
#IfWinActive
listGroups:
ToolTip % A_Groups "`n`n" GroupActive("mediaPlayers") "`n" GroupActive("browsers")
Return
;====================================================================================================
GroupAdd(groupName,groupMembers*){
Global A_Groups,A_GroupsArr
( !InStr(A_Groups, groupName ",") ? (A_Groups .= A_Groups ? "`n" groupName "," : groupName ",") : "" ) ;initialise group if it doesn't exist...
For i,groupMember in groupMembers
( !InStr(A_Groups, groupMember) ? (A_Groups := StrReplace(A_Groups, groupName ",", groupName "," groupMember ",")) : ) ;append to or create new group...
A_Groups := RegExReplace(RegExReplace(A_Groups, "(^|\R)\K\s+"), "\R+\R", "`r`n") ;clean up groups to remove any possible blank lines
,ArrayFromList(A_GroupsArr,A_Groups) ;rebuild group as array for most efficient cycling through groups...
}
GroupDelete(groupName, groupMember:=""){
Global A_Groups,A_GroupsArr
For i,group in StrSplit(A_Groups,"`n")
( groupMember && group && InStr(A_Groups,groupMember) && groupName = StrSplit(group,",")[1] ? (A_Groups:=StrReplace(A_Groups,group,StrReplace(group,groupMember ","))) : !groupMember && groupName = StrSplit(group,",")[1] ? (A_Groups:=StrReplace(A_Groups,group)) ) ;remove group member from group & update group in A_Groups
A_Groups := RegExReplace(RegExReplace(A_Groups, "(^|\R)\K\s+"), "\R+\R", "`r`n") ;clean up groups to remove any possible blank lines
,ArrayFromList(A_GroupsArr,A_Groups) ;rebuild group as array for most efficient cycling through groups...
}
ArrayFromList(ByRef larray, ByRef list, listDelim := "`n", lineDelim:=","){
larray := []
Loop, Parse, list, % listDelim
larray.Push(StrSplit(A_LoopField,lineDelim))
}
;Function's below are subject to a performance overhead & hence use A_GroupsArr...as they are repeatedly called...
GroupActive(groupName){
Global A_GroupsArr
For i,group in A_GroupsArr
If (group.1 = groupName)
For iG,groupMember in group
If (iG > 1 && groupMember && (firstMatchId := WinActive(groupMember)))
Return group.1 "," firstMatchId
}
GroupTranspose(groupName){ ;makes this custom group,a 'real' group,some use cases....
Global A_GroupsArr
For i,group in A_GroupsArr
If (group.1 = groupName)
For iG,groupMember in group
If (iG > 1 && groupMember)
GroupAdd, % group.1, % groupMember
Return groupName
}
IsInGroup(groupName, groupMember){
Global A_Groups
Loop, Parse, A_Groups, `n
If (StrSplit(A_LoopField,",")[1] = groupName && InStr(A_LoopField,groupMember))
Return True
}
;====================================================================================================