Autohotkey & autoit

Source: Internet
Author: User
As far as I know, many of the users who need to write autohotkey/autoit scripts to achieve automated operations are network management. Others may be personal users who generally have a considerable technical level, we also hope to use scripts to complete repetitive work that previously required manual operations. However, restrictions on language conditions may be hard to understand in official help documents. For the convenience of readers, I will start from the simplest point of view. Each example provides the corresponding Ahk and au3 versions as much as possible at the same time. Code . This article will try to describe in a more popular language, but it is not intended to explain the basic syntax, so it is not necessarily suitable for beginners to read.

The Ahk/au3 versions involved in this article:
Autohotkey
1.0.44. 08
Autoit
3.1.1
I. Script
1. What is a script?
This is a very popular term. In layman's terms, scripts generally refer to text files in specific formats written according to certain syntax rules. You may have heard of many types of scripts: VBScript, JScript, PHP, ASP, JSP, CGI, CS script, and even game plug-in script.
These script files are all executable files and can perform corresponding operations.
Ahk Script File Extension: *. Ahk
Au3 Script File Extension: *. au3


2. scripts andProgram?
Strictly speaking, the so-called "program" refers to a varietyProgramming Language(For example, C/C ++/C #/Delphi) compiled binary files compiled by the compiler are generally machine code that can be executed by the system. Scripts are just plain text files that contain various defined commands, much like batch files. In this way, we can draw a simple conclusion that users generally cannot obtain the "program"Source code, We can only reverse assemble it into assembly language code (or other), of course, some "programs" can obtain the source code (such as Java ); the script is a code file that you can directly view, while Ahk/au3 provides a method to convert the script file into an EXE file.


3. How to run the script?
A script is an interpreted language. Its operation depends on an interpreter, which translates and interprets every command (or code) of the script ), then perform the corresponding operation. If not strictly defined, both HTML and Java can be considered as explanatory languages. The main program of Ahk/au3(autohotkey.exeand autoit3.exe) is their "interpreter". The above mentioned scripts can be "converted" into EXE executable files that can run independently from the corresponding interpreter, we can also use the corresponding tools to "Restore" them into a script file, so we can fully understand that the script code is "COMPRESSED" into this EXE file, the interpreter is also in it. When running EXE, it is actually "decompress" the script code first, then run the interpreter and explain the script.


4. How to Create a script?
You can create a script file by right-clicking the resource manager menu, or create a text file and change the extension.
5. What are some syntax rules?
A) For Ahk, each built-in function is provided in the form of a "command:
Command, param1, param2 ,...
Au3 provides the following functions:
Function (param1, param2 ,...)
Parameters enclosed by the symbols "[" and "]" in a command or function are optional parameters, indicating that they can be omitted (no specific value is given) when using these commands or functions ).
If a parameter contains spaces, double quotation marks are recommended to enclose the parameter.
B) Top-Down interpreter (from the first line to the last line) "Explain" each line of the script statement, unless you encounter statements, functions, hotkeys, or other conditions that allow the script to "jump" to an identifier, such as "return", "Goto", "gosub", and "exit.
C) keywords and identifiers (including variable names, command names, and function names) are case insensitive.
2. Run a program or open a file
1. Run the program
Run t commands or functions are used to run external executable files. Ahk can also use them to directly open files.
Ahk:
Run, target file [, working directory, max | min | hide | useerrorlevel, output PID Variable]
Au3:
Run ("file name" [, "working directory" [, Flag])
[Example 2.1.1]
Ahk:
Run, notepad.exe
Au3:
Run ("notepad.exe ")
The program notepad.exe is not provided in the example. why can it still be executed? This is because they will automatically search for the target file in the directory where the script is located. If yes, they will run. Otherwise, they will search for the target file in the system folder (% PATH %.
Note:
A) Some programs can run successfully only when a "working directory" is specified!
B) providing complete file paths helps slightly improve program reliability.
C) The Run Command of Ahk can be used to run programs and open files directly, while the run function of au3 can only be used to run programs (executable files) or PASS Parameters to open the target file for a program.
Of course, the function of running a program is not just that simple. We can also specify the initial state of the running program, for example, to maximize the display of the running notepad window (or minimize or hide ):
[Example 2.1.2]
Ahk:
Run, notepad.exe, Max
Au3:
Run ("notepad.exe", "", @ sw_maximize)

2. Open a file
As mentioned above, the Run Command of Ahk can directly open the file, while the run function of au3 can only be used to run the program, so the method of opening the file is a bit different: the Ahk script can directly provide the target file, and Ahk will automatically run the associated program of the file to open it; and au3 must pass parameters by the user to open the target file for a program.
[Example 2.2.1]
Ahk:
Run, myfile.txt
Run, notepad.exe myfile.txt
Au3:
Run ("notepad.exe myfile.txt ")

