如何让 Windows 资源管理器通过指定快捷键,在“新标签页”中打开指定文件夹?

如何让 Windows 资源管理器通过指定快捷键,在“新标签页”中打开指定文件夹?

问题描述

我想在 Windows 的文件资源管理器中实现这样一个功能:

  • 按下某个自定义快捷键
  • 自动在资源管理器的新标签页
  • 打开一个指定文件夹

例如:

  • 激活资源管理器窗口后,按 Alt + D,在资源管理器新标签页打开 D:\Downloads
  • 激活资源管理器窗口后,按 Alt + P,在资源管理器新标签页打开 D:\Projects

我的目标

我需要的效果是:

  1. 使用固定快捷键
  2. 打开指定路径
  3. 尽量不是新开一个资源管理器窗口
  4. 而是优先在当前资源管理器窗口的新标签页中打开

当前了解的情况

目前问题在于:

  • 快捷方式热键或 explorer.exe 通常会新开窗口

使用环境

  • 操作系统:Windows 11 (请按实际填写)
  • 资源管理器版本:系统自带
  • 是否接受第三方工具:是
  • 是否接受脚本方案:是
  • 是否接受 AutoHotkey:是

希望有经验的朋友帮忙解答,感谢。

既然一开始没看清楚, 但还是试了一下, ahk; 测试来说AHK是可以做到的,但是并不能完全做到, 主要问题是

  1. 如何识别 资源管理器窗口 , 当前打开并且在前台, 后台存在窗口切换到前台, 没有窗口打开新窗口
  2. 如何控制打开新标签页, 使用com控件, 模拟键盘操作等;
  3. 使用什么API, 打开文件夹的默认程序如果不是资源管理器

大模型提问可以很容易的提供一些方案, 但测试下来, 都有不少边际问题要处理, 不太好说能简单的贴一个代码; 但至少确实可以做到, 比如;

#Requires AutoHotkey v2.0
#SingleInstance Force

OpenInNewExplorerTab(TargetDir) {
    if !DirExist(TargetDir) {
        MsgBox("路径不存在:`n" TargetDir)
        return false
    }

    hwnd := WinExist("ahk_class CabinetWClass")

    if !hwnd {
        Run('explorer.exe "' TargetDir '"')
        return true
    }

    try {
        WinActivate("ahk_id " hwnd)
        WinWaitActive("ahk_id " hwnd, , 2)

        ; 新建标签页,等待渲染完成
        Send("^t")
        Sleep(600)

        ; 用 Alt+D 代替 Ctrl+L,更可靠地聚焦地址栏
        Send("!d")
        Sleep(300)

        ; 全选当前地址栏内容,再覆盖输入
        Send("^a")
        Sleep(100)

        SendText(TargetDir)
        Sleep(100)
        Send("{Enter}")

        return true
    } catch Error as e {
        Run('explorer.exe "' TargetDir '"')
        return false
    }
}

!t:: {
    myPath := "D:\Download"
    OpenInNewExplorerTab(myPath)
}

感谢 ,我贴一个现在正在使用的代码,之前在AHK论坛里找到的。
但也是不太稳定,有时会打开此电脑,有时会在当前的标签页打开。

!P::{
     try Explorer_Navigate_New_Tab("D:\Project")
     Return
 }
Explorer_Navigate_New_Tab(FullPath, hwnd := "") {

    if (hwnd) { ; make sure this Explorer window is active
        WinActivate("ahk_id " hwnd)
        WinWaitActive("ahk_id " hwnd)
    }
    else
        hwnd := WinExist("A") ; if omitted, use active window
    ProcessName := WinGetProcessName("ahk_id " hwnd)
    if (ProcessName != "explorer.exe") ; not Windows explorer
        return
    if (FullPath = "") {
        Send("^t") ; add a new tab
        return
    }

    ; Windows Explorer is the active window
    Send("^t") ; add a new tab
    ; Note 1: This hotkey works in English and French, don't know for other localizations
    ; Note 2: AFAIK, this hotkey does nothing in earlier versions of Windows Explorer but
    ; it would be safer to check the version before sending it
    Sleep(100) ; for safety

    ; the new tab is the active tab
    Explorer_Navigate_Tab(FullPath, hwnd)
}

Explorer_Navigate_Tab(FullPath, hwnd := "") {
    ; originally from Learning one (https://www.autohotkey.com/boards/viewtopic.php?p=4480#p4480)
    ; adapted by JnLlnd for tabbed browsing new to Windows 11 version 22H2 (build 22621.675)
    ; with code from ntepa (https://www.autohotkey.com/boards/viewtopic.php?p=488735#p488735)
    ; also works with previous versions of Windows Explorer
    hwnd := (hwnd = "") ? WinExist("A") : hwnd ; if omitted, use active window
    ProcessName := WinGetProcessName("ahk_id " hwnd)
    if (ProcessName != "explorer.exe") ; not Windows explorer
        return
    for pExp in ComObject("Shell.Application").Windows {
        if (pExp.hwnd = hwnd) { ; matching window found
            activeTab := 0
            try activeTab := ControlGetHwnd("ShellTabWindowClass1", "ahk_id" hwnd)
            if activeTab {
                static IID_IShellBrowser := "{000214E2-0000-0000-C000-000000000046}"
                shellBrowser := ComObjQuery(pExp, IID_IShellBrowser, IID_IShellBrowser)
                ;thisTab:=DllCall(NumGet(ComObjValue(shellBrowser),3*A_PtrSize, "UPtr"), "Ptr", shellBrowser, "UInt*")
                ComCall(3, shellBrowser, "uint*", &thisTab := 0)
                if (thisTab != activeTab) ; matching active tab
                    continue
                ObjRelease(ComObjValue(shellBrowser))
            }
            pExp.Navigate("file:///" FullPath)
            return
        }
    }
}


Win11现在是默认

感谢提醒,打开这个开关后可以直接用命令打开文件夹,且是在新标签页打开的

XYPlorer 好用

还是只想使用自带的文件管理器

使用了该方案,之后再AutoHotKey就可以了。例如:

#HotIf WinActive("ahk_class CabinetWClass") ;仅在资源管理器中使用
;打开指定文件夹
!D:: try Run("D:\Downloads")
#HotIf