Window Position Manager

Post your working scripts, libraries and tools
BoBo
Posts: 3870
Joined: 13 May 2014, 17:15

Window Position Manager

08 Oct 2020, 16:05

At AHK's Discord channel fellow member @KraXen72 has :arrow: posted the following request/requirements to manage a set of windows ...
hotkey: ctrl = main monitor / shift = secondary monitor
- capture the current size and positions for all windows that are not minimized on the monitor
- gui prompt to name the layout and assign a hotkey for it
- save that into a json file or something -
- hotkeys assigned will restore all programs that are running and are included in the layout
- there will be a cycle hotkey for each monitor which cycles through all layouts
Here's @elModo7's solution who coded "a window pos/res saver" AKA 'Window Position Manager'.

Code: Select all

; code by elModo7
; https://github.com/elModo7
; https://discord.com/channels/115993023636176902/653362249687105536/763809349155946547

#NoEnv
#SingleInstance Force
#Persistent
SetBatchLines, -1
#Include <json>
#Include <JSON_FromObj>
#Include <json_beautifier>
global currentMonitor := 1

F1:: 			; Save Config
windowData := {}
Loop, Files, data\monitor\%currentMonitor%\*.json
{
	FileDelete, data\monitor\%currentMonitor%\%A_LoopFileName%
}
for k, v in getWindows()
{
	FileAppend, % JSON_Beautify(json_fromobj(v)), % "data\monitor\" currentMonitor "\" v.exe "_" v.class ".json"
}
return

F2::			; Load Config
windowData := {}
for k, v in getWindows_Json()
{
	if(WinExist(v.title " ahk_class " v.class " ahk_exe " v.exe))
	{
		WinMove, % v.title " ahk_class " v.class " ahk_exe " v.exe,, % v.x, v.y, v.w, v.h
		;~ MsgBox, % "Moving " v.title " to: " v.x " - " v.y " - " v.w " - " v.h
	}
}
return

F3::			; Open config gui
	if(!WinExist("WinPos Manager Config"))
	{
		Gui Font, s16 Bold
		Gui Add, Button, x16 y56 w184 h65 gmonitor1, First Monitor
		Gui Add, Button, x240 y56 w184 h65 gmonitor2, Second Monitor
		Gui Font, s14
		Gui Add, Text, x18 y13 w407 h23 +0x200 +Center, Monitor Layout in use?
		Gui Show, w438 h143, WinPos Manager Config
	}else
	{
		WinActivate, WinPos Manager Config
	}
return

getWindows_Json()
{
	winList := {}
	Loop, Files, data\monitor\%currentMonitor%\*.json
	{
		FileRead, file, data\monitor\%currentMonitor%\%A_LoopFileName%
		win := JSON.Load(file)
		winList[winList.length()+1] := win 
	}
	return winList
}

getWindows()
{
	winList := {}
    WinGet, OpenWindow, List 
    Loop, %OpenWindow%
    {
        WinGetTitle, title, % "ahk_id " OpenWindow%A_Index%
        WinGetClass, class, % "ahk_id " OpenWindow%A_Index%
		WinGet, exe, ProcessName, % "ahk_id " OpenWindow%A_Index%
		WinGetPos, x, y, w, h, % "ahk_id " OpenWindow%A_Index%
        if(Title != "") 
		{
			win := {}
            win["title"] := title
            win["class"] := class
            win["exe"] := exe
            win["x"] := x
            win["y"] := y
            win["w"] := w
            win["h"] := h
			winList[winList.length()+1] := win 
		}
    }
    return winList
}

monitor1:
	currentMonitor := 1
	Gui, Destroy
return

monitor2:
	currentMonitor := 2
	Gui, Destroy
return

Esc::ExitApp
Full set of (include) files attached.
Questions? Join AHK's Discord Server (see Menu-Option above)!
Attachments
Window_PosManager.zip
(6.62 KiB) Downloaded 35 times
User avatar
elModo7
Posts: 189
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: Window Position Manager

08 Oct 2020, 16:21

Was fun doing it on stream!
There are many things to improve but it's a decent base to start with I guess.
:beer:
JRW254
Posts: 1
Joined: 23 Jun 2020, 16:16

Re: Window Position Manager

10 Oct 2020, 00:03

That's some crafty code there. I've been playing around and if you pair it with VirtualDesktopAccessor.dll you can use a laptop and have the profiles move into any number of virtual desktops.

Appreciate the effort elModo7 put into coming up with this. Hope other folks find it useful too.

Thanks!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: gwarble and 38 guests