Jan 9, 2005 windows

Quick cheatsheet for AutoIt. I used it extensively while working on automation script for Windows. It is not pretty but it really worked well. There is also an alternative called Autohotkey, which does not have as extensive scripting capability as Autoit.

Variables

Variables are dynamically typed and is typically local.

Arrays

dim $arr[5]  	;0-4
ubound($arr)  	;returns 5

Special Var/Macros

Operators

Conditional Statements

if-then block

If ... Then
Elsif ... Then
...
Else
EndIf

case-switch

Select
	Case $var>1 
		...
	Case ...
	 	...
	Case Else 
		...
EndSelect

Loop

For <variable> = <start> To <stop> [Step <stepval>]
    statements
    ...
Next

while loop

While <expression>
    statements
    ...
WEnd

Do loop

Do
    statements
    ...
Until <expression>

continue and break

ContinueLoop: similar to c "continue"	
ExitLoop: similar to c "break"

Function

Func myFunc([ByRef] $param1)
Return [value] : optional returns
EndFunc 

Macro

#cs / #comments-start : same as c's /*
#ce / #commends-end: same as c's */
#include "filename": relative to current script path or hard path
#include <filename> : relative to @ScriptDir\Include
#include-once: forces all #include to be included only once

Keyboard Function

HotKeySet ( "key" [, "function"] )
	key: same as SEND. Returns 1 if success. 0 on error.

Send( "keys" [, flag] )  //  Send keystroke

Special characters and keystrokes

!: alt. !a = alt-a
+: shift. +a= shift-a
^: control
#: windows key. #r= windows-r

{SPACE} SPACE 
{ENTER} ENTER key on the main keyboard 
{ALT} ALT 
{BACKSPACE} or {BS} BACKSPACE 
{DELETE} or {DEL} DELETE 
{UP} Cursor up 
{DOWN} Cursor down 
{LEFT} Cursor left 
{RIGHT} Cursor right 
{HOME} HOME 
{END} END 
{ESCAPE} or {ESC} ESCAPE 
{INSERT} or {INS} INS 
{PGUP} PageUp 
{PGDN} PageDown 
{F1} - {F12} Function keys 
{TAB} 
{NUMPAD0} - {NUMPAD9} Numpad digits 
{NUMPADMULT} Numpad Multiply 
{NUMPADADD} Numpad Add 
{NUMPADSUB} Numpad Subtract 
{NUMPADDIV} Numpad Divide 
{NUMPADDOT} Numpad .
{LALT} Left ALT key 
{RALT} Right ALT key 
{LCTRL} Left CTRL key 
{RCTRL} Right CTRL key 
{LSHIFT} Left Shift key 
{RSHIFT} Right Shift key 

Flags: 
flag = 0 (default), Text contains special characters like + and ! to indicate SHIFT and ALT key presses.
flag = 1, keys are sent raw.

Examples:

Send("{+}") Usually checks a checkbox (if it's a "real" checkbox.)  
Send("{-}") Usually unchecks a checkbox.  
Send("{NumPadMult}") 
Send("#r")
WinWaitActive("Run")
Send("notepad.exe{Enter}")
WinWaitActive("Untitled -")
Send("Today's time/date is {F5}")
Send("!{ESC}")	  ; switch application alt-Esc

MessageBox

InputBox()

MsgBox ( flag, "title", "text" [, timeout in seconds] )

Return Value:
    Failure: Returns -1 if the message box timed out.
    Button Pressed Return Value  
    OK 1 
    CANCEL 2 
    ABORT 3 
    RETRY 4 
    IGNORE 5 
    YES 6 
    NO 7 
    TRY AGAIN ** 10 
    CONTINUE ** 11 
flag value Button-related Result 
    0 OK button 
    1 OK and Cancel 
    2 Abort, Retry, and Ignore 
    3 Yes, No, and Cancel 
    4 Yes and No 
    5 Retry and Cancel 
    6 ** Cancel, Try Again, Continue 
    flag value Icon-related Result 
    0 (No icon) 
    16 Stop-sign icon 
    32 Question-mark icon 
    48 Exclamation-point icon 
    64 Icon consisting of an 'i' in a circle 
    flag value Default-related Result 
    0 First button is default button 
    256 Second button is default button 
    512 Third button is default button 
    flag value Modality-related Result 
    0 Application  
    4096 System modal (dialog has an icon) 
    8192 Task modal 

SplashImageOn( "title", "file" [, width [, height [, x pos [, y pos [, opt]]]]] : Show bitmap

SplashOff() : hide bitmap

Example

SplashImageOn("Splash Screen", $destination)
Sleep(3000)
SplashOff()

Misc Functions

Call ("func"): dynamically call a func, cannot have param

SoundPlay ( "filename.wav/.mp3" [, wait] )

SoundSetWaveVolume ( percent ) set volume (0-100)

Process-related

ProcessClose ( "process.exe")
    Ends a running process/app
    ProcessClose ( "notepad.exe" )
ProcessExists ( "notepad.exe" ) 
    Checks to see if a specified process exists. 
    Returns 1 if the proc exists, otherwise returns 0.
ProcessWait ( "notepad.exe" [, timeout] )
    Pauses script execution until a given process exists.
ProcessWaitClose ( "notepad.exe" [, timeout] )
    Pauses script execution until a given process does not exist
Run ( "filename" [, "workingdir" [, flag]] )
    workingdir [optional] The working directory. 
    flag [optional] The "show" flag of the executed program:
          @SW_HIDE = Hidden window
          @SW_MINIMIZE = Minimized window
          @SW_MAXIMIZE = Maximized window 
RunAsSet ( ["user", "domain", "password"] )
    Initialise a set of user credentials to use during Run and RunWait operations. 2000/XP.


    ; Set the RunAs parameters to use local adminstrator account
    RunAsSet("Administrator", @Computername, "adminpassword")

    ; Run registry editor as admin
    RunWait("regedit.exe")

    ; Reset user's permissions
    RunAsSet()

RunWait ( "filename" [, "workingdir" [, flag]] )
    Runs an external program and pauses script execution until the program finishes

Exit app

Exit [exit code=0]
// Exits app, 0 (default)= ok, 1+= error 

Shutdown ( code ): shutdown PC.

0 = Logoff
1 = Shutdown
2 = Reboot
4 = Force
8 = Power down

Sleep ( delay in msec): pause.

Controls (buttons/UI)

ControlClick ( "title", "text", "classnameNN" [, button] [, clicks]] )
	button [optional] The button to click, "left", "right" or "middle". Default is the left button. 
	clicks optional] The number of times to click the mouse. Default is 1.  
ControlFocus ( "title", "text", "classnameNN" )
	Sets input focus to a given control on a window.
ControlSend ( "title", "text", "classnameNN", "string" [, flag] )
	Sends a string of characters to a control. See Send()
ControlSetText ( "title", "text", "classnameNN", "new text" )
ControlGetText ( "title", "text", "classnameNN" )

Windows functions

WinActivate ( "title" [, "text"] )
	Activates (gives focus to) a window.
WinClose ( "title" [, "text"] )
	Closes a window. Use WinKill to force exit.
WinKill ( "title" [, "text"] )
	Forces a window to close. No Asking for Save/etc.
WinExists ( "title" [, "text"] )
	Checks to see if a specified window exists.
	Returns 1 if the window exists, otherwise returns 0.
WinGetTitle ( "title" [, "text"] )
	Retrieves the full title from a window
	* WinGetTitle("") returns the active window's title.
WinGetState ( "title" [, "text"] ) 
	Return:  use BitAND() to examine the part you are interested in
		1 = Window exists 
		2 = Window is visible 
		 4 = Windows is enabled 
		 8 = Window is active 
		 16 = Window is minimized 
		 32 = Windows is maximized 
WinMinimizeAll ( ) 
	Minimize all windows
WinMinimizeAllUndo ( )
	Undo above
WinMove ( "title", "text", new x, new y [, new_width [, new_height]] )
	Moves and/or resizes a window.
WinSetState ( "title", "text", flag )
	flag The "show" flag of the executed program:
	@SW_HIDE = Hide window
	@SW_SHOW = Shows a previously hidden window
	@SW_MINIMIZE = Minimize window
	@SW_MAXIMIZE = Maximize window
	@SW_RESTORE = Undoes a window minimization or maximization 
WinWait ( "title" [, "text" [, timeout]] ) 
	Pauses execution of the script until the requested window exists.
	Returns 0 if timeout occurs.
WinWaitActive ( "title", ["text"], [timeout] )
	Pauses execution of the script until the requested window is active.
WinWaitClose ( "title" [, "text" [, timeout]] )
	Pauses execution of the script until the requested window does not exist.
WinWaitNotActive ( "title" [, "text" [, timeout]] )
	Pauses execution of the script until the requested window is not active.

Menu functions

WinMenuSelectItem ( "title", "text", "item" [, "item" [, "item" [, "item" [, "item" [, "item" [, "item"]]]]]] )