Skip to content

Instantly share code, notes, and snippets.

@saim-doruklu
Forked from FunkMonkey/WindowSetIterator.ahk
Last active May 21, 2019 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saim-doruklu/844bcd127d3d88a4732e6f2af1b112cd to your computer and use it in GitHub Desktop.
Save saim-doruklu/844bcd127d3d88a4732e6f2af1b112cd to your computer and use it in GitHub Desktop.
Window Set Iterator
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
; ATTENTION: requires Autohotkey_L
; alt+shift+a to add
; alt+shift+s to remove
; alt+shift+left to prev
; alt+shift+right to next
; TODO: changing currentWindowSet via hotkey
; ======================================
; Code
; ======================================
#WinActivateForce ; prevent flashing of taskbar buttons
windowSets := {}
currentWindowSet := 1
indexOf(array, item) {
for index, value in array {
if(value == item)
return index
}
return 0
}
insertIntoWindowSet(set, winId) {
; find duplicates
index := indexOf(set, winId)
if( index != 0) {
return
}
set.insert(winId)
}
removeFromWindowSet(set, winId) {
; find it
index := indexOf(set, winId)
if( index != 0) {
set.Remove(index)
}
}
getOrCreateSet(setIndex) {
global windowSets
set := windowSets[setIndex]
if( !set ) {
set := []
windowSets[setIndex] := set
}
return set
}
showWindow(winID) {
WinActivate, ahk_id %winID%
}
nextWindowInSet(set, currWinID) {
if( set.MaxIndex() == 0) {
return
}
index := indexOf(set, currWinID)
exists := false
while ( exists == false )
{
if( set.MaxIndex() == 0) {
return
}
if ( index == 0 || index + 1 > set.MaxIndex()) {
selected := set[1]
index := 1
} else {
selected := set[index + 1]
index := index + 1
}
exists:=WinExist("ahk_id" selected)
if ( exists == false ){
removeFromWindowSet(set, selected)
}
}
showWindow(set[index])
}
previousWindowInSet(set, currWinID) {
if( set.MaxIndex() == 0) {
return
}
index := indexOf(set, currWinID)
exists := false
while ( exists == false )
{
if( set.MaxIndex() == 0) {
return
}
if ( index == 0 || index - 1 < 1) {
selected := set[set.MaxIndex()]
index := set.MaxIndex()
} else {
selected := set[1]
index := 1
}
exists:=WinExist("ahk_id" selected)
if ( exists == false ){
removeFromWindowSet(set, selected)
}
}
showWindow(set[index])
}
; ======================================
; Hotkeys
; ======================================
!+a::
WinGet, activeWindowID, ID, A
set := getOrCreateSet(currentWindowSet)
insertIntoWindowSet(set, activeWindowID)
return
!+s::
WinGet, activeWindowID, ID, A
set := getOrCreateSet(currentWindowSet)
removeFromWindowSet(set, activeWindowID)
return
!+Right::
WinGet, activeWindowID, ID, A
set := getOrCreateSet(currentWindowSet)
nextWindowInSet(set, activeWindowID)
return
!+Left::
WinGet, activeWindowID, ID, A
set := getOrCreateSet(currentWindowSet)
nextWindowInSet(set, activeWindowID)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment