;IdleWinPosSaver.ahk ; Save and restore window positions when the system is idle for x minutes ;Skrommel @ 2017 #NoEnv #Persistent #SingleInstance,Force SetBatchLines,-1 SetWindelay,0 idle:=30*60*1000 applicationname=IdleWinPosSaver OnExit,EXIT Gosub,MENU SetTimer,SLEEP,1000 Return SLEEP: If (A_TimeIdlePhysical1000) Return SetTimer,WAKE,Off ToolTip,WAKE Loop { SysGet,newmonitorcount,MonitorCount If (newmonitorcount=monitorcount) Break } Gosub,LOAD SetTimer,SLEEP,1000 Return SAVE: windows:="" windows:={} WinGet,list,List Loop,% list { id:=list%A_Index% Loop { parent:=DllCall("GetParent","UInt",id) If parent=0 Break id:=parent } WinGetPos,x,y,w,h,Ahk_id %id% WinGet,m,MinMax,Ahk_id %id% If m<>0 WinGetNormalPos(id,x,y,w,h) windows[id]:=Object() windows[id].x:=x windows[id].y:=y windows[id].w:=w windows[id].h:=h windows[id].m:=m } Return LOAD: WinGet,list,List Loop,% list { id:=list%A_Index% Loop { parent:=DllCall("GetParent","UInt",id) If parent=0 Break id:=parent } x:=windows[id].x y:=windows[id].y w:=windows[id].w h:=windows[id].h m:=windows[id].m If m= Continue WinGet,newm,MinMax,Ahk_id %id% If (newm<>m) { WinRestore,Ahk_id %id% Loop,10 { WinGet,newm,MinMax,Ahk_id %id% If newm=0 Break Sleep,0 } } If m=-1 { WinMinimize,Ahk_id %id% Loop,10 { WinGet,newm,MinMax,Ahk_id %id% If newm=-1 Break Sleep,0 } WinSetNormalPos(id,x,y,w,h) } Else If minmax=1 { WinMaximize,Ahk_id %id% Loop,10 { WinGet,newminmax,MinMax,Ahk_id %id% If newminmax=1 Break Sleep,0 } WinSetNormalPos(id,x,y,w,h) } Else WinMove,Ahk_id %id%,,% x,% y,% w,% h } Return EXIT: ExitApp MENU: Menu,Tray,Click,1 Menu,Tray,NoStandard Menu,Tray,Add,%applicationname%,LOAD Menu,Tray,Add Menu,Tray,Add,E&xit,EXIT Menu,Tray,Default,%applicationname% Menu,Tray,Tip,%applicationname% Return ;Stolen from Lexikos at https://autohotkey.com/board/topic/25204-original-window-positionsize/ WinGetNormalPos(hwnd,ByRef x,ByRef y,ByRef w="",ByRef h="") { VarSetCapacity(wp,44) NumPut(44,wp) DllCall("GetWindowPlacement","UInt",hwnd,"UInt",&wp) x:=NumGet(wp,28,"int") y:=NumGet(wp,32,"int") w:=NumGet(wp,36,"int")-x h:=NumGet(wp,40,"int")-y } WinSetNormalPos(hwnd,ByRef x,ByRef y,ByRef w="",ByRef h="") { VarSetCapacity(wp,44) NumPut(44,wp) DllCall("GetWindowPlacement","UInt",hwnd,"UInt",&wp) NumPut(x,wp,28) NumPut(y,wp,32) NumPut(x+w,wp,36) NumPut(y+h,wp,40) DllCall("SetWindowPlacement","uint",hwnd,"UInt",&wp) }