AutoIt应用技巧示例[转]

用AutoIt实现了一个相对复杂的操作,其中用到了不少典型特性,在此总结一下。

### 遍历窗口

$var=WinList("7-Zip 文件管理器")
For $i=1 to $var[0][0]
WinActivate($var[$i][0])
WinWaitActive($var[$i][0])
……
Next

WinList返回二维数组,$var[0][0]为匹配的窗口个数,$var[$i][0]为窗口标题,$var[$i][1]为窗口句柄

### 字符串输入

ClipPut($printdir)
Send("^v{Enter}")

简单的输入可以直接用Send()模拟键盘动作,对于复杂的字符串,可以借助剪贴板函数ClipPut

### 遍历文件

$dwg_handle = FileFindFirstFile("*.dwg")
While 1
$dwg = FileFindNextFile($dwg_handle)
If @error Then ExitLoop
……
WEnd

遍历目录下所有的.dwg文件

### 文件读写

$outfile = FileOpen($printfile, 2)
if $outfile = -1 Then
MsgBox(0,"Error","Unable to create file")
Exit
EndIf

FileWriteLine($outfile, 'AutoCAD 2000 Batch Plot List File')

这部分的功能不是特别强,之提供了简单的行读写函数。而我的任务需要将每个文件的信息写入列表,文件的总数则在列表的开头,因此只好调用了Notepad进行查找替换,谁让windows下没有自带的Sed呢。

完整的示例如下,这个脚本的功能是遍历系统中的7zip窗口,将压缩包内的文件夹解压,然后把所有文件夹中的.dwg文件按一定的格式写入打印列表,并用AutoCAD批处理打印程序打开。所有的.doc文件用Word打开,


;功能:生成AutoCAD批处理打印列表
;作者:rogerz.zhang
;日期:2006.11.24

Opt("WinWaitDelay",100)
Opt("WinTitleMatchMode",2)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)

$printdir = "C:\Documents and Settings\public-proj\桌面\打印"
$printfile = "C:\Documents and Settings\public-proj\桌面\print.bp3"
$word = "c:\Program Files\Microsoft Office\Office10\WinWord.exe"
$st = '"C:\Documents and Settings\public-proj\My Documents\st.dwg|st"'
$count = 0

Unzip()
CreateList()
EditList()
BatchPlot()

Func Unzip()
$var=WinList("7-Zip 文件管理器")
For $i=1 to $var[0][0]
WinActivate($var[$i][0])
WinWaitActive($var[$i][0])
Send("+{NUMPADADD}{F5}")
WinWaitActive("复制")
ClipPut($printdir)
Send("^v{Enter}")
WinWaitActive($var[$i][0])
Send("!{F4}")
Next
EndFunc

Func CreateList()
$outfile = FileOpen($printfile, 2)
if $outfile = -1 Then
MsgBox(0,"Error","Unable to create file")
Exit
EndIf
FileWriteLine($outfile, 'AutoCAD 2000 Batch Plot List File')
FileWriteLine($outfile, '"BATCH FILE NAME",' & $printfile)
FileWriteLine($outfile, '"BATCH JOURNAL LOG ON?",#TRUE#')
FileWriteLine($outfile, '"BATCH JOURNAL LOG FILENAME","C:\Program Files\AutoCAD 2002\Support\BatchPlt\BPJOURNL.LOG"')
FileWriteLine($outfile, '"BATCH JOURNAL HEADER",""')
FileWriteLine($outfile, '"BATCH JOURNAL USER COMMENT",""')
FileWriteLine($outfile, '"BATCH JOURNAL APPEND?",#FALSE#')
FileWriteLine($outfile, '"BATCH ERROR LOG ON?",#TRUE#')
FileWriteLine($outfile, '"BATCH ERROR LOG FILENAME","C:\Program Files\AutoCAD 2002\Support\BatchPlt\BPERROR.LOG"')
FileWriteLine($outfile, '"BATCH ERROR HEADER",""')
FileWriteLine($outfile, '"BATCH ERROR APPEND?",#FALSE#')
FileWriteLine($outfile, '"BATCH NUM FILES",__COUNT__')
FileWriteLine($outfile, '"BATCH WIN SIZE","160,24"')
FileChangeDir($printdir)
$dir_handle = FileFindFirstFile("*.*")
If $dir_handle = -1 Then
MsgBox(0, "Error", "No files/directories matched the search pattern")
Exit
EndIf
While 1
$dir = FileFindNextFile($dir_handle)
If @error Then ExitLoop
FileChangeDir($dir)
$dwg_handle = FileFindFirstFile("*.dwg")
$doc_handle = FileFindFirstFile("*.doc")
While 1
$dwg = FileFindNextFile($dwg_handle)
If @error Then ExitLoop
$count = $count + 1
FileWriteLine($outfile, '"DWG#' & $count & 'FILENAME","' & @WorkingDir & '\' & $dwg & '"')
FileWriteLine($outfile, '"DWG#' & $count & 'LAYOUT","*当前选项卡*"')
FileWriteLine($outfile, '"DWG#' & $count & 'PLOTSETUP",' & $st)
FileWriteLine($outfile, '"DWG#' & $count & 'PLOTDEVICE","缺省"')
FileWriteLine($outfile, '"DWG#' & $count & 'LAYERS",""')
FileWriteLine($outfile, '"DWG#' & $count & 'PLOTAREA",""')
FileWriteLine($outfile, '"DWG#' & $count & 'PLOTSCALE",""')
FileWriteLine($outfile, '"DWG#' & $count & 'SCALECUSTOM",#FALSE#')
FileWriteLine($outfile, '"DWG#' & $count & 'SCALELW",#FALSE#')
FileWriteLine($outfile, '"DWG#' & $count & 'PLOTFILE",""')
WEnd
While 1
$doc = FileFindNextFile($doc_handle)
if @error Then ExitLoop
; Run($word & ' "' & @WorkingDir & '\' & $doc & '" /mFilePrintDefault', "", @SW_HIDE)
Run($word & ' "' & @WorkingDir & '\' & $doc, "")
WEnd
FileChangeDir("..")
FileClose($doc_handle)
FileClose($dwg_handle)
WEnd
FileClose($outfile)
FileClose($dir_handle)
EndFunc

Func EditList()
Run("notepad " & $printfile)
If not WinActive("print - 记事本") Then WinActivate("print - 记事本")
WinWaitActive("print - 记事本")
Send("^h")
WinWaitActive("替换")
Send("__COUNT__{tab}")
ClipPut($count)
Send("^v!a")
Sleep(50)
Send("{ESC}")
iF Not WinActive("print - 记事本") Then WinActive("print - 记事本")
WinWaitActive("print - 记事本")
Send("^s")
Sleep(50)
Send("!{F4}")
EndFunc

Func BatchPlot()
If Not WinExists("AutoCAD 批处理打印实用程序") Then
Run("C:\Program Files\AutoCAD 2002\Support\BatchPlt\batchplt.exe")
WinWaitActive("AutoCAD 2002")
WinWaitActive("AutoCAD 批处理打印实用程序")
EndIf
If Not WinActive("AutoCAD 批处理打印实用程序") Then WinActivate("AutoCAD 批处理打印实用程序")
WinWaitActive("AutoCAD 批处理打印实用程序")
Send("^o")
WinWaitActive("打开批处理打印列表文件")
ClipPut($printfile)
Send("^v{Enter}")
EndFunc

posted @ 2008-01-18 16:31  疾行者  阅读(1568)  评论(0编辑  收藏  举报