本帖最后由 sh1j1 于 2025-5-7 18:48 编辑
windows没有新建文本文档的快捷方式,反复新建很麻烦
上网没找到解决方式
询问gpt,他给出使用AutoHotkey编辑AHK脚本来实现快捷键功能
编辑AHK脚本
安装AutoHotkey后,右键桌面 → 新建 → AutoHotkey Script → 起个名字
以下是反复调试后能用的脚本,绑定在ctrl+alt+n快捷键
; 按下Ctrl+Alt+N创建新文本文件(无选中操作)
^!n::
targetPath := ""
; 获取资源管理器当前路径
if WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {
targetPath := GetActiveExplorerPath()
if (targetPath = "") {
targetPath := A_Desktop ; 回退到桌面
}
} else {
targetPath := A_Desktop ; 非资源管理器窗口使用桌面
}
; 规范路径格式
targetPath := RTrim(targetPath, "\") . "\"
; 生成唯一文件名
Loop {
newFile := targetPath "新建文本文件" (A_Index = 1 ? "" : " " A_Index) ".txt"
if !FileExist(newFile)
break
}
; 静默创建文件
try {
FileAppend, , %newFile%
} catch {
MsgBox 文件创建失败,请检查路径权限
}
return
; 辅助函数:获取当前资源管理器路径
GetActiveExplorerPath() {
hwnd := WinExist("A")
for window in ComObjCreate("Shell.Application").Windows {
try {
if (window.HWND == hwnd) {
return window.Document.Folder.Self.Path
}
}
}
return ""
}
设置脚本开机自启(可选)
- 按
Win + R 输入 shell:startup,打开启动文件夹
- 把你写的
.ahk 文件快捷方式放进去即可,系统每次启动自动加载脚本
实现效果
任意文件快捷方式
感谢评论区大佬指教,增加了输入框,可自定义任意文件,批量创建文件还是用bat脚本比较好
^!n::
targetPath := ""
; 获取资源管理器当前路径
if WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {
targetPath := GetActiveExplorerPath()
if (targetPath = "")
targetPath := A_Desktop
} else {
targetPath := A_Desktop
}
targetPath := RTrim(targetPath, "\") . "\"
; 弹出输入框,默认是“新建文件.txt”
defaultName := "新建文件.txt"
InputBox, userInput, 新建文件, 请输入文件名(可带扩展名):, , 300, 130,,, , %defaultName%
if ErrorLevel || userInput = ""
return
; 检查是否存在同名文件,自动编号
Loop {
newFile := targetPath . userInput
if (A_Index > 1) {
SplitPath, userInput, name, dir, ext, nameNoExt
if (ext = "")
newFile := targetPath . nameNoExt . " " . A_Index
else
newFile := targetPath . nameNoExt . " " . A_Index . "." . ext
}
if !FileExist(newFile)
break
}
; 创建文件
try {
FileAppend, , %newFile%
} catch {
MsgBox 文件创建失败,请检查路径权限
}
return
; 获取当前资源管理器路径
GetActiveExplorerPath() {
hwnd := WinExist("A")
for window in ComObjCreate("Shell.Application").Windows {
try {
if (window.HWND == hwnd)
return window.Document.Folder.Self.Path
}
}
return ""
}
help
官方帮助文档
英文:Quick Reference | AutoHotkey v2
社区翻译版
中文翻译:快速参考 | AutoHotkey v2
|