Library for accessing the Windows Explorer

Post your working scripts, libraries and tools.
User avatar
TheCrether
Posts: 1
Joined: 27 Mar 2024, 15:37
Contact:

Library for accessing the Windows Explorer

27 Mar 2024, 16:47

Hello! This is my first post here (even though I've been looking a lot into this forum and written some scripts ^^).

I made a library through which you can control the Windows Explorer programmatically. You can find the code on GitHub
The ExUtils class can control an explorer window, find out information about the current folder, items in folders, selected items, execute actions (verbs) on them and more. It also supports both Windows 10 and the Windows 11 Explorer Tabs

The code is has code comments in such a way that the AutoHotkey v2 Language Support Extension in VS Code can understand what types get returned and provide IntelliSense (it also describes the parameter options of most functions).
When I describe a tab, I mean both Windows 10 and Windows 11 because the Windows 10 explorer does theoretically have tabs, just one in one window :D
I will be working on automatically generating function documentation in the future so that it is easily possible to see the functionality of ExUtils.

The underlying ComObjects of the Tab, Folder, FolderItem etc. are wrapped in their own classes so you there can be extra functionality and good VS Code IntelliSense at the same time.

Examples

Copy the current path
Copies the path of the currently opened tab in Explorer and displays the copied paths in a tooltip

Code: Select all

#Requires AutoHotkey v2
#Include "ExplorerUtils.ahk"

CopyCurrentPath() {
	; get paths of selected items without quotes
	items := ExUtils.SelectedItems() ; you can give an other explorer window handle if you want. Defaults to the current window
	str := ""
	if items.Length == 0 {
		str := ExUtils.GetCurrentPath()
	}
	for item in items {
		str .= (str ? '`n' : '') . item.Path
	}
	A_Clipboard := str
	ToolTip("copied:`n" . str)
	SetTimer(() => ToolTip(), -2000)
}
Open a new tab
Open a new tab with a path supplied (only works on Windows 11)

Code: Select all

#Requires AutoHotkey v2
#Include "ExplorerUtils.ahk"

OpenNewTab() {
	ExUtils.GetCurrentExplorer().NewTab("C:\")
}
Edit the selected file (Calling the "edit" verb)
Gets the selected item and calls the "edit" verb on it

Code: Select all

#Requires AutoHotkey v2
#Include "ExplorerUtils.ahk"

EditFirstSelectedItem() {
	selectedItems := ExUtils.GetActiveTab().SelectedItems
	selectedItems[1].InvokeEdit()
}
Navigate to a folder
Navigates to a folder by setting the Path property of ExUtils.Tab

Code: Select all

#Requires AutoHotkey v2
#Include "ExplorerUtils.ahk"

Navigate() {
	tab := ExUtils.GetActiveTab()
	tab.Path := "C:\temp"
}

Known problems
  • The method ExUtils.FolderItems.Filter() (filter a collection of FolderItem objects) does not work for some reason (at least on my machine). I have implemented it theoretically but my tests have not been successful.
  • I have not a found a way to copy a FolderItems object, which makes for the following problem: When trying to use the ExUtils.Tab.SelectedItems property, the items will change if you navigate to another Tab
I am curious to get some feedback on what I could do differently (or some help with the above mentioned known problems ^^)

Cheers, Denis
windfancy3
Posts: 8
Joined: 21 Sep 2023, 02:08

Re: Library for accessing the Windows Explorer

29 Mar 2024, 09:01

Hello, thank you for the code.
I'm just getting started and can't understand all the code. The code doesn't seem to have the function of getting the name of the tab that is currently open. I currently use a code in the forum to use a shortcut to open the specified folder in just one window, but when a folder has been opened, it will open the folder again from a new tab. If there is code that can detect the opened tab and jump to that folder, it seems that the effect will be better.
Then I also looked at the code for get selected items. If I wanted to double-click in the background of a folder to return to the previous folder, it seemed impossible.
If there is actually a way to implement it, and it's just because I didn't understand the code, I would appreciate your advice.
iamMG
Posts: 28
Joined: 12 Nov 2017, 11:32

Re: Library for accessing the Windows Explorer

01 Apr 2024, 10:23

I haven't completely checked @TheCrether's code, but I am using these two functions to carry out a lot of tasks in the file explorer of Windows 10.

Explorer_GetSelection()
Description: Returns the currently selected items [folder(s)/file(s)] in the file explorer. (I don't know whether it works on Windows 11 or not)
Credits: @teadrinker
Link: viewtopic.php?t=60403#p255169

GetExplorerPath()
Description: Returns the path of the currently open folder (works with Windows 11 Tabs too, as far as I know.)
Credits: plankoe
Link: https://www.reddit.com/r/AutoHotkey/comments/10fmk4h/comment/kuplyts
windfancy3
Posts: 8
Joined: 21 Sep 2023, 02:08

Re: Library for accessing the Windows Explorer

01 Apr 2024, 21:10

I solved the problem of getting the path through @TheCrether's code,

Code: Select all

ExUtils.SelectedItems().Length
.
I didn't read the code carefully before. Sorry for wasting your time.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 118 guests