我是新手,网上搜的都是AutoHotkey V1.
但我想使用 autohotkey V2 .
在 Windows 10 或 11 上获取当前选择的文件或文件夹路径和文件名。
原问题的答案: 如何获取当前文件或文件夹路径和文件名?
#Requires AutoHotkey v1.1
#NoEnv
#SingleInstance Force
; Create a group of the windows that contain files and/or folders:
; ahk_group ExplorerDesktopGroup
GroupAdd, ExplorerDesktopGroup, ahk_class ExploreWClass
GroupAdd, ExplorerDesktopGroup, ahk_class CabinetWClass
GroupAdd, ExplorerDesktopGroup, ahk_class Progman
GroupAdd, ExplorerDesktopGroup, ahk_class WorkerW
return
#IfWinActive ahk_group ExplorerDesktopGroup
; Press Win+P in explorer or desktop
; to get the path of the selected items in a message box
#p::MsgBox % Explorer_GetSelection()
#IfWinActive
Explorer_GetSelection() {
; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255169
WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
Return
shellWindows := ComObjCreate("Shell.Application").Windows
if (winClass ~= "Progman|WorkerW")
shellFolderView := shellWindows.Item( ComObject(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
else {
for window in shellWindows
if (hWnd = window.HWND) && (shellFolderView := window.Document)
break
}
for item in shellFolderView.SelectedItems
result .= (result = "" ? "" : "`n") . item.Path
Return result
}
编辑:
我设法使用 AHK-v2-script-converter 将它转换为 v2:
#Requires AutoHotkey v2.0
#SingleInstance Force
; Create a group of the windows that contain files and/or folders:
; ahk_group ExplorerDesktopGroup
GroupAdd("ExplorerDesktopGroup", "ahk_class ExploreWClass")
GroupAdd("ExplorerDesktopGroup", "ahk_class CabinetWClass")
GroupAdd("ExplorerDesktopGroup", "ahk_class Progman")
GroupAdd("ExplorerDesktopGroup", "ahk_class WorkerW")
return
#HotIf WinActive("ahk_group ExplorerDesktopGroup")
; Press Win+P in explorer or desktop
; to get the path of the selected items in a message box
#p:: MsgBox(Explorer_GetSelection())
#HotIf
Explorer_GetSelection() {
; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255169
result := ""
winClass := WinGetClass("ahk_id" . hWnd := WinExist("A"))
if !(winClass ~= "^(Progman|WorkerW|(Cabinet|Explore)WClass)$")
Return
shellWindows := ComObject("Shell.Application").Windows
if (winClass ~= "Progman|WorkerW")
shellFolderView := shellWindows.Item( ComValue(VT_UI4 := 0x13, SWC_DESKTOP := 0x8) ).Document
else {
for window in shellWindows
if (hWnd = window.HWND) && (shellFolderView := window.Document)
break
}
for item in shellFolderView.SelectedItems
result .= (result = "" ? "" : "`n") . item.Path
Return result
}
• “没有这样的文件或目录:'./settingsicon.png'” PIL
• 当文件位于 Android Studio 的 drawbale 文件夹中时,如何使用 Kotlin 获取文件路径?
• 在文档管理系统 (DMS) 中呈现文件夹路径的优化方法是什么?
• 如何在 Power Query 中找不到数据源时跳过文件的处理?
• 如何在 Linux 中获取多个子文件夹中的文件列表以及它们的绝对路径
• Get-ChildItem 使用多个路径,并检索所有匹配的文件及其路径 - 只有服务器名称
• 如何将 .gz 文件解压到当前目录? [Python 3.11]