Reading Win 10 File Explorer View Mode

Get help with using AutoHotkey and its commands and hotkeys
Harry
Posts: 4
Joined: 14 Feb 2017, 01:48

Reading Win 10 File Explorer View Mode

18 Feb 2017, 23:59

Hello everyone,

I want to end up with a variable containing a number between 1 and 8, representing the current file explorer view layout (Extra large icons, Large icons, Medium icons, List, Details, etc.). Ideally, it would be the number I would use with Ctrl-Shift-<#> shortcut to achieve that view.

The final result is a key that cycles through the views. I've found a few examples here that do something similar, but they all use radically different methods and are typically not commented, so I'm having trouble picking out the pieces I might need. For me, the only missing piece is reading the setting info.

Here's what I'm currently doing:

Code: Select all

; Select the key to drive the action. In my case, the numberpad slash key.
NumpadDiv::
;
; Check if file explorer (CabinetWClass in Win 10) is running on the system. 
	IfWinExist, ahk_class CabinetWClass
		{
; If it is running but not active (minimized or behind another window), shift focus back to it. 
			IfWinNotActive, ahk_class CabinetWClass
				WinActivate, ahk_class CabinetWClass
; Now that were in an active file explorer window, figure out what the current view setting is.
			view = ??? <------Help!
; Increment the view variable, but if it's out of range, reset it back to 1.
			if ++view > 8
				view = 1
; Rather than mess with dll or put commands, just translate the incremented view to a keyboard function.
;			Send, {Ctrl Down}{lshift Down}%view%{lshift Up}{Ctrl Up}
		}
; Otherwise, if no file explorer is running, use the Windows shortcut to launch one.
		Else
			Send, {LWin Down}e{LWin Up}
; And, we're done.
	Return
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Reading Win 10 File Explorer View Mode

19 Feb 2017, 02:32

I use the following for going through the modes when I press Ctrl+=:

Code: Select all

ExplorerViewChange_Window(explorerHwnd)
{
	if (!explorerHwnd)
		return
	Windows := ComObjCreate("Shell.Application").Windows
	for window in Windows
		if (window.hWnd == explorerHwnd)
			sFolder := window.Document
	if (sFolder.CurrentViewMode == 8)
		sFolder.CurrentViewMode := 6 ; Tiles
	else if (sFolder.CurrentViewMode == 6)
		sFolder.CurrentViewMode := 4 ; Details
	else if (sFolder.CurrentViewMode == 4)
		sFolder.CurrentViewMode := 3 ; List
	else if (sFolder.CurrentViewMode == 3) {
		sFolder.CurrentViewMode := 2 ; Small icons
		sFolder.IconSize := 16 ; Actually make the icons small...
	} else if (sFolder.CurrentViewMode == 2) {
		sFolder.CurrentViewMode := 1 ; Icons
		sFolder.IconSize := 48 ; Medium icon size
	} else if (sFolder.CurrentViewMode == 1) {
		if (sFolder.IconSize == 256)
			sFolder.CurrentViewMode := 8 ; Go back to content view
		else if (sFolder.IconSize == 48)
			sFolder.IconSize := 96 ; Large icons
		else
			sFolder.IconSize := 256 ; Extra large icons
	}
}

#If (exphWnd := WinActive("ahk_class CabinetWClass"))
^+::
^=::ExplorerViewChange_Window(exphWnd)
#If
return
For getting the current view mode of a window, read the value of sFolder.CurrentViewMode after finding the right Document.
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reading Win 10 File Explorer View Mode

19 Feb 2017, 11:38

This might work to set Details view:
PostMessage, 0x111, 28716, 0, , A ;Windows XP
PostMessage, 0x111, 28747, 0, SHELLDLL_DefView1, A ;Windows 7

Menu item IDs for Windows 7, obtained using:
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

Code: Select all

