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

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

任务如下:

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

image-20210913102849141

官网中文文档

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

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

代码实战


^#t::
txtFileName := Explorer_GetSelection(hwnd)
txtFileName := RegExReplace(txtFileName, ".mp3$", "") 
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$", "") 

显示对话框

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")  
} 

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱玩的安哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值