3. Run the program as a command line
You can use the command line interpreter (.exe/command.com) to run the system, specify the command to be executed, and pass parameters.
Suppose we want to execute the command "dir C: \ WINDOWS \ SYSTEM 32" to list all the files and subdirectories in the specified directory.
[Example 2.3.1]
Ahk:
Run, % comspec %/K dir c: \ windows \ system32
Au3:
Run (@ comspec & "/K dir c: \ windows \ system32 ")
Note:
A) comspec is a variable or macro built in the script to indicate the location of the command line interpreter.
B) The/k parameter indicates "executing the specified string command but retaining it". If it is changed to/C, it indicates "executing the specified string command and then final disconnection ". The intuitive explanation is that/K retains the Command Prompt window after executing the command, and/C closes the Command Prompt window after executing the command.
C) the symbol "&" is a string connector defined by au3.

4. Special Applications
A) Open the webpage
[Example 2.4.1]

Ahk:
Run, www.autohotkey.com
Run, % a_programfiles % \ Internet Explorer \ iw.e. EXE www.autohotkey.com
Au3:
Run (@ programfilesdir & "\ Internet Explorer \ iw.e. EXE www.autohotkey.com ")
B) open a special folder
Some special folders of the system are defined CLSID (Please refer to the help Documentation) You can use it to open the corresponding folder, such as the recycle bin:
[Example 2.4.2]

Ahk:
Run: {645ff040-5081-101b-9f 08-00aa 002f 954e}
Au3:
Not applicable!
C) Running Control Panel Tool
Microsoft has provided us with a command line to open a control panel tool or project, such as opening the System Properties window:
[Example 2.4.3]

Ahk:
Run Control sysdm. CPL
Au3:
Run ("control sysdm. Cpl ")
For details about the access control panel project, refer to this article: Article Address.
D) specify the search location and open the search window.
Suppose we want to open a search window and specify the search location, such as c :\:
[Example 2.4.4]

Ahk:
Run, find c :\
Au3:
Not applicable!
E) display the Properties window of the specified file
If we want to open the Properties window of "Export myfile.txt", use the keyword "properties" and then connect it to the target file:
[Example 2.4.5]

Ahk:
Run, properties myfile.txt
Au3:
Not applicable!
Note: Ahk will automatically close the attribute window before exiting!
F) use "Resource Manager" to open a specified folder
We know that you can use run, explorer C: Or run ("Explorer C:") to open the specified folder, but sometimes we need to open it in the resource manager, then you can use the keyword cmde:
[Example 2.4.6]

Ahk:
Run, cmde C:
Au3:
Run ("assumer.exe/E, C :\")
G) print the specified file
To print a specified file, use the keyword print:
[Example 2.4.7]

Ahk:
Run, print myfile.txt
Au3:
Not applicable!

F) use "Resource Manager" to open a specified folder
We know that you can use run, explorer C: Or run ("Explorer C:") to open the specified folder, but sometimes we need to open it in the resource manager, then you can use the keyword cmde:
[Example 2.4.6]

Ahk:
Run, cmde C:
Au3:
Not applicable!