1:&Toolbars-ID=-1
2:Status &bar-ID=41474
3:{Empty String}-ID=0
4:E&xtra large icons-ID=28749
5:La&rge icons-ID=28751
6:&Medium icons-ID=28750
7:Small ico&ns-ID=28752
8:&List-ID=28753
9:&Details-ID=28747
10:Tile&s-ID=28748
11:Conten&t-ID=28754
12:{Empty String}-ID=-1
13:S&ort by-ID=-1
14:Grou&p by-ID=-1
15:{Empty String}-ID=-1
16:C&hoose details...-ID=28723
17:Customize this &folder...-ID=28722
18:{Empty String}-ID=32897
19:&Go To-ID=-1
20:&Refresh-ID=41504
@qwerty12 This is a key technique (CurrentViewMode), thanks so much.
Some great knowledge from you again, I wonder how you first came across it.
It led me to these links:
IFolderView interface (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775606(v=vs.85).aspx
IFolderView2 interface (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775541(v=vs.85).aspx

DoRename works on Windows 7, I haven't tested SetSortColumns, but it should allow sorting by columns.
[EDIT 2:]
A link re. sorting by columns:
Explorer column interaction (get/set: which appear, width, ascending/descending order) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=33129

[EDIT:]
Btw one thing I've been interested in is if there is a one-word method to invert selection. I know that you can use SendMessage/PostMessage to invoke the invert selection menu item, and that you can use Document.SelectedItems / Document.Folder.Items / Document.SelectItem, but a one-word solution via objects might exist, I just don't know.
[EDIT 3:]
Links re. invert selection/file selection:
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31755
Explorer window interaction (folder windows/Desktop, file/folder enumeration/selection/navigation/creation) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=35041
Last edited by jeeswg on 16 Jun 2019, 14:34, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Harry
Posts: 4
Joined: 14 Feb 2017, 01:48

Re: Reading Win 10 File Explorer View Mode

19 Feb 2017, 20:19

Thank you guys.

I've seen @qwerty12's technique in another message. I totally get the part that sets the type and icon size, but I'm still having trouble sorting out some of the elements I too have read through the IFolder materials (as jeeswg did), but I think I'm missing some background to really understand it. I could copy it, but I'm trying to grok it first.

I was indeed using a PostMessage technique at first, like jeeswg, but it scares me a bit because there seems to be discrepancies in the ID numbers between Win 10 (my system) and prior versions.

So, while I continue to study the interesting shell method, I've decided to brute force it. I've set an environment variable for persistence, and just read and increment it, skipping the views I'm not interested in. So my less than elegant "for now" solution is this:

Code: Select all

NumpadDiv::	;My key
	IfWinExist, ahk_class CabinetWClass
	{
		IfWinActive, ahk_class CabinetWClass			;Make sure we're in an active folder explorer.
		{
			EnvGet, curview, FEView				;Put environment FEView into curview variable .
			If !curview or curview < 1 or curview > 8	;If it's empty or out of range, fix that.
				curview := 5
			if ++curview > 8				;Increment it, but wrap around if it goes over the top.
				curview = 1
			If curview = 3					;Skip the views I don't like (3 & 4).
				curview := 5
			Send, {Ctrl Down}{lshift Down}%curview%{lshift Up}{Ctrl Up}
			EnvSet, FEView, %curview%			;Update the environment variable.
		}
		Else
			WinActivate, ahk_class CabinetWClass		;Make a dormant window active if needed.
	}
	Else
		Send, {LWin Down}e{LWin Up}				;Launch a folder explorer if needed.
Return
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reading Win 10 File Explorer View Mode

19 Feb 2017, 20:27

Regarding ID numbers, from:
best utilities + AutoHotkey scripts (+ useful tips) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28149

[HotkeyP][HotkeyP.zip contains spy.exe]
[spy.exe watches for WM_COMMAND messages, e.g. when you click menu items]
[note: this is not the same as Spy++ (spyxx.exe)]
[unlike many window spies that watch all messages directed at one window]
[spy.exe watches WM_COMMAND messages directed at all windows]
[it does not capture absolutely every WM_COMMAND message however]
Freeware programs including Precise Calculator
http://petr.lastovicka.sweb.cz/others.html
Last edited by jeeswg on 20 Feb 2017, 15:43, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Reading Win 10 File Explorer View Mode

20 Feb 2017, 14:27

jeeswg wrote: @qwerty12 This is a key technique (CurrentViewMode), thanks so much.
Some great knowledge from you again, I wonder how you first came across it.
The code to match a given Explorer HWND with the right Document I stole from somebody - Rapte_Of_Suzaku, I think. IIRC, there it was used to get the path displayed in an Explorer window. I used ComObjType() on the objects and eventually found my way to the same MSDN documents you linked. Looked at the available properties and put two and two together...
Btw one thing I've been interested in is if there is a one-word method to invert selection. I know that you can use SendMessage/PostMessage to invoke the invert selection menu item, and that you can use Document.SelectedItems / Document.Folder.Items / Document.SelectItem, but a one-word solution via objects might exist, I just don't know.
No idea, sorry. It seems Classic Shell did what you do - send WM_COMMAND - but moved onto just doing the inverting itself if you look at ClassicShellSrc_3_6_8.zip

Thanks for the Spy++ pointer, I've never considered sending WM_COMMAND before
Harry wrote: I've seen @qwerty12's technique in another message. I totally get the part that sets the type and icon size, but I'm still having trouble sorting out some of the elements I too have read through the IFolder materials (as jeeswg did), but I think I'm missing some background to really understand it. I could copy it, but I'm trying to grok it first.
If it helps any, I don't really "get" it... What I did was to keep changing the view myself with the button on the toolbar and then read sFolder.CurrentViewMode. The icons view mode provided some trouble as it's not actually the view that changes but rather the size of the icons - but MSDN cleared that up for me. Anyway, I'm glad you have something that works for you.
User avatar
jeeswg
Posts: 6655
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Reading Win 10 File Explorer View Mode

20 Feb 2017, 17:20

Btw there's a difference between Spy++ (spyxx.exe) and HotkeyP's spy.exe. I edited the post above, and the link it's from, to make it clearer.

Thanks for the information regarding Classic Shell, it is an interesting source for comparing with how I do things, it seems it has gone closed source now which is sad.
Classic Shell - Browse Files at SourceForge.net
https://sourceforge.net/projects/classicshell/files/

One classic thing that still seems impossible on Windows XP/Windows 7 is to get the paths of the files selected on the Desktop directly, without using the clipboard. However ... you can get the text from the selected items, enumerate the files in A_Desktop and A_DesktopCommon, and get their display names via SHGetFileInfo, compare the two lists, and if there are no ambiguities, retrieve the paths that way.

[EDIT:] To get the paths of files on the Desktop without using the clipboard:
get full paths of selected files on Desktop and Common File Dialogs - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=31135&p=154836#p154836
Last edited by jeeswg on 16 Jun 2019, 14:37, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
joefiesta
Posts: 274
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: Reading Win 10 File Explorer View Mode

11 Apr 2018, 09:55

@jeeswg: I found with great delight the following from above:

Code: Select all

q:: ;Explorer folder window messages (tested on Windows 7)
ControlGet, hCtl, Hwnd,, SHELLDLL_DefView1, A
PostMessage, 0x111, 31492, 0,, % "ahk_id " hCtl ;view, sort by, name
return
I am wondering if you know a way to sort by Modification Date. I googled stuff like "sort by" "name" 31492, and came up with nothing. so, I have no idea where you got Postmessage 31492. Should not a similar value exist for Modification Date?

I tried automating the sort by sending commands to open the Explorer menus. Alt + V: opens view menu. Then, "O" opens SORT
BY menu. Then a number of DOWN keys gets to the right field. The problem is: I can't know the number of DOWN keys to use because the SORT BY menu sequence is a function of the sequence of VIEW COLUMNS. And, I can't find a way to know which column (1, 2 or 3, etc) is the Date Modified column (since it may vary depending upon my folder and setting all folders the same just never seems to work well.)

I just found this post: https://autohotkey.com/boards/viewtopic.php?t=33129
It references IColumnManager, but after looking at that from Microsoft, I got nowhere on how to use the method.

Another edit:

Looking further, I found this: https://autohotkey.com/boards/viewtopic.php?t=33129

While at first it was confusing, I managed to wade through it and discover that what I was looking for was right there.

Return to “Ask For Help”

Who is online

Users browsing this forum: Godvicien, scriptor2016, swagfag and 148 guests