[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

VBScript (Windows Scripting Host)-part4-AutotIt



PureBytes Links

Trading Reference Links

See my homepage for more info
http://home.wxs.nl/~anthmaas

Regards,
Ton Maas
ms-irb@xxxxxxxxxxxxx
Dismiss the ".nospam" bit (including the dot) when replying.

=====================================================

>From the AutoIt-site, Help and History files :
---------------------------------------------------------------------
AutoIt
 
What is it?

AutoIt is a tool to automate simple tasks and software installations by
using a combination of simulated keystrokes, mouse clicks and window
commands.

Introduction

AutoIt is a tool designed to assist in automatically installing software that
cannot be automatically installed by other means. This is most useful during
a PC rollout where the client machines are being automatically installed.
Many programs such as MS Office, Mcafee, IE4, etc. feature their own automated
installation routines. Far too many programs, however, feature no way of
automatic installation. That's where AutoIt comes in.
AutoIt can also be used to automate small tasks on your machine. Best of all,
its a small (44Kb) self contained .exe (no DLL's required!) and FREE !

How to use

When AutoIt is executed, it reads a specified script file. This file contains window titles
(and optional text on those windows). When a specified window exists, becomes active, or
ceases to exist AutoIt can perform a number of commands, these include:

Sending keystrokes and mouseclicks (all characters, keyboard layout independant). 
Window functions e.g. minimizing, hiding, restoring, waiting for, activating
(even under Win98/NT2000).
Most keystrokes can be sent (i.e. not just alphanumerics, but characters like
!"$%^&*()_-+=, using any language keyboard.  Also, left and right mouse clicks
can be sent.

AutoIt was my attempt to write a replacement for the Microsoft utility "ScriptIt".
Here are the associated advantages and disadvanatages over ScriptIt.

Advantages
----------
  
  - It's much smaller at ~44Kb (600Kb smaller than ScriptIt).
  - It will be (and has been) updated if problems are found (or suggestions made).
  - The "Reveal" function is much better (can copy and paste).
  - Non-active as well as active windows can be waited for.
  - Variable delays are possible (defined in milliseconds).
  - The time in between keystrokes can be altered (in milliseconds).
  - When running an application, a working directory can be specified.
  - Can correctly activate windows under Win98/NT 5.0 (Use "WinActivate").
  - Don't have to wait for a window in order to send keystrokes 
    (e.g. send keys to the desktop).
  - Can simulate left and right mouse button clicks.
  - Simple loops (IfWinExist, IfWinNotExist, Goto, etc.)
  - Simple dialog boxes and user input boxes.
  - Check for the existance of files and directories.
  
Disadvantages
-------------
  - None of the Wilson WinBatch commands are featured.
    (But who used them anyway? :) )

AutoIt Download

AutoIt v1.8 - 2nd April, 1999
=============================
If you are going to be using AutoIt for automating tasks on your own machine,
go for the "proper" installation (AutoIt.exe). 

AutoIt.exe (117Kb) - Current Version: v1.8 (2nd April, 1999) - Win9x / Windows NT.
                               
"AutoIt.exe" is the "Proper" installation version, with ".aut" filetype registration and un-install.
Documentation comes in English, French, German and Italian.

Email me, and I'll notify you of a new release.

If anyone is willing to do some non-English language versions of the 
examples and documentation, then please contact me at the below address.
Example Scripts

This page has some example scripts sent to me by users of AutoIt - feel free to send more! ;)

HTML Documentation

Tim Houser kindly produced a HTML version of the documentation for easier reference.

(C)1999 Jonathan Bennett
Email: jbennett@xxxxxxxxxxxxxxxxxx

--------------------------------------------------------------------------------


Bugs and suggestions to jbennett@xxxxxxxxxxxxxxxxxx


--------------------------------------------------------------------------------


***************************************************************************

Examples
========

See the "Examples" directory that accompanies this archive for some examples.
Also check the AutoIt home pages for other examples.

Please send me your example scripts and I'll publish them on my web site.  
This would make a useful resource for other people. (Try to include lots of
comments to aid others).


Thanks to
=========
 - Jean-Paul Mesnage for the French translations and ideas.
 - Davide Fiorentino for the Italian translations.
 - Holger Schmitz for the German translations.
 - Steve Hurcombe for the "/reveal2" suggestion and filetype info.
 - Per Jacobson for the {PRINTSCREEN} suggestion (amongst others :) )
 - Graham Clinch for the first pass at a decent installation routine.
 - Everyone else who who has since emailed me with suggestions and comments
   (too many to list!) - thank you all very much!

***************************************************************************

Conventions
===========

Parameters are shown in angled brackets, optional parameters are shown in
square brackets.

e.g.
  <cmd>,<parameter1>[,<parameter2>]

This is a command, followed by a comma and parameter1, optionally followed
by a comma and parameter2. 

i.e. 
  <cmd>,<parameter1>

  OR

  <cmd>,<parameter1>,<parameter2>


***************************************************************************


Command Line Syntax
===================

AutoIt is executed from the command line in the two following ways:

  AutoIt /reveal
  AutoIt /reveal2

  OR

  AutoIt <filename of script file>


Reveal Mode
-----------

When launched with the "/reveal" command switch, the program pauses for five
seconds and then displays information on the currently active window
(five seconds should be enough time for you to switch to the window that you
are interested in).  This allows you to see what information AutoIt can "see"
on a window.  

The switch "/reveal2" is similar, but causes the text to be continuously
updated.

N.B. As with ScriptIt, not all text on a window can be "Seen" - experiment!

Highlight text on the reveal screen with the mouse and press "CTRL-C" to copy
it to the clipboard.  You can then paste this into a text editor.


Script Mode
-----------

AutoIt will execute the script filename given on the command line.  The file
can have any extension (if you use the installation routine, .aut will be
automatically registered.  Right clicking on a .aut file will enable you to
run or edit the script).


***************************************************************************


Script File Syntax
==================

; Start of script
; Comments are indicated by a semi-colon

<Cmd>,<Cmd Parameters>
<Cmd2>,<Cmd2 Parameters>
<Cmd3>,<Cmd3 Parameters>

label:

; Labels in the file must end with a colon (:) They are used for
; Commands like IfWinExist and IfWinActive (see section below)
; End of script


There may also be an optional [ADLIB] section at the end of the script.
(see below)

Each line in the script can contain leading and trailing white spaces (space
or tabs).  

i.e. 
  "   Wait ,   Parameter1  ,  Parameter2     " 


When using the key "Send" command, you may wish to send spaces, in this case
use "{SPACE}" to denote a space.

N.B. If a line contains a command that is not recognised, it will be treated as
a comment!


***************************************************************************


Window Titles and Text
======================

A number of script commands have "Window Title" and "Window Text" as parameters.  
"Window Title" is text that appears in the windows title bar.  
"Window Text" is any other text on that window, e.g. text on buttons such as 
"OK" and "Cancel".

Having "Window Text" allows you to distinguish between multiple windows that 
have the same title.

Neither the title nor text needs to be completely specified.  
I.e.  "Untitled - No" will match a window with the title "Untitled - Notepad".  
See the text on the "SetTitleMatchMode" command for additions to this.

Use the "/reveal" mode to see what text is present on a window.

N.B.  Windows titles and text are CASE SENSITIVE!!!!!


***************************************************************************


DOS Environment Variables
=========================

You can use DOS environment variables anywhere in an AutoIt script.  As in 
DOS batch files, just include the variable enclosed in percent (%) symbols.

For example, if you want to send the current DOS path to the notepad
window you could do:

  Run, notepad.exe
  WinWaitActive, Untitled - Notepad
  Send, This is the DOS path %PATH%

This example shows an extreme example of using a DOS batch file to create
an entire line in a script.

  REM DOS BATCH FILE 
  REM
  SET LINE1=Send, hello
  AUTOIT.EXE MYFILE.INI
  END END OF BATCH FILE


  ; MYFILE.INI - AutoIt Script
  Run, notepad.exe
  WinWaitActive, Untitled - Notepad
  %LINE1%
  ; End of script

This will use the DOS variable LINE1 to completely replace a line in
the AutoIt script!

N.B. Because of its special status, if you want to actually use a 
percent sign without it being changed into a variable, use two.
e.g.
  Send, This will send a single percent sign %%

You can also use "SetEnv" to set variables, but these changes will not be 
visible from DOS - the value will only be valid in the current script.


***************************************************************************


Script Commands
===============

The following commands are currently recognised (commands are not case sensitive):

Run,               <Program path and name>[*<Working directory>]
RunWait,           <Program path and name>[*<Working directory>]
Send,              <Series of key presses>
WinWait,           <Window Title>[,<Window Text>]
WinWaitClose,      <Window Title>[,<Window Text>]
WinWaitActive,     <Window Title>[,<Window Text>]
WinHide,           <Window Title>[,<Window Text>]
WinRestore,        <Window Title>[,<Window Text>]
WinMinimize,       <Window Title>[,<Window Text>]
WinMaximize,       <Window Title>[,<Window Text>]
WinActivate,       <Window Title>[,<Window Text>]
WinClose,          <Window Title>[,<Window Text>]
IfWinExist,        <Window Title>,<Window Text>,<Label>
IfWinNotExist,     <Window Title>,<Window Text>,<Label>
IfWinActive,       <Window Title>,<Window Text>,<Label>
IfWinNotActive,    <Window Title>,<Window Text>,<Label>
Sleep,             <milliseconds>
SetKeyDelay,       <milliseconds>
LeftClick,         <x>,<y>
RightClick,        <x>,<y>
Exit
Goto,              <label>
SetTitleMatchMode, <mode>
MsgBox,            <display mode>,<message>
IfMsgBox,          <return value>,<label>
SetEnv,            <variable>,<value>
IfExist,           <file or directory>,<label>
IfNotExist,        <file or directory>,<label>
InputBox,          <variable>,<message>
Gosub,             <label>
Return


The syntax of these script commands are given below:


Run,<Program path and name>[*<Working directory>]
-------------------------------------------------

  Executes a given program and proceeds to the next line of the script.  
  N.B. The program to run and the working directory are separated by a '*'.
  The working directory is optional.

  e.g.  Run,notepad.exe*C:\WINDOWS
        Run,C:\Program Files\Microsoft Office\Office\WinWord.exe

  The first command runs notepad and sets the working directory to "C:\WINDOWS".
  The second command runs MS Word in the current directory.

  You can also run command interpreter commands line Echo, Copy, etc.
  
  e.g.  Run,COMMAND.COM /C Echo Hello > C:\Hello.txt
  OR
        Run,%COMSPEC% /C Echo Hello > C:\Hello.txt

  Creates a file called C:\Hello.txt containing the word "Hello".


RunWait,<Program path and name>[*<Working directory>]
-----------------------------------------------------

  Executes a given program and waits for it to close before proceeded to the next
  line of the script.

  e.g.  RunWait,Notepad.exe


Send,<Series of key presses>
----------------------------

  Sends a set of keystrokes to the currently active window.
  (The syntax of these keystrokes can be found in the following section).

  e.g  Send,This is a line of text#{ENTER}


WinWait,<Window Title>[,<Window Text>]
--------------------------------------

  Stops script execution until the specified window exists.  The window does not
  need be active.

  e.g.  WinWait,Untitled - Notepad


WinWaitClose,<Window Title>[,<Window Text>]
-------------------------------------------

  Stops script execution until the specified window ceases to exist.

  e.g.  WinWaitClose,Untitled - Notepad


WinWaitActive,<Window Title>[,<Window Text>]
--------------------------------------------

  Stops script execution until the specified window to exists and is active.


WinHide,<Window Title>[,<Window Text>]
WinRestore,<Window Title>[,<Window Text>]
WinMinimize,<Window Title>[,<Window Text>]
WinMaximize,<Window Title>[,<Window Text>]
WinActivate,<Window Title>[,<Window Text>]
WinClose,<Window Title>[,<Window Text>]
------------------------------------------

  Stops script execution until the specified window exists and then performs
  the relevant command, i.e. minimising, hiding, restoring, etc.
  >> N.B. WinActivate will bring a window to the top even under Win98/NT5. <<


IfWinExist,<Window Title>,<Window Text>,<Label>
IfWinNotExist,<Window Title>,<Window Text>,<Label>
IfWinActive,<Window Title>,<Window Text>,<Label>
IfWinNotActive,<Window Title>,<Window Text>,<Label>
---------------------------------------------------

  If the condition is met (i.e. the window title and text exists, is active,
  doesn't exist, is inactive) then the script will continue from <label>.
  N.B. If no window text is to be given you MUST include the extra comma.
  e.g.
 IfWinExist, Win Title, Win Text, label1
   OR
 IfWinExist, Win Title,, label1

  Also note that the colon (:) is not specified for the label.

  e.g.

  ; Loop until the notepad window exists
  loop:
  IfWinNotExist, Untitled - Notepad,, loop


Sleep,<milliseconds>
--------------------

  Halts execution of the script for the given number of milliseconds.  Max is 32767.
  1000 milliseconds = 1 second. 


SetKeyDelay,<milliseconds>
--------------------------

  Changes the delay between keystrokes in milliseconds.  Max is 32767.
  Default is 10ms.  1000 milliseconds = 1 second.


LeftClick,<x>,<y>
RightClick,<x>,<y>
------------------

  Simulates a left or right mouse button click.  The X and Y co-ordinates are relative
  to the currently active window.  Run AutoIt in reveal mode to determine the required
  co-ordinates of a window.
  To perform a double-click, simply run the command twice :)


Exit
----

  This command will end the script.


Goto,<label>
-------------

  Script execution will continue at the specified label.
  e.g.
    ; This script will loop forever
 myloop:
 Sleep, 10
 Goto, myloop


SetTitleMatchMode,<mode>
--------------------------

  Changes the way that window titles are matched against the script.
  Valid modes are "1" and "2" - the default is "1".  This mode affects most
  of the commands within AutoIt that have any sort of window title and text in
  the command, i.e. WinWait, WinWaitActive, IfWinActive, etc.

  e.g.
    ; Script using mode 1 (default)
    SetTitleMatchMode, 1

    ; Script using mode 2
    SetTitleMatchMode, 2

  mode 1
  ------
  In the script you specify the start of a window title to match.  i.e. for
  the notepad.exe window (Untitled - Notepad), valid matches would be:
  "Untitled", "Untitled -", "Unt" and "Untitled - Notepad".

  mode 2
  ------
  In the script you can specify ANY substring of the window title you want to match.
  Again for the notepad.exe window valid matches would be:
  "Untitled", "Untitled - Notepad", "Notepad", "No".


MsgBox,<display mode>,<message>
-------------------------------

  Displays a dialog box with the specified message.  Different display
  modes will give different results (appearance, number of buttons).  A
  list of modes is given below, add up the numbers of the display mode
  you want.

  e.g. To display "Hello" with just an "OK" button:
    MsgBox, 0, Hello
  
  e.g. To display "Hello" with an exclamation box and OK and Cancel:
    MsgBox, 49, Hello
      (1=OKCANCEL, 48=Exclamation, = 49) 

  MB_OK                       0
  MB_OKCANCEL                 1
  MB_ABORTRETRYIGNORE         2
  MB_YESNOCANCEL              3
  MB_YESNO                    4
  MB_RETRYCANCEL              5

  MB_ICONHAND                 16
  MB_ICONQUESTION             32
  MB_ICONEXCLAMATION          48
  MB_ICONASTERISK             64
 
  MB_APPLMODAL                0
  MB_SYSTEMMODAL              4096
  MB_TASKMODAL                8192
  MB_HELP                     16384


IfMsgBox,<return value>,<label>
-------------------------------

  Use this to branch to a label based on the button the user pressed in the
  MsgBox command.  Valid return values from MsgBox are:
    ABORT, CANCEL, IGNORE, NO, OK, RETRY, YES 

  e.g. To put up a dialog box, with OK and CANCEL buttons, then branch if
  the user selects CANCEL:

    ; Example Script
 MsgBox, 1, This is a test message
 IfMsgBox, CANCEL, cancellabel
 Exit
 cancellabel:
 ; If the script gets to here, user pressed cancel


SetEnv,<variable>,<value>
-------------------------

  This command will set an environment variable to the specified value.
  N.B.  This variable only exists within AutoIt you will not be able
  to access it from DOS.  (Unbelieveably, applications cannot change
  the DOS variables!)

  e.g. To set the Env variable "ERROR" to "There has been an error":
    SetEnv, ERROR, There has been an error.

  To use this variable, use the percent symbol:

  e.g. After running the previous command, this will output "There has
  been an error":
    MsgBox, 1, %ERROR%


IfExist,<file or directory>,<label>
IfNotExist,<file or directory>,<label>
----------------------------------------

  Will cause the script to branch to <label> if the specified file
  or directory exists.

  e.g.
    IfExist, C:\COMMAND.COM, fileexist
 Exit
 fileexist:
 ; Script will get to here if C:\COMMAND.COM DOES exist.


InputBox,<variable>,<message>
-----------------------------

  Will cause a dialog box with <message> to appear.  The user can enter text, press OK,
  and the text will be stored in the DOS variable <variable>.

  e.g.
    InputBox, MyMessage, Please - Enter some text to display!
 MsgBox, 0, %MyMessage%


IfEqual,<variable>,<label>,<text>
IfNotEqual,<variable>,<label>,<text>
------------------------------------

  Will compare the DOS variable <variable> with <text> and branch depending
  on the result.

  e.g.
    InputBox, MyVar, Please enter the word: WOMBLE
 IfEqual, MyVar, match, WOMBLE
 MsgBox, 0, You didnt type the correct word!
 Exit
 match:
 MsgBox, 0, Well done!  You typed in the correct word!


Gosub,<label>
Return
-------------
  
  Just like the old BASIC commands.  Gosub will branch to a specified label.
  Return will return to the line after the Gosub command.

  e.g.
    SetEnv, MyMessage, This is the first message!
    Gosub, mysubroutine
    SetEnv, MyMessage, This is the second message!
    Gosub, mysubroutine
    SetEnv, MyMessage, This is the third message!
    Gosub, mysubroutine
    Exit

    mysubroutine:
    MsgBox, 0, %MyMessage%
    Return


***************************************************************************


"Send" Command Syntax
=====================

The "Send" command syntax is similar to that of ScriptIt and the Visual Basic
"SendKeys" command.  Characters are sent as written with the exception of the
following characters:

'!'
  This tells AutoIt to send an ALT keystroke, therefore "Send,This is text!a"
  would send the keys "This is text" and then press "ALT+a".  

  N.B. Some programs are very choosy about capital letters and ALT keys, 
  i.e. "!A" is different to "!a".  The first says ALT+SHIFT+A, the second is ALT+a.
  If in doubt, use lowercase!

'+'
  This tells AutoIt to send a SHIFT keystroke, therefore "Send,Hell+o" would send
  the text "HellO".  "Send,!+a" would send "ALT+SHIFT+a".

'^'
  This tells AutoIt to send a CONTROL keystroke, therefore "Send,^!a" would send 
  "CTRL+ALT+a".

  N.B. Some programs are very choosy about capital letters and CTRL keys, 
  i.e. "^A" is different to "^a".  The first says CTRL+SHIFT+A, the second is CTRL+a.
  If in doubt, use lowercase!

'#'
  The hash is used as a key delimiter to make a line easier to read.  
  i.e. "Send, H#e#l#l#o" is the same as "Send,Hello".


Certain special keys can be sent and should be enclosed in braces:

