ahk restart explorer and restore previous window

Post your working scripts, libraries and tools for AHK v1.1 and older
KnIfER
Posts: 15
Joined: 27 Nov 2023, 06:10

ahk restart explorer and restore previous window

06 Mar 2024, 01:30

For file explorer on win10.

Saving and loading sessions are more important that just tabbed window!

Code: Select all


global SessionSavepath := "D:\tmp\session.txt"

RestartExplorer( WaitSecs:=10 ) { ; requires OS Vista+    ; v2.10 by SKAN on CSC7/D39N
Local PID, Explorer, ID:=WinExist("ahk_class Progman")  ; @ tiny.cc/restartexplorer2
  WinGet, PID, PID
  WinGet, Explorer, ProcessPath
  PostMessage, 0x5B4, 0, 0,, ahk_class Shell_TrayWnd ; WM_USER+436
  Process, WaitClose, %PID%, % ( PID ? WaitSecs : (WaitSecs:=0) )
  If (PID && !Errorlevel)
    Run, %Explorer%
  WinWait, ahk_class Progman,, %WaitSecs%
Return (WinExist()!=ID)
}


OpenFolder(path) {
    ; Convert the folder path to a PIDL
    folderPIDL := DllCall("Shell32\ILCreateFromPathW", "Str", path, "Ptr")
    if (folderPIDL) {
		result := DllCall("Shell32\SHOpenFolderAndSelectItems", "Ptr", folderPIDL)
        DllCall("Shell32\ILFree", "Ptr", folderPIDL)
    } else {
        MsgBox, Could not create PIDL from folder path.
    }
}

SaveSession() {
    FileDelete, %SessionSavepath%  ; Delete the old session file if it exists
    for Window in ComObjCreate("Shell.Application").Windows 
	{
        try 
		{
			hwnd := Window.hwnd
			VarSetCapacity(WINDOWPLACEMENT, 44, 0) ; Initialize WINDOWPLACEMENT structure
			NumPut(44, WINDOWPLACEMENT, 0) ; Set the length of the structure
			if (DllCall("GetWindowPlacement", "Ptr", hwnd, "Ptr", &WINDOWPLACEMENT) )
			{
				X := NumGet(WINDOWPLACEMENT, 28, "Int")
				Y := NumGet(WINDOWPLACEMENT, 32, "Int")
				Width := NumGet(WINDOWPLACEMENT, 36, "Int") - X
				Height := NumGet(WINDOWPLACEMENT, 40, "Int") - Y
				rcNormalPosition :=  X . "," . Y . "," . Width . "," . Height
				; MsgBox, % "Normal Position Rect: " rcNormalPosition
			} else {
				; MsgBox, % "Failed to get window placement."
				WinGet, MinMax, MinMax, % "ahk_id " Window.hwnd
				if (MinMax == -1) ; The window is minimized
				{
					WinRestore, % "ahk_id " Window.hwnd
					Sleep, 1350 
				}
				WinGet, MinMax, MinMax, % "ahk_id " Window.hwnd
				if (MinMax == 1) ; The window is maximized
				{
					WinRestore, % "ahk_id " Window.hwnd
					Sleep, 1350 
				}
				WinGetPos, X, Y, Width, Height,  % "ahk_id " Window.hwnd
			}
			tmp := Window.LocationURL "|" X "|" Y "|" Width "|" Height "`n"
            FileAppend, % tmp, %SessionSavepath%
        }
    }
}
; RestartExplorer() {
;     ; Process, Close, explorer.exe  ; Gracefully close Explorer
; 	Run, taskkill /im explorer.exe /f
;     Sleep, 1000  ; Wait a bit for the process to terminate
; 	Run, cmd /c start explorer.exe
;     ; Run, explorer.exe  ; Start Explorer again
; }
RestoreSession() {
    FileRead, data, % SessionSavepath
    if (ErrorLevel) {
        MsgBox, Could not read %SessionSavepath%
		Run, explorer.exe
        return
    }
    Loop, parse, data, `n, `r
    {
        if (A_LoopField = "")
            continue
        StringSplit, windowInfo, A_LoopField, |
     	; Run, explorer.exe "%windowInfo1%"
		OpenFolder(windowInfo1)
		; Sleep, 200  ; Wait for the window to open. Adjust timing as necessary.
		if(windowInfo2>-70 && windowInfo3>-70 && windowInfo4>10 && windowInfo5>10) {
			WinMove, ahk_class CabinetWClass,, %windowInfo2%, %windowInfo3%, %windowInfo4%, %windowInfo5%
		}
	}
}

SaveSession()
RestartExplorer()
Sleep,2000
RestoreSession()



library used : RestartExplorer from viewtopic.php?t=81252

The comments are from chatgpt.
KnIfER
Posts: 15
Joined: 27 Nov 2023, 06:10

Re: ahk restart explorer and restore previous window

13 Mar 2024, 03:58

Won't work for frozen explore window! The entire script will freeze in such case.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: submeg and 123 guests