Check Current File Explorer View Mode Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WalterRoberts
Posts: 67
Joined: 25 Feb 2020, 20:00

Check Current File Explorer View Mode

18 May 2021, 01:41

I need to check whether the current view mode in File Explorer is set to 'Details'. I have managed to achieve this using Objects. My current understanding of Objects is rather limited.
The following code works to my knowledge fully as expected. Is there something I still could improve?

Code: Select all

	CheckDetailsView() {
		oWin := ""
		WinGet, hWnd, ID, A
		for oWin in ComObjCreate("Shell.Application").Windows
			if (oWin.Document.CurrentViewMode == 4)	;Details View is equivalent to 4
				return true
		return false
	}

F1::
	if (CheckDetailsView())
		MsgBox, 'Details View' is active.
	else
		MsgBox, Other View currently selected.
	return

I would really like to get this also working in #If-directives for better maintainability down the road:

Code: Select all

;Is this somehow possible to achieve?
#If (CheckDetailsView()) && WinActive("ahk_class CabinetWClass")

	F2::MsgBox, 'Details View' is active.
#If
User avatar
boiler
Posts: 17032
Joined: 21 Dec 2014, 02:44

Re: Check Current File Explorer View Mode  Topic is solved

18 May 2021, 02:04

You’re not doing anything with the hWnd after you get it, so you can remove that. Also, you can return the result of the comparison to the value 4 directly since the result is true or false.

Code: Select all

CheckDetailsView() {
	for oWin in ComObjCreate("Shell.Application").Windows
		return oWin.Document.CurrentViewMode = 4	;Details View is equivalent to 4
}

#If CheckDetailsView() && WinActive("ahk_class CabinetWClass")
F2::MsgBox, 'Details View' is active.
#If 
F2::MsgBox, Other View currently selected or File Explorer not active window.
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Check Current File Explorer View Mode

18 May 2021, 02:05

If you study Shell.Application you can easily make many useful functions, like following one which you needed

Code: Select all

f1::
msgbox, % CheckView() 
return

CheckView() 
{
	WinGetActiveTitle, Title		; where we get active title
	for oWin in ComObjCreate("Shell.Application").Windows
		if( oWin.LocationName = Title)		; here we match active title
			return oWin.Document.CurrentViewMode		; and here we get matched title CurrentViewMode 
	return "No folder is selected"
}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
WalterRoberts
Posts: 67
Joined: 25 Feb 2020, 20:00

Re: Check Current File Explorer View Mode

18 May 2021, 10:17

boiler wrote: You’re not doing anything with the hWnd after you get it, so you can remove that. Also, you can return the result of the comparison to the value 4 directly since the result is true or false.
Thank you, this is exactly the simplification I was looking for! I overlooked implementing the obtained hWnd; this is mandatory so when multiple Explorer windows are open it will still yield a consistent result with the active one.

Code: Select all

CheckDetailsView() {
	WinGet, hWnd, ID, A
	for oWin in ComObjCreate("Shell.Application").Windows
		if (oWin.hWnd = hWnd)
			return oWin.Document.CurrentViewMode = 4	;Details View is equivalent to 4
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Darkmaster006 and 341 guests