Is possible to get path when Explorer window is closing and restore when is opening?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Is possible to get path when Explorer window is closing and restore when is opening?

30 May 2018, 09:10

Hi!

I want to do this thing:
Windows File Explorer when is open displays (in Windows 10) Quick Access or This Computer (by selection in options).
I want to get the path when is explorer closing/closed and set that path when is opened again.
I know that Explorer is always running, but its windows is possible to activate by AHK.
Yes, many explorer replacements doing this normally, but I want to apply it to File Explorer, nothing other.

So, how to get path when is window of Explorer closed/closing - to any variable or so?
And how to apply last path stored in variable at new Explorer window is opened/opening?

Explorer has one parameter to open user defined path by:
explorer /E <path>

Thank you for all.
Miro
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is possible to get path when Explorer window is closing and restore when is opening?

31 May 2018, 14:28

- I've got an example that retrieves only the folder name, when the Explorer window closes. Maybe someone could go one better.
- You could try periodically checking the Explorer path, or checking on key presses.

Code: Select all

#Persistent
OnExplorerStartEnd(hWinEventHook, vEvent, hWnd)
{
	;EVENT_OBJECT_CREATE := 0x8000
	;EVENT_OBJECT_DESTROY := 0x8001
	static _ := DllCall("user32\SetWinEventHook", UInt,0x8000, UInt,0x8001, Ptr,0, Ptr,RegisterCallback("OnExplorerStartEnd"), UInt,0, UInt,0, UInt,0, Ptr)
	DetectHiddenWindows, On
	WinGetClass, vWinClass, % "ahk_id " hWnd
	if !(vWinClass = "CabinetWClass")
	&& !(vWinClass = "ExploreWClass")
		return

	;didn't work
	static oArray := []
	if 0
	if (vEvent = 0x8000)
	{
		Sleep, 1000
		ControlGet, hCtl, Hwnd,, ToolbarWindow322, % "ahk_id " hWnd
		oArray[hWnd] := hCtl
	}
	else
	{
		ControlGetText, vText,, % "ahk_id " oArray[hWnd]
		WinGetTitle, vWinTitle, % "ahk_id " oArray[hWnd]
		MsgBox, % oArray[hWnd] "`r`n" vText
		MsgBox, % oArray[hWnd] "`r`n" vWinTitle
	}

	;didn't work
	if 0
	if (vEvent = 0x8001)
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.HWND = hWnd)
				MsgBox, % oWin.Document.Folder.Self.Path

	;didn't work
	if 0
	if (vEvent = 0x8001)
	{
		ControlGetText, vText, ToolbarWindow322, % "ahk_id " hWnd
		MsgBox, % vText
	}

	if (vEvent = 0x8001)
	{
		WinGetTitle, vWinTitle, % "ahk_id " hWnd
		MsgBox, % vWinTitle
	}

	if (vEvent = 0x8000)
		SoundPlay, *64
	else
		SoundPlay, *48
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Re: Is possible to get path when Explorer window is closing and restore when is opening?

31 May 2018, 17:23

Thank you.

Can you bit explain me that code?
I'm not so good in AHK scripting.
And where is for now name stored?

Miro
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is possible to get path when Explorer window is closing and restore when is opening?

03 Jun 2018, 23:54

- I have an explanation for some similar code, here:
Simple File Explorer Fix - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=48911
- SetWinEventHook checks for event numbers in the range specified (I specified from 0x8000 to 0x8001), and if such an event occurs, the callback function is notified.
- All 'static' lines are executed once and only once, when the script starts. The SetWinEventHook DllCall line is executed, and thus the OnExplorerStartEnd starts receiving window creation/deletion information.
- Since the window receives a message at the moment that a window is being deleted, it doesn't have much time to retrieve information from the window. I tried various techniques to retrieve the Explorer path, most of which failed, however retrieving the window title appeared to succeed.
- The variable vWinTitle contains the folder name, you could make it global by defining global vWinTitle at the top of the function. Also, you could choose a different name for the variable.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Re: Is possible to get path when Explorer window is closing and restore when is opening?

04 Jun 2018, 00:05

Yes, I know you helped me and I'm thankful.
But it always returns only title...

Miro
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is possible to get path when Explorer window is closing and restore when is opening?

04 Jun 2018, 00:12

Here's another approach, it periodically retrieves information for open Explorer windows.

Code: Select all

;detect closed Explorer folder windows

;based on JEE_ExpListOpenDirs:
;Explorer window interaction (folder windows/Desktop, file/folder enumeration/selection/navigation/creation) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=35041

ComObjError(False)
oWinPath := []
Loop
{
	oWinExist := []
	for oWin in ComObjCreate("Shell.Application").Windows
		if (oWin.Name = "Windows Explorer")
		{
			try
			{
				vPath := oWin.Document.Folder.Self.Path
				hWnd := oWin.HWND
				oWinPath[hWnd] := vPath
				, oWinExist[hWnd] := 1
			}
		}
	oWin := ""
	oWinDel := []
	for hWnd in oWinPath
		if !oWinExist.HasKey(hWnd)
		{
			MsgBox, % oWinPath[hWnd]
			oWinDel[hWnd] := 1
		}
	for hWnd in oWinDel
		oWinPath.Delete(hWnd)
	Sleep, 1000
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: arrondark, Descolada, just me, Rohwedder and 125 guests