قالب وردپرس درنا توس
Home / Tips and Tricks / To write an AutoHotkey script

To write an AutoHotkey script



  autohotkey
AutoHotkey

AutoHotkey is a fantastic but complicated program. It was originally intended to bind custom hotkeys to various documents but is now a complete Windows automation package.

AHK is not very difficult to learn for new users, because the general concept is quite simple, but it is full, Turing ̵

1; Full programming language. You will get the syntax much easier if you have a programming background or are familiar with the concepts.

Installing and using AutoHotkey

AutoHotkey installation process is straightforward. Download the installer from the official website and run it. Select "Express Installation". After installing the software, right-click anywhere and select New> AutoHotkey Script to create a new script.

 New Auto Movie Script

AHK scripts are text files with a .ahk extension. Right-clicking on them will give you some options:

  • "Run Script" will load your script with AHK runtime.
  • "Compile Script" will be bundled with an AHK executable to create an EXE file you can run.
  • "Edit Scripts" opens your script in your default text editor. You can use notebooks to write AHK scripts, but we recommend using SciTE4AutoHotkey, an editor for AHK that supports syntax highlighting and debugging.

  compile autohotkey script

While a script is running, whether it is an EXE or not -You find it running in the background of the Windows message area, also known as the system tray. Look for the green icon with an "H" on it.

To exit, pause, reload or edit a script, right-click the notification icon and select an appropriate option. Scripts continue to run in the background until you quit them. They will also go away when you log out of Windows or restart your computer.

 Autohotkey script running

How does AutoHotkey work?

In its core, AHK does one thing action for hot keys. There are many different actions, quick command combinations and control structures, but all scripts will work on the same principle. Here's a basic AHK script that launches Google Chrome when you press Windows + C:

  #c ::
Run Chrome
return 

The first row defines a hot key. The pound sign (#) is short for the Windows key and c is the C key on the keyboard. Then there is a double colon (: 🙂 to denote the start of an action block.

The next line is an action. In this case, the operation starts an application with the command Run . The block is completed with a return at the end. You may have a number of actions before return . They will all be fired in succession.

Like you, you have defined a simple key-to-action mapping. You can place as many of them as you want in a file .hk and set it to run in the background, always look for hotkeys to remap.

Shortcuts and Modifiers

You can find a complete list of AHK's modifiers in official documentation, but we will focus on the most useful (and call) features.

The modification keys all have single characters. For example, #! ^ + are Windows, Alt, Control and Shift, respectively. You can also distinguish between left and right Alt, Control and Shift with < and > modifiers, which opens up a lot of space for extra hotkeys. For example, + is the right Shift. Take a look at the key list for everything you can refer to. (Spoiler: You can refer to almost every key. You can also refer to other non-keyboard input devices with a small extension.)

You can combine as many keys as you want to a hotkey, but you will soon end up with key combinations to remember. Here are modifiers that allow you to do crazy things, come in. Let us break down an example from the AHK documents:

 autohotkey directives

The green #IfWinActive is called a directive and applies additional context for hotkeys physically below it in the script. Any hotkey after that will only fire if the condition is true, and you can group several hotkeys according to a directive. This directive will not change until you strike another directive, but you can reset it with a blank #If (and if it seems like a hack, welcome to AHK).

The directive here checks whether a particular window is openly defined by ahk_class Notepad . When AHK receives the "Win + C" entry, it will burn the action during the first #IfWinActive only if the directive is true and check the other if it did not. AHK has many directives, and you can find them all in the documents.

AutoHotkey also has hotstrings, which act as hotkeys except to replace a whole series of text. This is similar to how autocorrect works – in fact, there is an auto-correction script for AHK but supports some AHK action.

 autohotkey hotstrings

Hotstring will only match the string if it is written exactly. It will automatically remove matched text to replace hotstring as well, although this behavior can be adjusted.

Actions

An action in AHK is all that has an external effect on the operating system. AHK has many measures. We cannot explain them all, so we choose some useful ones.

Most of these actions will also have information-oriented commands associated with them. For example, you can write to the clipboard, but you can also have the contents of the clipboard stored in a variable and run functions when the clipboard is changed.

Connect it all with control structures

AHK would not be what it is but all the control structures that make it Turing-complete.

In addition to the Directives, you also have access to About within Action Blocks. AHK has for loops, curly brace blocks, and and Catch statements and many others. You can access external data from the activity block and store it in variables or objects to be used later. You can define custom functions and labels. Actually, you can easily do something in another programming language that you can certainly do in AHK with a little headache and a look through the documents.

Imagine that you have a boring and repeated task that requires you to click on several buttons in a row and wait for a server to respond before you do it again ad infinitum. You can use AHK to automate this. You want to define a few loops to move the mouse to specific locations, click and then move to the next location and click again. Throw some waiting claims so it doesn't break. You can even try to read the color of the image on the screen to determine what happens.

One thing is for sure, your script will probably not be cute. But neither is AutoHotkey, and it's okay.




Source link