Skip to content

Instantly share code, notes, and snippets.

@kosugi
Created March 22, 2025 10:51
Show Gist options
  • Select an option

  • Save kosugi/3143c754d22ebc2a96c63418394d8a58 to your computer and use it in GitHub Desktop.

Select an option

Save kosugi/3143c754d22ebc2a96c63418394d8a58 to your computer and use it in GitHub Desktop.
;;
;; macOS の「次のウィンドウを操作対象にする」を実現するための AutoHotkey Script (v2)
;;
;; active window が属する process の window 群の中で,
;; active window の window handle が次に大きい (かつ UI を持つ) window を focus する.
;;
;; is_asc が false の場合は「次に小さい」 window を対象とする.
;;
;; TODO: 「UI を持つ」判定に window style を用いているが確証が無い
;;
next_app_window(is_asc)
{
currentHwnd := WinGetID("A")
if !currentHwnd
return
currentExe := WinGetProcessName("ahk_id " currentHwnd)
if currentExe = ""
return
windows := WinGetList()
if windows.Length < 2
return
str := ""
for index, hwnd in windows {
exeName := WinGetProcessName("ahk_id " hwnd)
if (exeName != currentExe)
continue
style := WinGetStyle("ahk_id " hwnd)
required := 0x00C00000 | 0x00080000 ; = 0x00C80000
if ( (style & required) != required )
continue
str .= hwnd "`n"
}
str := Sort(str, "N Z U")
sortedHandles := []
Loop Parse, str, "`n"
if (A_LoopField != "")
sortedHandles.Push(A_LoopField + 0)
currentIndex := 0
for idx, hwnd in sortedHandles {
if (hwnd = currentHwnd) {
currentIndex := idx
break
}
}
if currentIndex = 0
return
if is_asc
targetIndex := (currentIndex < sortedHandles.Length) ? currentIndex + 1 : 1
else
targetIndex := (currentIndex > 1) ? currentIndex - 1 : sortedHandles.Length
targetHwnd := sortedHandles[targetIndex]
WinActivate("ahk_id " targetHwnd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment