编程技术分享平台

网站首页 > 技术教程 正文

效率工具|autohotkey帮我节省了400次新建文件操作

xnh888 2024-11-09 14:45:12 技术教程 15 ℃ 0 评论

最近用autohotkey解决了一个需要重复几百次的工作任务,类似枯燥的的任务都可以用autohotkey来解决掉!

任务如下:

根据mp3文件名称,生成一个相同文件名称的文本文件和一个带有-1的文本文件,比如当我选中过零丁洋.mp3的时候,按下ctrl+win+t,就会生成下面这样的两个 txt文件。一开始我用笨办法,复制mp3文件名,再新建两个文本文件,把文件名粘贴过去,修改其中一个为-1, 全部弄完需要10多秒,有时候还容易出错,写成脚本就节省了自己大量的时间,正所谓磨刀不误砍柴工!

官网中文文档

autohotkey是一个制作按键脚本的工具,免费且强大!

https://www.autoahk.com/help/autohotkey/zh-cn/docs/AutoHotkey.htm

代码实战


^#t::
txtFileName := Explorer_GetSelection(hwnd)
txtFileName := RegExReplace(txtFileName, ".mp3#34;, "") 
MsgBox % "准备写入" txtFileName 

file := FileOpen(txtFileName "-1.txt", "w")
file.Write(txtFileName ".txt")
file.Close()

file := FileOpen(txtFileName ".txt", "w")
file.Write(txtFileName ".txt")
file.Close()
return

Explorer_GetSelection(hwnd="")   
{  
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")  
    WinGetClass class, ahk_id %hwnd%  
    if (process != "explorer.exe")  
        return  
    if (class ~= "Progman|WorkerW") {  
            ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%  
            Loop, Parse, files, `n, `r  
                ToReturn .= A_Desktop "\" A_LoopField "`n"  
        } else if (class ~= "(Cabinet|Explore)WClass") {  
            for window in ComObjCreate("Shell.Application").Windows 
            {
                try{
                if (window.hwnd==hwnd)  
                    sel := window.Document.SelectedItems  
                }catch e {
                    continue
            }
            }
            for item in sel  
                ToReturn .= item.path "`n"  
        }  
    return Trim(ToReturn,"`n")  
} 

解释下:

触发快捷键

^#t::

表示的就是当你按下ctrl+win+t的时候干什么。

符号描述:

符号

描述

#

Win(Windows 徽标键)

!

Alt

^

Ctrl

+

Shift

&

用于连接两个按键(含鼠标按键) 合并成一个自定义热键.

赋值

txtFileName := Explorer_GetSelection(hwnd)

从函数Explorer_GetSelection中获取到选中文件名称后,新建txtFileName变量并赋值。

正则替换

替换.mp3为空

RegExReplace(txtFileName, ".mp3#34;, "") 

显示对话框

MsgBox % "准备写入" txtFileName 

%是把后面的参数变成表达式,不加的话会出错。

字符串合并

非常简单,与其它编程语言不同的是,并不需要+号

"txtFileName: "  txtFileName

相当于js中的"txtFileName: " + txtFileName

新建文件

file := FileOpen(txtFileName "-1.txt", "w")
file.Write(txtFileName ".txt")
file.Close()
  • txtFileName "-1.txt"是目标文件名称,"w"表示写入模式打开文件
  • file.write是写入文件内容,本例中是写入
  • 记得最后使用file.Close()关闭文件

获取选中文件名称

直接拿过去用:

Explorer_GetSelection(hwnd="")   
{  
    WinGet, process, processName, % "ahk_id" hwnd := hwnd? hwnd:WinExist("A")  
    WinGetClass class, ahk_id %hwnd%  
    if (process != "explorer.exe")  
        return  
    if (class ~= "Progman|WorkerW") {  
            ControlGet, files, List, Selected Col1, SysListView321, ahk_class %class%  
            Loop, Parse, files, `n, `r  
                ToReturn .= A_Desktop "\" A_LoopField "`n"  
        } else if (class ~= "(Cabinet|Explore)WClass") {  
            for window in ComObjCreate("Shell.Application").Windows 
            {
                try{
                if (window.hwnd==hwnd)  
                    sel := window.Document.SelectedItems  
                }catch e {
                    continue
            }
            }
            for item in sel  
                ToReturn .= item.path "`n"  
        }  
    return Trim(ToReturn,"`n")  
} 

呼,节省的时间可以多陪陪家人了。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表