Run ("assumer.exe/E, D :\")

In this way, you can open the target folder in the tree type you mentioned!
Note: The window title and window text parameters are always case sensitive.
1. Wait for window commands/functions
Both Ahk and au3 provide a similar set of window wait commands/functions: winwait/winwaitactive/winwaitclose.
They are used to wait for the window to appear, wait for the window to be activated, and wait for the window to be closed. Because these command/function parameters are similar, we only use winwait as an example.
Ahk:
Winwait [, window title, window text, timeout time, exclude title, exclude text]
Au3:
Winwait ("window title" [, "window text" [, timeout])
What is the role of winwait?All subsequent statements are not executed until the target window appears..
Suppose we want to run the Notepad program and prompt the user when its window appears:
[Example 3.1.1]

Ahk:
Run notepad
Winwait, untitled-Notepad
The msgbox notepad window has been opened!
Au3:
Run ("Notepad ")
Winwait ("untitled-Notepad ")
Msgbox (0, "", "the notepad window has been opened! ")

2. Activation window-related commands/functions
To make the target window an active window, activate it. The available command/function is winactivate:
Ahk:
Winactivate [, window title, window text, exclude title, exclude text]
Au3:
Winactivate ("window title" [, "window text"])

3. Close the window
You can close a window in either of the following ways ):
Ahk:
Winclose/winkill [, window title, window text, timeout time, exclude title, exclude text]
Au3:
Winclose/winkill ("window title" [, "window text"])

Now we can implement a relatively simple function. For example, we can open the System Properties window and wait for it to appear, activate it after the window appears, and then close it after 3 seconds:
[Example 3.1.2]

Ahk:
Run, sysdm. CPL
Winwait, system attributes
Winactivate, system attribute
Winwaitactive, system attribute
Sleep 3000
Winclose, system attribute
Winwaitclose, system attribute
Au3:
Run ("control sysdm. Cpl ")
Winwait ("system attributes ")
Winactivate ("system attributes ")
Winwaitactive ("system attributes ")
Sleep (3000)
Winclose ("system attributes ")
Winwaitclose ("System Properties ")

Suggestion: if these window titles are frequently used in the program, it may lead to a problem: the readability of the script. Maybe you will think, isn't it very intuitive? But what if the title of the repeated window is a long string? This will seriously affect the overall formatting of the Code. In addition, we do not know how the title of these windows comes from. If we define a variable (assuming the variable name is "appwindow1") to save the window title, we can use the variable to represent it in the command/function, in this way, the intention of the Code is clearer. In addition, even if the target software changes its window title for some reason (such as upgrade), we can easily make modifications.

4. More Accurate window identification method (mainly for Ahk scripts)
The program has at least one process during running. If you can obtain this process ID, you can ensure accurate identification of the window to a certain extent. In addition, each window has a defined window class name (class, for example, notepad is the class name of the notepad window), so we can exclude other window classes different from the target window. In fact, we have another more accurate method:
Each window (including controls) is assigned a unique identifier (ID) that distinguishes it from other windows, which is called the window handle (hwnd ).
A disadvantage of directly specifying the window title to indicate the window is that it cannot be ensured that the window is always the operation target during script running, in this process, there may be other windows with the same name (or Windows Meeting the matching conditions, if we use this identifier to indicate the window, we can ensure that the operation window of the command/function is always Same Window.
Let's take a look at the command/function to get the window handle:
Ahk:
Winget [, output variable, ID, window title, window text, exclude title, exclude text]
Au3:
Wingethandle ("window title" [, "window text"])
The window ID obtained by winget is returned through the "output variable", and the return value of wingethandle is the obtained window ID.

When performing automated operations, we need to run a program first. How can we obtain the window handle displayed after the program runs successfully? An insurance method is to first obtain the process ID of the program, and then obtain its window Handle Based on the process ID. Ahk supports using the process ID as the window title; however, au3 does not support this function. You can only obtain the Class Name of the window and then obtain the window handle based on the class name (not safe enough ):
[Example 3.1.3]
Ahk:
Run, notepad, thispid
Winwait, ahk_pid % thispid %

The ahk_pid indicates that the variable following is the process ID.
Winget, thisid, ID, ahk_pid % thispid %

; Thisid will save the obtained window handle
Au3:

Opt ("wintitlematchmode", 4)
Run ("Notepad ")
$ Handle = wingethandle ("classname = Notepad ")
Now we forget au3, because its window functions generally do not support the use of window handle as the window title parameter.
As for how to use a window handle in Ahk, simply put, any command with the "window title" parameter can be replaced by a window handle, for example:
[Example 3.1.4]
Ahk:
Run, notepad, thispid

First, obtain the process ID of the running Notepad program.
Winwait, untitled-Notepad ahk_pid % thispid %

; Wait for the process window to appear
Winget, thishwnd, ID, untitled-Notepad ahk_pid % thispid %

; Get the window handle
Winactivate, ahk_id % thishwnd %
The ahk_id indicates that the variable following is the window handle.
Winwaitactive, ahk_id % thishwnd %
Sleep 3000
Winclose, ahk_id % thishwnd %
Winwaitclose, ahk_id % thishwnd %
1. Simulate the mouse click (button, etc.) Control
Since it is a simulated user operation, it naturally includes a simulated mouse click.
Applicable command/function: Click/mouseclick/controlclick
Click/mouseclick is used to simulate users' Physical Operations (click) and send the mouse clicking event to the specified Coordinate Position (relative to the current window or absolute position, however, this method does not guarantee 100% accuracy. screen resolution, user interference, and system environment all affect their execution results, controlclick directly sends the mouse click event to the target control of the target window, which is more accurate. Generally, we do not consider using the Coordinate Position Method for clicking. The following uses controlclick as an example:
Ahk:
Controlclick [, target control or coordinate position, window title, window text, mouse button, number of clicks, options, exclude title, exclude text]
Au3:
Controlclick ("window title", "window text", Control ID [, button] [, number of clicks])
For Ahk, the "target control" parameter refers to the class name (classnn) or control text of the control to be clicked, you can also use the control handle (if the control handle is used, leave the first parameter blank and use ahk_id % control handle % in the second parameter ).

Q: What tools are used to obtain the information of the target control?
A: For Ahk users, use autoit3 window spy. For au3 users, use autoit window info. You can find them in the corresponding Start Menu or find them in the installation directory.

Q: How do I use these two tools?
A: First open the target window you want to operate on, then run autoit3 window spy or autoit window info, and then move the mouse over the target control (such as a button ):
Autoit3 window spy usage Demonstration:


Autoit window info usage Demonstration:


Now we assume that the "System Properties" window has been opened and activated, and the task is to click its "OK" button, the following methods can be used:
[Example 4.1.1]
Ahk:
Controlclick, OK, system attribute
Controlclick, button2, system attributes


Au3:

Controlclick ("System Properties", "", 1)
Controlclick ("System Properties", "", "button2 ")
Controlclick ("System Properties", "", "OK ")
Reminder: even if the target window or control is hidden, the controlclick command can still "click" the target control, but the success rate cannot be guaranteed.

2. Simulate keyboard operations
The keyboard is also a tool we will use in the Operation window. For example, when installing the software, the classic "one-way carriage return" method is used ". The following describes how to simulate a keyboard operation.
Send
This is the most direct method. It is to simulate the user's key-press behavior and directly send the key-press command. For usage instructions, refer to the official documentation.
The simplest application-press Enter:
Ahk:

Run, control sysdm. CPL
Winwait, system attributes
Send, {enter}
Au3:

Run ("control sysdm. Cpl ")
Winwait ("system attributes ")
Send ("{enter }")

The common combination key-Alt + x/Ctrl + N, and so on, when installing the software, a button "next (N), Which means that pressing Alt + N triggers the effect equivalent to clicking this button. Take the "file" menu in the notepad window as an example:
Ahk:

Run, notepad
Winwait, untitled-Notepad
Winactivate, untitled-Notepad
Winwaitactive, untitled-Notepad
Send ,! F
Au3:

Run ("Notepad ")
Winwait ("untitled-Notepad ")
Winactivate ("untitled-Notepad ")
Winwaitactive ("untitled-Notepad ")
Send ("! F ")
Control Operations
However, it is often difficult to achieve the expected goal only by relying on the above technologies. The following describes the most important control operations.
1. Set text
During software installation, users often need to provide necessary information, such as the installation directory. Many users do not like to install the software to the default C drive, but prefer to install them elsewhere. So what method does the script provide for us to modify the path shown in?

We first use the autoit3 window spy or autoit window info mentioned above to obtain the information of the editing box for this path. Assume that the title of this window is setup foobar, the class name in the path editing box is edit1, and we need to change it to "D: \ foobar2000". Then we can use the following command/function to set its text:
Ahk:
Controlsettext [, target control, new text, window title, window text, exclude title, exclude text]
Au3:
Controlsettext ("window title", "window text", Control ID, "new text ")
The usage is as follows:
[Example 5.1.1]
Ahk:
Controlsettext, edit1, D: \ foobar2000, setup foobar
Au3:

Controlsettext ("setup foobar", "", "edit1", "d: \ foobar2000 ")

2. Select and deselect the single-choice and check-box items
Sometimes a program needs to provide more information to meet the user's personalized settings. We often encounter this situation:


How can I ensure that the required project is selected and some projects are canceled?
The following describes the commands/functions used in Ahk and au3 to set various attributes of controls:
Ahk:
Control [, command, value, target control, window title, window text, exclude title, exclude text]
Au3:
Controlcommand ("window title", "window text", Control ID, "command", "option ")

The "command" specifies the parameters to be set. For these single button/check box buttons, the applicable commands are "check" and "uncheck ".
Assume that the title of the window is setup foobar. We plan to perform the following operations:
Select its "desktop" check box (button5), deselect the "Quick Start" check box (button7 );
Select "0.7x" (button14 ).

The usage example is as follows:
[Example 5.1.2]
Ahk:
Control, check, button5, foobar
Control, uncheck, button7, foobar
Control, check, button14, foobar
Au3:

Controlcommand ("foobar", "", "button5", "check ","")
Controlcommand ("foobar", "", "button7", "uncheck ","")
Controlcommand ("foobar", "", "button14", "check ","")

2. Select a project from the drop-down list.
I believe you have encountered the following situation:


The question is: how to select the project you need?
The answer is to use the commands/functions mentioned above. For such controls, the command that Ahk applies is Choose, n AndChoosestring , String ", Indicating that the nth project is selected and the project that matches the string is selected respectively. The command for au3 is" Setcurrentselection , N And Selectstring , String ", Indicating the n + 1 Selected (note that it starts from scratch !) Project and the project that matches the string.

Suppose we want to select the fifth project "simplified Chinese", the specific usage example is as follows:

[Example 5.1.3]
Ahk:
Control, choose, 5, combox1, Installer
Control, choosestring, simplified Chinese, combox1, Installer
Au3:
Controlcommand ("Installer", "", "combox1", "setcurrentselection", 4)
Controlcommand ("Installer", "", "combox1", "selectstring", "simplified Chinese ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.