Skip to content

Instantly share code, notes, and snippets.

@szkrd
Created September 9, 2022 21:45
Show Gist options
  • Save szkrd/62630392c02420fab9c9f009aa765b39 to your computer and use it in GitHub Desktop.
Save szkrd/62630392c02420fab9c9f009aa765b39 to your computer and use it in GitHub Desktop.
Swap all windows between two vertical monitors (2nd top, primary 1st bottom)
; MonSwap - Swaps all the application windows from one monitor to another.
; see: https://www.autohotkey.com/boards/viewtopic.php?t=68311
; v1.0.0 Author: Alan Henager
; v1.0.1 Xenrik - Updated to use relative screen size when swapping
; v1.0.2 Boiler, Masgo - exclude Windows 10 secondary monitor taskbar from being swapped
; v1.0.3 Szkrd - dumbing it down for two vertical monitors, 1 below, 2 above
; +-------+
; | 2 |
; +-------+
; +-------+
; | 1 |
; +-------+
SetWinDelay, 0 ; This switching should be instant
; Set this key combination to whatever.
^#Z::
SwapAll:
{
DetectHiddenWindows, Off ; I think this is the default, but just for safety's sake...
WinGet, WinArray, List ; , , , Sharp
; Enable the above commented out portion if you are running SharpE
i := WinArray
Loop, %i% {
WinID := WinArray%A_Index%
WinGetClass, ThisClass, ahk_id %WinID%
; Do not swap the primary or the secondary monitor taskbar
If (ThisClass <> "Shell_SecondaryTrayWnd" and ThisClass <> "Shell_TrayWnd") {
WinGetTitle, CurWin, ahk_id %WinID%
If (CurWin = ) {
; For some reason, CurWin <> didn't seem to work.
} else {
WinGet, IsMin, MinMax, ahk_id %WinID% ; The window will re-locate even if it's minimized
If (IsMin = -1) {
WinRestore, ahk_id %WinID%
SwapMon(WinID)
WinMinimize, ahk_id %WinID%
} else {
SwapMon(WinID)
}
}
}
}
return
}
SwapMon(WinID) ; Swaps window with and ID of WinID onto the other monitor
{
SysGet, Mon1, Monitor, 1
Mon1Height := Mon1Bottom - Mon1Top
SysGet, Mon2, Monitor, 2
Mon2Height := Mon2Bottom - Mon2Top
WinGetPos, WinX, WinY, WinWidth, WinHeight, ahk_id %WinID%
WinCenter := WinY + (WinHeight / 2)
if (WinCenter >= Mon1Top and WinCenter <= Mon1Bottom) {
NewY := (WinY - Mon1Top) / Mon1Height
NewY := Mon2Top + (Mon2Height * NewY)
} else {
NewY := (WinY - Mon2Top) / Mon2Height
NewY := Mon1Top + (Mon1Height * NewY)
}
WinMove, ahk_id %WinID%, , %NewX%, %NewY%, ,
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment