浅析autoit

AutoIt v3 是用以编写并生成具有 BASIC 语言风格的脚本程序的免费软件,它被设计用来在Windows GUI(用户界面)中进行自动操作.通过它可以组合使用模拟键击,鼠标移动和窗口/控件操作等来实现自动化任务,而这是其它语言所无法做到或尚无可靠方法实现的(比如VBScript和SendKeys). AutoIt 非常小巧,完全运行在所有windows操作系统上.并且不需要任何"运行库".

AutoIt 最初是为PC(个人电脑)的"批量处理"而设计,用于对数千台PC进行(同样的)配置.现在,autoit是一个支持复杂表达式,自定义函数,循环等的强大脚本软件. AutoIt 可以做的事:

  • 简单易懂的类 BASIC 表达式
  • 模拟键盘,鼠标动作事件
  • 操作窗口与进程
  • 直接与窗口的"标准控件"交互(设置/获取 文字,移动,关闭,等等)
  • 脚本可以编译为标准可执行文件
  • 创建用户图形界面接口(GUI)
  • COM支持
  • 正则表达式
  • 直接调用外部DLL 和 Windows API 函数
  • 程序运行为功能(让程序运行于其它账户)
  • 详细易懂的帮助文件于基于社区的支持论坛
  • 完全兼容于 Windows 2000 / XP / 2003 / Vista / 2008 / 7
  • Unicode 与 64位 运算支持
  • 高精度,易使用的数学运算
  • 可以运行于 Windows Vista Account Control (UAC)

AutoIt 被设计得尽可能小,并且不用依赖外部DLL文件或添加注册表项目即可独立运行.也可以安全的成为服务运行.脚本可以使用Aut2Exe编译为可独立运行的文件。

 

下载链接:https://www.autoitscript.com/site/autoit/downloads/

在线文档:http://www.autoitx.com/Doc/

中文论坛:http://www.autoitx.com/

官网:https://www.autoitscript.com/site/autoit/

 

基本函数:

MsgBox:

#include 
MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "This is line 1" & @CRLF & "This is line 2" & @CRLF & "This is line 3")

Do....Until,If ....Then....EndIf:

#include 
Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example", "This script will count to 5 using a 'Do' loop.  Do you want to run it?")
If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "OK.  Bye!")
	Exit
EndIf
Local $iCount = 1
Do
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Count is: " & $iCount)
	$iCount = $iCount + 1 ; Alternatively $iCount += 1 can be used to increase the value of $iCount by one
Until $iCount > 5

MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Finished!")

For....Next:

#include 

Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example", "This script will count to 5 using a 'For' loop.  Do you want to run it?")

If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "OK.  Bye!")
	Exit
EndIf

For $iCount = 1 To 5
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Count is: " & $iCount)
Next


MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Finished!")

While....WEnd:

#include 

Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example", "This script will count to 5 using a 'While' loop.  Do you want to run it?")

If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "OK.  Bye!")
	Exit
EndIf

Local $iCount = 0

While $iCount < 5
	$iCount = $iCount + 1 ; Alternatively $iCount += 1 can be used to increase the value of $iCount by one

	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Count is: " & $iCount)
WEnd

MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Finished!")

Func....EndFunc:

#include 

Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example", "This script will call a couple of example functions.  Do you want to run it?")


If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt", "OK.  Bye!")
	Exit
EndIf

TestFunc1()
TestFunc2(20)

MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Finished!")
Exit



Func TestFunc1()
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Inside TestFunc1()")
EndFunc 


Func TestFunc2($vVar)
	MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "Inside TestFunc2() - $vVar is: " & $vVar)
EndFunc

InputBox:

#include 

Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example (English Only)", "This script will open an input box and get you to type in some text.  Do you want to run it?")

If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt", "OK.  Bye!")
	Exit
EndIf


Local $iLoop = 1, $sText = ""
While $iLoop = 1
	$sText = InputBox("AutoIt Example", "Please type in the word ""autoit"" and click OK")
	If @error = 1 Then
		MsgBox($MB_SYSTEMMODAL, "Error", "You pressed 'Cancel' - try again!")
	Else
		If $sText <> "autoit" Then
			MsgBox($MB_SYSTEMMODAL, "Error", "You typed in the wrong thing - try again!")
		Else
			$iLoop = 0 
		EndIf
	EndIf
WEnd

MsgBox($MB_SYSTEMMODAL, "AutoIt Example", "You typed in the correct word!  Congrats.")

Run操作txt:

#include 

Local $iAnswer = MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), "AutoIt Example", "This script will run Notepad, type in some text and then quit.  Do you want to run it?")

If $iAnswer = 7 Then
	MsgBox($MB_SYSTEMMODAL, "AutoIt", "OK.  Bye!")
	Exit
EndIf

Run("notepad.exe")

WinWaitActive("[CLASS:Notepad]")

Send("Hello from Notepad.{ENTER}1 2 3 4 5 6 7 8 9 10{ENTER}")
Sleep(500)
Send("+{UP 2}")
Sleep(500)

Send("!f")
Sleep(1000)
Send("{DOWN 6}{ENTER}")


WinWaitActive("[CLASS:#32770]")
Sleep(500)
Send("{TAB}{ENTER}")


WinWaitClose("[CLASS:Notepad]")

MouseClick:

; Double click at the current mouse position.
MouseClick("left")
MouseClick("left")

; Double click at the x, y position of 0, 500.
MouseClick("left", 0, 500, 2)

; Double click at the x, y position of 0, 500. This is a better approach as it takes into account left/right handed users.
MouseClick("primary", 0, 500, 2)

Send:

Example()

Func Example()
    ; Simulate the key combination Win + R to open the Run dialogue window.
    Send("#r")

    ; Wait 10 seconds for the Run dialogue window to appear.
    WinWait("Run", "", 10)

    ; Simulate entering notepad.exe and pressing the 'ENTER' key.
    Send("notepad.exe{Enter}")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

    ; Simulate entering the following string and pressing the 'F5' key to input the date and time into edit control of Notepad.
    Send("Today's time/date is {F5}")

    ; Close the Notepad window using the handle returned by WinWait.
    WinClose($hWnd)

    ; Now a screen will pop up and ask to save the changes, the classname of the window is called
    ; "#32770" and simulating the "TAB" key to move to the second button in which the "ENTER" is simulated to not "save the file"
    WinWaitActive("[CLASS:#32770]")
    Sleep(500)
    Send("{TAB}{ENTER}")
EndFunc   ;==>Example

ClipPut:

ClipPut("我被拷贝到剪贴板")
;打开记事本,右键粘贴,看看有什么!

HotKeySet:

; 按Esc键终止脚本, 按Pause/Break键暂停

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ;Shift-Alt-d

;;;; 下面是程序正文 ;;;;
While 1
    Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = Not $Paused
    While $Paused
        Sleep(100)
        ToolTip('脚本已经"暂停"了',0,0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause

Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

Func ShowMessage()
    MsgBox(4096,"标题","这是一个消息.")
EndFunc   ;==>ShowMessage

AutoItSetOption:

; 可直接复制所需语句到脚本中      ;最先列出的是各项属性的默认值 

AutoItSetOption("CaretCoordMode", 1)        ;1=绝对位置, 0=相对位置, 2=客户区
AutoItSetOption("ExpandEnvStrings", 0)      ;0=不展开, 1=展开
AutoItSetOption("ExpandVarStrings", 0)      ;0=不展开, 1=展开
AutoItSetOption("GUICloseOnESC", 1)         ;1=按下 ESC 将关闭窗口, 0=按下 ESC 将不关闭窗口
AutoItSetOption("GUICoordMode", 1)          ;1=绝对位置, 0=相对位置, 2=单元格
AutoItSetOption("GUIDataSeparatorChar","|") ;"|"为默认
AutoItSetOption("GUIOnEventMode", 0)        ;0=取消, 1=启用 OnEvent 模式
AutoItSetOption("GUIResizeMode", 0)         ;0=不调整, <1024 特定的调整值
AutoItSetOption("GUIEventAutoItSetOptionions",0)        ;0=默认, 1=立即通知, 2=GuiCtrlRead 标签索引
AutoItSetOption("MouseClickDelay", 10)      ;10 毫秒
AutoItSetOption("MouseClickDownDelay", 10)  ;10 毫秒
AutoItSetOption("MouseClickDragDelay", 250) ;250 毫秒
AutoItSetOption("MouseCoordMode", 1)        ;1=绝对位置, 0=相对位置, 2=客户区
AutoItSetOption("MustDeclareVars", 0)       ;0=无需声明变量, 1=需要预先声明
AutoItSetOption("PixelCoordMode", 1)        ;1=绝对位置, 0=相对位置, 2=客户区
AutoItSetOption("SendAttachMode", 0)        ;0=不捆绑, 1=捆绑
AutoItSetOption("SendCapslockMode", 1)      ;1=保存并恢复, 0=不保存
AutoItSetOption("SendKeyDelay", 5)          ;5 毫秒
AutoItSetOption("SendKeyDownDelay", 1)      ;1 毫秒
AutoItSetOption("TCPTimeout",100)           ;100 毫秒
AutoItSetOption("TrayAutoPause",1)          ;0=不暂停, 1=暂停
AutoItSetOption("TrayIconDebug", 0)         ;0=无信息, 1=调试信息
AutoItSetOption("TrayIconHide", 0)          ;0=显示, 1=隐藏托盘图标
AutoItSetOption("TrayMenuMode",0)           ;0=扩展, 1=没有默认菜单, 2=没有自动 check, 4=菜单项目ID 不返回
AutoItSetOption("TrayOnEventMode",0)        ;0=关闭, 1=启用
AutoItSetOption("WinDetectHiddenText", 0)   ;0=不检测, 1=检测
AutoItSetOption("WinSearchChildren", 1)     ;0=不搜索, 1=搜索子窗口
AutoItSetOption("WinTextMatchMode", 1)      ;1=完全匹配, 2=快速匹配
AutoItSetOption("WinTitleMatchMode", 1)     ;1=开头, 2=子串, 3=完全, 4=高级 , -1 to -4=Nocase
AutoItSetOption("WinWaitDelay", 250)        ;250 毫秒 

WinWaitActive:

Run("notepad.exe")
WinWaitActive("[CLASS:Notepad]")
Send("!{tab}")
Sleep(1000)

WinActivate("[CLASS:Notepad]", "")

WinSetState:

Example()

Func Example()
    ; Run Notepad
    Run("notepad.exe")

    ; Wait 10 seconds for the Notepad window to appear.
    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

    ; Set the state of the Notepad window to "hide".
    WinSetState($hWnd, "", @SW_HIDE)

    ; Wait for 2 seconds.
    Sleep(2000)

    ; Set the state of the Notepad window to "show".
    WinSetState($hWnd, "", @SW_SHOW)

    ; Wait for 2 seconds.
    Sleep(2000)

    ; Close the Notepad window using the handle returned by WinWait.
    WinClose($hWnd)
EndFunc   ;==>Example

 

2demos:

获取计算机已安装程序列表:

#include 
$file = FileOpen(@ScriptDir&"\RegInstalledItems.csv",1)
if $file = -1 Then
    ConsoleWrite("ERROR: Unable to write to the output file")
    Exit
EndIf
 
$i = 1
While not @error
    $var = RegEnumKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", $i)
    If @error <> 0 then ExitLoop
    ;MsgBox(4096, "SubKey #" & $i & " under HKLM\Software: ", $var)
    $var2 = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"&$var,"DisplayName")
    ;MsgBox(0,"Val",$var2)
    $outLine = $var&","&$var2
    FileWriteLine($file,$outLine)
    $i += 1
WEnd
FileClose($file)

获取计算机信息:

#include 
#include 

_Main()

Func _Main()
	Local $sVOL, $sSERIAL, $sTOTAL, $sFREE
	Local $idInput_ComputerName, $idInput_CurrentUserName, $idInput_OperatingSystem
	Local $idInput_ServicePack, $idInput_VolumeLabel, $idInput_SerialNumber
	Local $idInput_TotalSpace, $idInput_FreeSpace, $idInput_IpAddress, $idInput_StartupDirectory
	Local $idInput_WindowsDirectory, $idInput_SystemFolderDirectory, $idInput_DesktopDirectory
	Local $idInput_MyDocumentsDirectory, $idInput_ProgramFilesDirectory, $idInput_StartMenuDirectory
	Local $idInput_TemporaryFileDirectory, $idInput_DesktopWidth, $idInput_DesktopHeight
	Local $idInput_Date, $idInput_Time, $iMsg

	#forceref $idInput_ComputerName, $idInput_CurrentUserName, $idInput_OperatingSystem
	#forceref $idInput_ServicePack, $idInput_VolumeLabel, $idInput_SerialNumber
	#forceref $idInput_TotalSpace, $idInput_FreeSpace, $idInput_IpAddress, $idInput_StartupDirectory
	#forceref $idInput_WindowsDirectory, $idInput_SystemFolderDirectory, $idInput_DesktopDirectory
	#forceref $idInput_MyDocumentsDirectory, $idInput_ProgramFilesDirectory, $idInput_StartMenuDirectory
	#forceref $idInput_TemporaryFileDirectory, $idInput_DesktopWidth, $idInput_DesktopHeight
	#forceref $idInput_Date, $idInput_Time

	GUICreate("Computer Information - By : Para", 469, 639, (@DesktopWidth - 469) / 2, (@DesktopHeight - 639) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)

	$sVOL = DriveGetLabel("C:\")
	$sSERIAL = DriveGetSerial("C:\")
	$sTOTAL = DriveSpaceTotal("C:\")
	$sFREE = DriveSpaceFree("C:\")

	GUICtrlCreateLabel("Computer Name", 10, 10, 150, 20)
	GUICtrlCreateLabel("Current User Name", 10, 40, 150, 20)
	GUICtrlCreateLabel("Operating System", 10, 70, 150, 20)
	GUICtrlCreateLabel("Service Pack", 10, 100, 150, 20)
	GUICtrlCreateLabel("C: Volume Label", 10, 130, 150, 20)
	GUICtrlCreateLabel("C: Serial Number", 10, 160, 150, 20)
	GUICtrlCreateLabel("C: Total Space", 10, 190, 150, 20)
	GUICtrlCreateLabel("C: Free Space", 10, 220, 150, 20)
	GUICtrlCreateLabel("Ip Address", 10, 250, 150, 20)
	GUICtrlCreateLabel("Startup Directory", 10, 280, 150, 20)
	GUICtrlCreateLabel("Windows Directory", 10, 310, 150, 20)
	GUICtrlCreateLabel("System Folder Directory", 10, 340, 150, 20)
	GUICtrlCreateLabel("Desktop Directory", 10, 370, 150, 20)
	GUICtrlCreateLabel("My Documents Directory", 10, 400, 150, 20)
	GUICtrlCreateLabel("Program File Directory", 10, 430, 150, 20)
	GUICtrlCreateLabel("Start Menu Directory", 10, 460, 150, 20)
	GUICtrlCreateLabel("Desktop Width (Pixels)", 10, 520, 150, 20)
	GUICtrlCreateLabel("Temporary File Directory", 10, 490, 150, 20)
	GUICtrlCreateLabel("Desktop Height (Pixels)", 10, 550, 150, 20)
	GUICtrlCreateLabel("Date", 10, 580, 150, 20)
	GUICtrlCreateLabel("Time", 10, 610, 150, 20)
	$idInput_ComputerName = GUICtrlCreateInput("" & @ComputerName, 180, 10, 280, 20)
	$idInput_CurrentUserName = GUICtrlCreateInput("" & @UserName, 180, 40, 280, 20)
	$idInput_OperatingSystem = GUICtrlCreateInput("" & @OSType, 180, 70, 280, 20)
	$idInput_ServicePack = GUICtrlCreateInput("" & @OSServicePack, 180, 100, 280, 20)
	$idInput_VolumeLabel = GUICtrlCreateInput("" & $sVOL, 180, 130, 280, 20)
	$idInput_SerialNumber = GUICtrlCreateInput("" & $sSERIAL, 180, 160, 280, 20)
	$idInput_TotalSpace = GUICtrlCreateInput("" & $sTOTAL, 180, 190, 280, 20)
	$idInput_FreeSpace = GUICtrlCreateInput("" & $sFREE, 180, 220, 280, 20)
	$idInput_IpAddress = GUICtrlCreateInput("" & @IPAddress1, 180, 250, 280, 20)
	$idInput_StartupDirectory = GUICtrlCreateInput("" & @StartupDir, 180, 280, 280, 20)
	$idInput_WindowsDirectory = GUICtrlCreateInput("" & @WindowsDir, 180, 310, 280, 20)
	$idInput_SystemFolderDirectory = GUICtrlCreateInput("" & @SystemDir, 180, 340, 280, 20)
	$idInput_DesktopDirectory = GUICtrlCreateInput("" & @DesktopDir, 180, 370, 280, 20)
	$idInput_MyDocumentsDirectory = GUICtrlCreateInput("" & @MyDocumentsDir, 180, 400, 280, 20)
	$idInput_ProgramFilesDirectory = GUICtrlCreateInput("" & @ProgramFilesDir, 180, 430, 280, 20)
	$idInput_StartMenuDirectory = GUICtrlCreateInput("" & @StartMenuDir, 180, 460, 280, 20)
	$idInput_TemporaryFileDirectory = GUICtrlCreateInput("" & @TempDir, 180, 490, 280, 20)
	$idInput_DesktopWidth = GUICtrlCreateInput("" & @DesktopWidth, 180, 520, 280, 20)
	$idInput_DesktopHeight = GUICtrlCreateInput("" & @DesktopHeight, 180, 550, 280, 20)
	$idInput_Date = GUICtrlCreateInput("(MONTH)(DAY)(YEAR) " & @MON & "-" & @MDAY & "-" & @YEAR, 180, 580, 280, 20)
	$idInput_Time = GUICtrlCreateInput("(HOUR)(MIN)(SEC) " & @HOUR & ":" & @MIN & ":" & @SEC, 180, 610, 280, 20)

	GUISetState()
	While 1
		$iMsg = GUIGetMsg()
		Select
			Case $iMsg = $GUI_EVENT_CLOSE
				ExitLoop
			Case Else
				;;;
		EndSelect
	WEnd
	Exit
EndFunc   ;==>_Main

 

你可能感兴趣的:(服务架构)