Send Command Resulting Keystroke
------------    -------------------
{#}             #
{!}             !
{^}             ^
{+}             +
{{}             {
{}}             }
{%}             %
{SPACE}         Space
{ENTER}         The return/enter key is sent
{ALT}           Alt key
{BACKSPACE}     Backspace
{BS}            Backspace
{DEL}           Delete
{DELETE}        Delete
{DOWN}          Cursor down
{END}           End
{ESC}           Escape
{ESCAPE}        Escape
{F1}-{F12}      Function keys
{HOME}          Home
{INS}           Insert
{INSERT}        Insert
{LEFT}          Cursor left
{PGDN}          Page down
{PGUP}          Page up
{RIGHT}         Cursor right
{TAB}           Tab
{UP}            Cursor up
{PRINTSCREEN}   PrintScrn
{LWIN}   Left "Windows"
{RWIN}   Right "Windows"


Single keys can also be repeated, e.g.
  {DEL 4}    Presses the DEL key 4 times
  {S 30}     Sends 30 'S' characters
  +{TAB 4}   Presses SHIFT+TAB 4 times


***************************************************************************


[ADLIB] Section
===============

The [ADLIB] section is optional, and if present should be placed at the
end of the script file.  The ADLIB section is designed to cope with unexpected
conditions.  The ADLIB section consists of single line commands that check if a
window is active and if so, send keys to it.
The ADLIB section must start with the line "[ADLIB]".

The format of the a line in the ADLIB section is:

 <Window Title>,<Window Text>,<Keys to Send>

e.g.
 Run,Notepad.exe
 WinWaitActive,Untitled - Notepad
 Send,12345678#!{F4}
 WinWaitClose,Untitled - Notepad

 [ADLIB]
 Notepad,save the changes?,!n

Without the ADLIB section, this script would never end as Notepad would never
close until someone responded to the message "Do you want to save the changes?".

ADLIB commands must contain a window title and some text on the window.
If there is no text on the window, or if you don't want to specify any, then you must
use two commas.

e.g.
 Run,Notepad.exe
 WinWaitActive,Untitled - Notepad
 Send,12345678#!{F4}
 WinWaitClose,Untitled - Notepad

 [ADLIB]
 Notepad,,!n


***************************************************************************

HISTORY
02/04/99 - v1.8
===============
- Added MsgBox and IfMsgBox commands. (Note, these have changed from 1.8 beta)
- Added IfExist and IfNotExist commands for files and directories.
- Added DOS environment variable support ( e.g. Send, Path is %PATH% ).
    N.B. Because of this, the percent symbol '%' is now special.  If you
    really want to use this symbol you must do it twice ie. %%
- Added SetEnv command.
- Added InputBox command.
- Added IfEqual and IfNotEqual commands.
- Added Gosub and Return commands.
- "Page-fault" bug fixed (when mouse coords are out of range).

14/03/99 - v1.7
===============
- Added {LWIN} and {RWIN} "Window key" keystrokes.
- Added SetTitleMatchMode command.  (Alters the way window titles are matched).
- A blank window title is now valid.  (e.g. WinWait,,Window text)
- AutoIt executable grows for the first time to 44KB.

28/02/99 - v1.6
===============
- Added IfWinExist, IfWinNotExist, IfWinActive, IfWinNotActive commands.
- Added support for labels (for the above commands).
- Added Goto command.
- Added Exit command.
- Window titles are now case sensitive.  i.e. "NOTEPAD" and "Notepad" are
    different.

20/02/99 - v1.5
===============
- Added {PRINTSCREEN} command to simulate the "PrintScrn" key.
- Copy and paste from the "reveal" mode.  (Use CTRL-C to copy).
- Added command line syntax - "AutoIt /?".
- Added simulated mouse clicks (left and right).
- Added mouse co-ordinates in "reveal" mode.

20/01/99 - v1.4
===============
- Added {BS}, {INSERT}, {ESCAPE} and {DELETE}
- Added "/reveal2" command line switch.  Cause AutoIt to update the
  reveal window every half second (useful for complex dialog windows)

18/01/99 - v1.3
===============
- Added WinClose command.
- The title "WinWait,Notepad" will no longer match the window 
  "Untitled - Notepad", but "WinWait,Untitled" will.
- You can now use white spaces (spaces and tabs) all over the place to
  make the script file look pretty :)

14/01/99 - v1.2
===============
- Changed "Run" command.  Asterisk (*) now states start of working directory.
- Added [ADLIB] section support
- Officially released on winfiles.com

12/01/99 - v1.1
=============== 
- Fixed WinActivate under Windows 98/NT 2000!
- Added WinMaximize
- Added executable version info
- Added icons

12/01/99 - v1.0
=============== 
- Initial build (beta) 
 
***************************************************************************