How to switch to the File Explorer Window that's opened to a specific folder path? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
omareg94
Posts: 88
Joined: 27 Jun 2016, 22:46

How to switch to the File Explorer Window that's opened to a specific folder path?

Post by omareg94 » 27 Jan 2018, 01:40

I have a folder path stored in a variable. And, I have multiple explorer.exe (File Explorer) windows, each is opened to a different path. I want Autohotkey to activate the explorer.exe (File Explorer) window that's opened to that specific folder path.

GEV
Posts: 844
Joined: 25 Feb 2014, 00:50

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by GEV » 27 Jan 2018, 03:27

Code: Select all

folder_path := "C:\Windows\Temp"

; Get fullpath of all opened explorer windows:
; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
for window in ComObjCreate("Shell.Application").Windows
{
	try explorer_path := window.Document.Folder.Self.Path 
	If (explorer_path = folder_path)
	{
		WinGet, id, id, %explorer_path%
		WinActivate, ahk_id %id%
			break			
	}
}
Last edited by GEV on 27 Jan 2018, 03:43, edited 2 times in total.

omareg94
Posts: 88
Joined: 27 Jun 2016, 22:46

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by omareg94 » 27 Jan 2018, 03:34

GEV wrote:post
It doesn't work because it doesn't get the id of the window right.

I'm using Windows 10.

gqqnbig
Posts: 13
Joined: 27 Jul 2014, 15:00

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by gqqnbig » 27 Jan 2018, 04:15

I'm in favor of GEV's solution but there is my two cents.

My code utilize the pattern that the title of the explorer window shows the folder name. So my script gets the last folder name of your desired path, search for all opened explorer windows and find the match.

Note if there are explorer windows opening two paths that ends with the same folder name, my script may activate the wrong window. If it's a concern, you may go to Folder Options of explorer, and check "Display full path in the title bar".

The script is tested in Windows 10.

Code: Select all


targetPath:="C:\Program Files"

folderName:= GetLastFolderName(targetPath)
WinGet, explorerWindows, List, ahk_exe explorer.exe

loop, %explorerWindows% {
	val:=explorerWindows%A_Index%

	WinGetTitle, title, ahk_id %val%
	IfInString, title, %folderName%
	{
		WinActivate, ahk_id %val%
		break  
	}
}

GetLastFolderName(path) {
	StringGetPos, p2, path,\ , R
	if(p2+1=StrLen(path)) ; The path ends with \.
	{
		StringGetPos, p1, path,\ , R2
		
		if(p1>-1)
			return SubStr(path, p1+2,p2-p1-1)
		else
			return SubStr(path, 1, p2)
	}
	else if(p2>-1)
		return SubStr(path, p2+2)
	else
		return "(" path ")"
	
}

omareg94
Posts: 88
Joined: 27 Jun 2016, 22:46

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by omareg94 » 27 Jan 2018, 04:28

gqqnbig wrote:Post
I'm not willing to show the full path. It gets my tabs names on taskbar messy.

I tried something like window.Document.Folder.Self.ID and window.Document.Folder.Self.WinID to get the window's id. But none of them seems to work. I really don't know because I'm not informed with the Microsoft COM.

It needs linking each window's ID with the folder path.

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

Re: How to switch to the File Explorer Window that's opened to a specific folder path?  Topic is solved

Post by jeeswg » 27 Jan 2018, 05:45

A slight mod of GEV's script:

Code: Select all

q:: ;explorer - activate window that matches folder
folder_path := "C:\Windows\Temp"

; Get fullpath of all opened explorer windows:
; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
for window in ComObjCreate("Shell.Application").Windows
{
	explorer_path := ""
	try explorer_path := window.Document.Folder.Self.Path
	if (explorer_path = folder_path)
	{
		WinActivate, % "ahk_id " window.HWND
			break
	}
}
window := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

omareg94
Posts: 88
Joined: 27 Jun 2016, 22:46

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by omareg94 » 27 Jan 2018, 06:22

jeeswg wrote:Post
Thanks jeeswg. It works like charm. :)

gqqnbig
Posts: 13
Joined: 27 Jul 2014, 15:00

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by gqqnbig » 27 Jan 2018, 11:43

jeeswg wrote:A slight mod of GEV's script:

Code: Select all

q:: ;explorer - activate window that matches folder
folder_path := "C:\Windows\Temp"

; Get fullpath of all opened explorer windows:
; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
for window in ComObjCreate("Shell.Application").Windows
{
	explorer_path := ""
	try explorer_path := window.Document.Folder.Self.Path
	if (explorer_path = folder_path)
	{
		WinActivate, % "ahk_id " window.HWND
			break
	}
}
window := ""
return
Can you explain why you need to add % in the WinActivate statement?

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

Re: How to switch to the File Explorer Window that's opened to a specific folder path?

Post by jeeswg » 27 Jan 2018, 15:52

- There are two styles: traditional and expression. Both can handle variables, but only expression-style can handle objects/keys.
- The use of % is a bit quirky, but it's because the traditional-style has many limitations, it was introduced as a way to specify expression-like (function-like) parameters in commands. In AHK v2, everything will be more sensible.

Code: Select all

;variable (AHK v1)
WinActivate, ahk_id %hWnd% ;traditional
WinActivate, % "ahk_id " hWnd ;expression

;object (AHK v1)
WinActivate, % "ahk_id " obj.hWnd ;expression

;==============================

;variable (AHK v2)
WinActivate("ahk_id " hWnd) ;expression

;object (AHK v2)
WinActivate("ahk_id " obj.hWnd) ;expression
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Post Reply

Return to “Ask For Help”