How to Read a String From a Window in AutoIt
AutoIt, a Windows automation program, frees users from repetitive tasks such as clicking buttons to check email and pressing keys to launch applications. Another useful AutoIt feature gives you the ability to retrieve the text string from the title bar of any window. Use AutoIt, for example, to read the full title from an Internet Explorer window, and your program can determine the current URL string stored in the browser’s title bar. AutoIt handles the complicated programming behind the scenes. All you have to do to retrieve a string is add a few lines of code to an AutoIt file.
Instructions
-
-
1
Download AutoIt from the application's home page and install the program on your computer if it isn't already installed.
-
2
Launch Notepad and create a new document.
-
-
3
Paste the following text into the document:
Opt("WinTitleMatchMode", 2)
$targetWindow = "Notepad"
$string1 = WinGetTitle($targetWindow)
MsgBox(0, "String from title = ", $string1)The first line, a comment, explains what the script does. Line 2 defines a variable named $targetWindow; its value is “Notepad.” AutoIt will retrieve the title string from Notepad in this example. The third line executes the WinGetTitle command. This command retrieves the text string and stores it in a variable named $string1. The final line of code displays the retrieved text string.
-
4
Press the "Ctrl" and "S" keys on your keyboard simultaneously to display Notepad's "Save As" window. Type a name for the AutoIt file, such as "Autoit_Test1.au3", in the File Name input box and click "Save." Remember to add the ".au3" extension to end of the file name as shown in this example.
-
5
Launch Windows Explorer and locate the file you saved. Double-click on the file. The AutoIt script runs, retrieves the title string from Notepad and displays the string retrieved from Notepad in a pop-up message box.
-
1
Tips & Warnings
Experiment with string retrieval from other windows by changing the value of $targetWindow to the title found in another window, such as “Firefox." Save the file, rerun it and AutoIt will store the retrieved string from the window in the $targetWindow variable. Use this variable any way you like after obtaining its value.
In this example, the names of the variables that hold the target window and retrieved string value are $targetWindow and $string1. Use any name for these variables, but begin all variable names with the "$" character.