reDock // Window Restore after Laptop Dock/Undock

Post your working scripts, libraries and tools
Posts: 34
Joined: 08 Mar 2017, 16:15

reDock // Window Restore after Laptop Dock/Undock

19 Aug 2019, 07:14

At my work I need to undock my laptop more often then I want to and I find it annoying that I need to rearrange my windows manually every time I dock it again. I found this old solution called Dockwin but it didn't have the level of automation I wanted.

Here is my updated solution. Let me know what you think, if you have a solution of your own, or if there are improvements that can be made. Thanks!

UPDATED 10/11/19 @ 09:53, made some small changes that came thru continued use.

Code: Select all

#SingleInstance, Force
SetTitleMatchMode, 3		; 2: A window's title can contain WinTitle anywhere inside it to be a match. -- 3: A window's title must exactly match WinTitle to be a match.
SetTitleMatchMode, Fast		;Fast is default
DetectHiddenWindows, off	;Off is default
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


SavedWindowPositions = reDockWindowPositions.txt
dockedMonitorNumber= 3
dockedScreenWidth= 5760
dockedScreenHeight= 1080

    SysGet, virtualScreenWidth, 78 ; SM_CXVIRTUALSCREEN - Width of the virtual screen, in pixels. The virtual screen is the bounding rectangle of all display monitors.
    SysGet, virtualScreenHeight, 79 ; SM_CYVIRTUALSCREEN - Height of the virtual screen, in pixels. 
    SysGet, MonitorCount, 80 ; Number of display monitors on the desktop (not including "non-display pseudo-monitors")
    if (MonitorCount = dockedMonitorNumber) && (virtualScreenWidth = dockedScreenWidth) && (virtualScreenHeight = dockedScreenHeight) ; if there are the correct number of monitors with the correct dimensions
        if (wasUndocked){ 
            wasUndocked =
            MsgBox, 4,, It appears you have reDocked`, would you like to restore windows to their previous state?`n`nThis msgbox will time out to "Yes" in 30 seconds., 30
            IfMsgBox Yes
                Gosub, RESTOREWINDOWS
            IfMsgBox Timeout
                Gosub, RESTOREWINDOWS

        WinGet, activeWindowID , ID, A ; get the id of the active window
        WinGetTitle, activeWindowTitle, ahk_id %activeWindowID% ;get the title of the active window

        if (activeWindowTitle) && (StrLen(activeWindowTitle) < 80) ;if the activeWindowTitle is not empty and the activeWindowTitle is not over 100 chras long (because chrome loads url as page title before page title loads to replace it)
            WinWaitNotActive, %activeWindowTitle% ; once the window is longer active, get it's state

            if WinExist(activeWindowTitle) ; if the window still exists, get it's state
                Gosub, WRITETOINI ; write current window state to file
    } else ; if there are not 3 monitors, or the virtual screen dimensions are not correct
        wasUndocked = 1
        Sleep 30000

WinGetPos, x, y, width, height, %activeWindowTitle% ;get the x/y/width/height of the window 
WinGet, minMax, minmax, %activeWindowTitle% ;get the min/max state of the window

activeWindowTitleNSC :=  RegExReplace(activeWindowTitle, "[^a-zA-Z0-9_-]")   ; Remove special chars from the window title to use as key

; write information to ini - note that if an ini section already exists with the same key the information is replcaed, else a new section is created
IniWrite, %activeWindowTitle%,          %SavedWindowPositions%, %activeWindowTitleNSC%,title
IniWrite, %x%,                          %SavedWindowPositions%, %activeWindowTitleNSC%,x
IniWrite, %y%,                          %SavedWindowPositions%, %activeWindowTitleNSC%,y
IniWrite, %width%,                      %SavedWindowPositions%, %activeWindowTitleNSC%,width
IniWrite, %height%,                     %SavedWindowPositions%, %activeWindowTitleNSC%,height
IniWrite, %minMax%,                     %SavedWindowPositions%, %activeWindowTitleNSC%,minMax


;Win-0 (Restore window positions from file)
wasUndocked = 
WinGet,Windows,List ; get a list of all open windows
Sort, Windows, U ; U: Removes duplicate items from the list so that every item is unique.
	openWindowID := "ahk_id " . Windows%A_Index% ; format id of window
	WinGetTitle,thisOpenWindow,%openWindowID% ;get title of window

    if (thisOpenWindow) ; if the window title is not empty
        thisOpenWindowNSC :=  RegExReplace(thisOpenWindow, "[^a-zA-Z0-9_-]")   ; Remove special chars from the window title so that it matches the saved window title, which was also stripped 
        IniRead, title,             %SavedWindowPositions%, %thisOpenWindowNSC%, title          , ERROR ; assign title value if ini section is found with exact window title, else make it ERROR
        if (title != "ERROR") && WinExist(title) ; if the window exists
            IniRead, x,                     %SavedWindowPositions%, %thisOpenWindowNSC%, x             , ERROR
            IniRead, y,                     %SavedWindowPositions%, %thisOpenWindowNSC%, y             , ERROR
            IniRead, width,                 %SavedWindowPositions%, %thisOpenWindowNSC%, width         , ERROR
            IniRead, height,                %SavedWindowPositions%, %thisOpenWindowNSC%, height        , ERROR
            IniRead, minMax,                %SavedWindowPositions%, %thisOpenWindowNSC%, minMax        , ERROR
            WinRestore, %title% ; bring up window
            WinActivate, %title% ; activate window
            WinMove,%title%,,%x%,%y%,%width%,%height% ;reposition window to saved state
            If (minMax = 1)    ;minmax 1 means window is maximized
                WinMaximize, %title%
            } Else If (minMax = -1)		; minmax -1 means Window is minimised
                WinMinimize, %title%
TrayTip,reDock,All windows restored.,1,1
Posts: 2
Joined: 17 Nov 2019, 17:53

Re: reDock // Window Restore after Laptop Dock/Undock

17 Nov 2019, 18:44

How is this different from DockWin my friend?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Chunjee and 23 guests