AutoHotKey神器的使用

最近利用AutoHotKey这个神器,写了几个脚本,自动化完成了平时工作中的一部分繁琐又重复的事情。总结一下编写脚本中遇到的问题:

             注意:脚本运行不正确,可能是文件编码不兼容,存为UTF-8 试试^_^

给脚本加上运行图标

icon.ico和脚本要放到同一个文件夹里

IfExist, icon.ico
{
Menu TRAY, Icon, icon.ico
}


AutoHotKey中热键脚本里调用其它热键

使用Gosub,把热键当做标签使用。

Gosub #s  ;#s是win+S热键
发送指令 Send 发送Win、Alt、Ctrl键的时候使用#、!、^,而不是{Alt}这种,

把.ahk文件打包成.exe文件

使用自带的Convert .ahk to .exe即可,还可以设置程序图标,如果想让exe程序双击就运行的话,可以把#g:: 这个设置的热键直接删除。


我写的脚本:

从服务器get新版本解压到本地目录

SetTitleMatchMode, MatchMode
SetTitleMatchMode, Fast|Slow
这是WinTitle 参数的匹配模式。一般必带!

#g::  ;热键是Windos键+G
SetTitleMatchMode, 2  
SetTitleMatchMode, slow  
IfWinNotExist,Q-Dir 5.70  ;这句话要想起作用,前面两句必须加!  
    Run C:\Program Files\Q-Dir\Q-Dir.exe  
Path = D:\work\release  
TimeNow = %A_YYYY%%A_MM%  ;%A_DD% 获取现在的年月,因为文件名是年月日组合,防止某日的所有编译都不成功,所以采用年月组合
FileList =  ;初始为空
FileName =    
rar = %A_ProgramFiles%\WinRAR\WinRAR.exe  
Loop,\\192.168.1.111\方案设计师\Main\%TimeNow%*.*,1,0  ;找到所有包含今天年月名的文件夹
    FileList = %FileList%%A_LoopFileName%`n  ;`n是换行符
Sort, FileList,R  ;逆序排列  
Loop, parse, FileList, `n  ;解析字符串,读取FileList  
{  
    if A_LoopField =  ;忽略列表末尾的空项.  
        continue  
    FileName = %A_LoopField% ;
    IfExist,\\192.168.1.111\方案设计师\Main\%FileName%\%FileName%.release.pdb.rar ;判断文件是否存在,因为有编译不成功的情况
        break  
}  
SetWorkingDir,\\192.168.1.111\方案设计师\Main\%FileName%\ 
FileGetTime,OutputVar,%FileName%.release.pdb.rar,c ;获取文件时间戳  
Loop, read, %Path%\更新历史.txt  
    last_line := A_LoopReadLine ;获取文本文件的最后一行,这里存着上次更新的文件的时间戳
if ( last_line = OutputVar )  
{  
    msgbox,4,,%FileName%:为最新可用文件夹,本地文件与服务器一致耶! `n`n按Y:将只拷贝“更新”文件夹下的DLL文件(如果有的话:D)...`n`n按N:我好闲,让我们再更新一次吧!  
    IfMsgBox Yes  
    {  
                IfExist,\\192.168.1.111\方案设计师\Main\%FileName%\更新\  
        {  
            FileCopy,\\192.168.1.111\方案设计师\Main\%FileName%\更新\*.dll,%Path%\bin  
            MsgBox,更新dell成功  
            return  
        }  
        else  
        {  
            MsgBox,48,,嘛也没有,白忙活一趟:-(  ;a joke ^_^
        }  
        return  
    }  
    else  
    {  
        Gosub GetV  
        FileAppend,%OutputVar%`n,%Path%\更新历史.txt  ;把刚更新的文件的时间戳写入txt的最后一行,方便下次读取比对
    }         
}  
else  
{  
    Gosub GetV  
    FileAppend,%OutputVar%`n,%Path%\更新历史.txt  ;写入  
}  
return  
  
  
;Gosub的标签  
GetV: ;获取方案设计师版本  
RunWait,%rar% X -o+  %FileName%.release.pdb.rar %Path% ;解压的命令行  
IfExist,\\192.168.1.111\方案设计师\Main\%FileName%\更新\  
{  
    FileCopy,\\192.168.1.111\方案设计师\Main\%FileName%\更新\*.dll,%Path%\bin  
}  
SoundPlay, %A_WinDir%\Media\ding.wav  
return

Visual Studio调试方案设计师

Visual Studio调试,自动编译后打开方案设计师加载上次的bdn文件,然后鼠标点击出图,同时以不保存的形式关闭CAD中所有打开的图纸。复杂的是考虑了各种调试期间的情况。取得VS现在的状态,靠的是检测左下角的提示信息文本。需要注意各个操作之间间隔时间不能太短,否则界面反应不过来。

;一键调试脚本!
#s::  ;Windows键+S 
SetTitleMatchMode, 2
SetTitleMatchMode, slow
IfWinExist,THLGaiLiangGJ - Microsoft Visual Studio,,(Running),  ;Visual Studio不在调试中的情况
{
	WinActivate
	Send ^{F7} ;Ctrl+F7 编译
	WinWait, - Microsoft Visual Studio,,,,Build started... ;等待含有Build started文本的窗口结束
	sleep 200
	WinWait, - Microsoft Visual Studio,Build succeeded,2,,
	IfWinExist, - Microsoft Visual Studio,Build succeeded,,
	{
		Send {F5}
		WinWait, 方案设计师V3.0
		Gosub,OpenBDN
		Gosub,Draw
		return
	}
	else
	{
		Gosub,SoundError
		return
	}
}
else ;Visual Studio在调试中的情况
{
	IfWinActive,(Running) - Microsoft Visual Studio,, ;如果Visual Studio窗口是当前是活动的窗口
	{
		IfWinExist,(Running) - Microsoft Visual Studio,.cpp*, ;.cpp文件代码有改动且未保存
		{
			Send ^+{F5} ;Ctrl+Shift+F5 重启调试
			sleep 1000
			WinWait, - Microsoft Visual Studio,,,,Build started...
			sleep 200
			IfWinExist,,There were build errors. ;如果Build Error
			{
				Gosub,SoundError
				return
			}
			else
			{
				IfWinNotExist,There were build errors.
				{
					WinWait,方案设计师V3.0
					Gosub,OpenBDN
					Gosub,Draw
					return
				}
				else ;未取到任何以上窗口文本
				{		
					MsgBox,0,,Not My Thing... ∑( ̄□ ̄) ,
					Gosub,SoundError
					return
				}
			}
		}
		else
		{
			IfWinExist,(Running) - Microsoft Visual Studio,.h*, ;.h文件代码有改动且未保存
			{
				Send ^+{F5} ;Ctrl+Shift+F5 重启调试
				sleep 1000
				WinWait, - Microsoft Visual Studio,,,,Build started...
				sleep 200
				IfWinExist,,There were build errors. ;如果Build Error
				{
					Gosub,SoundError
					return
				}
				else
				{
					IfWinNotExist,There were build errors.
					{
						WinWait,方案设计师V3.0
						Gosub,OpenBDN
						Gosub,Draw
						return
					}
					else ;未取到任何以上窗口文本
					{		
						MsgBox,0,,No Error! :) ,
						Gosub,SoundSucceed
						return
					}
				}
			}
			else ;文件无改动,如下断点
			{
				IfWinExist,方案设计师V3.0 - [D:\
				{
					WinActivate
					Gosub,Draw
					return
				}
				else
				{
					WinActivate,方案设计师V3.0,
					Gosub,OpenBDN
					Gosub,Draw
					return
				}
			}
		}
	}
	else
	{
		IfWinExist,方案设计师V3.0 ;如果未改动代码仅重新出图
		{
			WinActivate
			IfWinExist,方案设计师V3.0 - [D:\
			{
				Gosub,Draw
				return
			}
			else
			{
				Gosub,OpenBDN
				Gosub,Draw
				return
			}
		}
		else
		{
			MsgBox,0,,Have a rest! Enjoy! :-),
			Gosub,SoundSucceed
			return
		}
	}
}
return

OpenBDN:
SetTitleMatchMode, 2
SetTitleMatchMode, slow
WinMaximize
MouseClick, left, 40,40  ;相对座标,座标原点为激活窗口的左上角
sleep 100
MouseClick, left, 100,265  ;相对座标,座标原点为激活窗口的左上角
WinWait, ,打开项目成功!,
WinWait, ,就绪,
sleep 100
return

Draw:
SetTitleMatchMode, 2
SetTitleMatchMode, slow
WinMaximize
MouseClick, right, 200,805  ;相对座标,座标原点为激活窗口的左上角
sleep 200
MouseClick, left, 310, 630  ;相对座标,座标原点为激活窗口的左上角
sleep 200
;启动构件出图...
WinWait, ,系统即将启动构件绘图,请不要修改构件数据!,2, , ;超时前等待2秒
sleep 100
Send {space}
Gosub,CloseAllCAD
WinWait,方案设计师V3.0,,1200,,启动构件出图... ;超时前等待120秒
IfWinExist,方案设计师V3.0,构件出图结束,,,
{
	Gosub,RunCAD
	Gosub,SoundSucceed	
}
else
{
	Gosub,SoundError
}
return

CloseAllCAD:
IfWinExist,AutoCAD 2008 - [D:\
{
	WinActivate
	Send closeall{Enter}
	sleep 100
	Loop
	{
		IfWinExist,AutoCAD,是否将改动保存到
			send n
		sleep 1000
		IfWinNotExist,AutoCAD,是否将改动保存到
			break
	}
	IfWinExist 方案设计师V3.0
		WinActivate
}
return

RunCAD:
Run D:\L形盖梁钢筋_钢筋设计图纸.dxf
Run D:\work\L盖梁\LCapBeamV4\Drawing\L形盖梁柱式墩\L形盖梁钢筋构造图.dxf
return

SoundSucceed:  ;成功提示音
Loop 3
{
    SoundPlay, %A_WinDir%\Media\ding.wav
    sleep 1000
}
return

SoundError:  ;错误提示音
Loop 3
{
    SoundPlay,*16
    sleep 1000
}
return


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章