1 May 2013

AutoIt - The Scripting Language for Windows

Introduction
AutoIt is a free and excellent scripting language. It's almost a full programming language because you can do so much with it. Primarily it was designed for automating installations but it's grown and grown over the years. The best thing about it is that you write the code in a text editor (Notepad or anything you want) then compile it. Once compiled your resulting program is a standalone exe file! Yes, no DLL files to package with it!


Download and Install
http://www.autoitscript.com/

Along with AutoIt itself (it includes the compiler, samples and help files) I highly recommend you download and install the editor for it, SciTE.


SciTE
This code editor makes writing AutoIt scripts a breeze. It colour codes the commands, click a word and press F1 to see help on the command/function. It even has links on the menus so you can easily create form windows and it pastes the automatically generated code into the text editor for you.


The above is SciTE with an AutoIt script in it. You can see the colour coding, for example, the comments are in green, variables are red, etc.


An Example AutoIt Script
Here's an example of what's possible with AutoIt. It's a little script I wrote to convert text from hex to text and back again. It outputs the results to a text file and opens it in Notepad.

; Hex/Text Converter
; By Michael Gerrard
; 01/11/2011
; AutoIt 3

$title  = "Hex/Text Converter"
$input = ""
$output = ""
$outputFile = @ScriptDir & "\HexText-output.txt"

$input = InputBox($title, "Enter a hex or text:")
If @Error = 1 Then Exit

$iCheck = StringInStr($input, ":", 0, 1, 1)
If $iCheck = 3 Then ;if successful it returns 1, a colon was found and therefore it's a hex string
 _CreateText()
Else
 _CreateHex()
EndIf

; Output the result to a file
$file = FileOpen($outputFile, 2)
FileWriteLine($file, $input)
FileWriteLine($file, $output)
FileClose($file)
Run('notepad "' & $outputFile & '"', @ScriptDir)
Exit




; =========
; Functions
; =========

; Create a Hex string from Text
Func _CreateHex()

  $i = StringSplit($input, "")

  ; Make the hex numbers from each character of the text string
 For $n = 1 to $i[0]

   $hex = StringRight(Hex(Asc($i[$n])), 2)

   ; Add each char together with colons
  If $n = $i[0] Then
   $output = $output & $hex
  Else
   $output = $output & $hex & ":"
  EndIf
 Next

EndFunc


; Create a Text string from Hex
Func _CreateText()

  $i = StringSplit($input, ":")

  ; Take the hex numbers and convert them to text
 For $n = 1 to $i[0]

   $iText = Chr(Dec($i[$n]))

   ; Add each char together with colons
  $output = $output & $iText
 Next

EndFunc



What else can you do?
Read and write to the registry, to ini files easily with dedicated commands, control windows, send keystrokes to programs, interact with window controls, run command line DOS programs, pass variables to DOS programs, etc. AutoIt is really for making scripts to automate a process but you can also write GUI (Graphical User Interface) forms and other windows.


Conclusion
I've used AutoIt countless times for many years. It is powerful yet easy to learn. If you want to automate Windows this is one of the best scripting languages out there.

Recommended: 9.9 / 10



No comments: