How to switch to the File Explorer Window that's opened to a specific folder path? Topic is solved
How to switch to the File Explorer Window that's opened to a specific folder path?
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.
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
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.
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
It doesn't work because it doesn't get the id of the window right.GEV wrote:post
I'm using Windows 10.
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
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.
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 ")"
}
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
I'm not willing to show the full path. It gets my tabs names on taskbar messy.gqqnbig wrote:Post
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.
Re: How to switch to the File Explorer Window that's opened to a specific folder path? Topic is solved
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
Thanks jeeswg. It works like charm.jeeswg wrote:Post
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
Can you explain why you need to add % in the WinActivate statement?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
Re: How to switch to the File Explorer Window that's opened to a specific folder path?
- 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.
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA