Thanks for the feedback. I hadn't used Shell.NameSpace for windowless COM scripting before, nice to learn new things.
I looked again at the Microsoft docs. Here is my updated attempt to fill in the gap. I'm trying to spell out every step to make it stick in my memory. Perhaps I'm still missing or confusing something and in that case let me know and help me get it right.
COM, Windows Shell and File Explorer windows
To use COM to do things with a File Explorer window we have to take a few steps through the Windows Shell hierachy.
I use this script snippet as a starting point and spell out some of its steps.
Code: Select all
;HWND to the active Explorer window
handle := WinExist("A")
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd = handle)
{
sfv := window.Document
path := sfv.Folder.Self.Path
;do something with path to the active Explorer window ...
}
}
ComObjCreate("Shell.Application").Windows
This AHK code uses what Microsoft names the
Shell.Windows method to get a
ShellWindows object that "represents a collection of all of the open windows that belong to the Shell."
for window in ComObjCreate("Shell.Application").Windows
This AHK code iterates the
ShellWindows object and at each step gets "an InternetExplorer object that represents the Shell window." But the
InternetExplorer object
doc page is a little confusing, for four reasons.
- The name "InternetExplorer" sounds like something specific to the old Internet Explorer browser built into Windows. But in reality this object can also represent other things, like a File Explorer window.
- The doc page does not explicitly mention File Explorer windows, even though it partly applies to them.
- Some of the methods and properties are not usable on File Explorer windows.
- The doc page does not document the property hwnd, which returns the window's handle: window.hwnd
sfv := window.Document
This AHK code uses the
InternetExplorer object's property
Document to get a
ShellFolderView object.
I 'm naming it "sfv", short for ShellFolderView, like I've seen others on the AutoHotkey forums do.
This step can be confusing because of a gap in Microsoft's documentation:
- The docs page for Document property does not say that it gives us a ShellFolderView object.
- The docs page for ShellFolderView object does not say that we get it from the Document property.
Once that connection is made clear the pieces fit together.
The
ShellFolderView object "Represents the objects in a view and provides properties and methods used to obtain information about the contents of the view." In other words, it lets us do things with the File Explorer window (change sort order, select files, toggle between icon view and list view, ...) and with its files and folders (open a file, move a file, ...).
Methods/properties to read or change the File Explorer window
For example
SelectedItems ,
SelectItem ,
FocusedItem and
CurrentViewMode
They are listed on the docs page
shellfolderview and some more here
ishellfolderviewdual ,
ishellfolderviewdual2 ,
ishellfolderviewdual3
Methods/properties for doing things with the files and folders
This takes two steps.
- Use the ShellFolderView object's Folder property to get a Folder object.
- Then use the Folder object's properties/methods to do things with one or more of its files and (sub)folders. They are listed at folder and folder2-object
For example we can use the
Folder object's
FolderItems method to get a
FolderItems object and iterate over it to get each
FolderItem object's
Name property. In short, get the name of each file in the File Explorer window.
Code: Select all
for item in sfv.Folder.Items
MsgBox % item.Name
Right?