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

To write an AutoHotkey script



  autohotkey
AutoHotkey

AutoHotkey is a fantastic but complicated software. Originally, it was intended to use custom shortcuts for various actions, but it is now a full Windows automation suite.

AHK is not particularly hard to learn for new users because the general concept is fairly simple, but it is a complete Turing complete programming language. It will be much easier to pick up the syntax if you have a programming background or are familiar with the concepts.

Installing and Using AutoHotkey

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

 New Autohotkey script

AHK scripts Text files with the extension .ahk . If you right-click it, you have some options:

  • Run Script loads your script with the AHK runtime.
  • "Compiling a script" bundles it with an AHK executable to build an EXE file.
  • Edit Script opens the script in your default text editor. You can use Notepad to write AHK scripts, but we recommend using SciTE4AutoHotkey, an editor for AHK that supports syntax highlighting and testing – you'll find it in the background in the Windows notification area (also known as the taskbar). Look for the green icon with an "H".

    To stop, pause, reload, or edit a script, right-click the notification icon and select an appropriate option. Scripts run in the background until you stop them. They also go away when you log out of Windows or reboot your PC, of ​​course.

     Autohotkey script running

    How AutoHotkey works

    At the core, AHK does one thing – tying actions to hotkeys. There are many different actions, hotkey combinations and control structures, but all scripts work on the same principle. Here's a simple AHK script that launches Google Chrome when you press Windows + C:

      #c ::
    Run Chrome
    return 

    The first line defines a hotkey. The number sign (#) stands for the Windows key and c is the C key of the keyboard. Then there is a colon (:) for the beginning of an action block.

    The next line is an action. In this case, the action starts an application with the command Run . The block is finished with a return at the end. You can perform any number of actions before return . They are all triggered one after the other.

    This is how you have defined a simple key-to-action mapping. You can place any number of them in a file .ahk and set them to run in the background, always looking for hotkeys that need to be remapped.

    Hotkeys and Modifiers

    You can find a complete list of AHK modifiers in the official documentation. However, we focus on the most useful (and coolest) features.

    The modifier keys all have single-line shortcuts. For example #! ^ + are Windows, Alt, Control and Shift. You can also use the modifiers [ and > to differentiate left and right alt, control, and shift, leaving plenty of room for additional hotkeys. For example, + is the right-shift key. The key list contains everything you can reference. (Spoiler: You can reference almost all buttons, you can even reference other input devices outside the keyboard with a small extension.)

    You can combine as many buttons as you want in a hotkey, but you'll do it soon No keyboard shortcuts anymore. This is where modifiers come in to help you do crazier things. Let's summarize an example from the AHK documents:

     Autohotkey Directives

    The Green #IfWinActive is a directive called, and applies additional context to hotkeys that are physically in the script underneath. Any hotkey after that will fire only if the condition is met, and you can group several hotkeys under a directive. This directive does not change until you hit another directive, but you can reset it with a space #If (and if this seems like a hack, welcome to AHK).

    The directive here checks if a particular window is open, defined by ahk_class Notepad . If AHK receives the "Win + C" input, the action under the first #IfWinActive will be fired only if the directive returns true, and then the second if it does not. AHK has many instructions and you can find them all in the documents.

    AutoHotkey also has hotstrings that work like hotkeys, except for the replacement of a whole text string. This is similar to how AutoCorrect works - in fact, there is an auto-remediation script for AHK - but supports any AHK action.

     Auto Hotkey Hotstrings

    The hotstring string only matches the string if entered exactly. It automatically removes the matching text to replace the hotstring, although this behavior can be adjusted.

    Actions

    An action in AHK is anything that affects the operating system. AHK has many actions. We may not be able to explain all, so we will pick out some useful ones.

    Most of these actions also include information-oriented commands. For example, you can write to the clipboard, but you can also get the contents of the clipboard to store in a variable and perform functions when the clipboard changes.

    Tying Everything Together with Control Structures

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

    In addition to the instructions #If you also have access to If within action blocks. AHK has for loops, curly bracket blocks, try and catch statements and many others. You can access external data within the action block and save it to variables or objects for later use. You can define custom functions and labels. Anything else you could easily do in another programming language, you can probably do in AHK with a bit of a headache and a look at the documentation.

    For example, imagine you have a boring, repetitive task where you have to click multiple buttons in a row and wait for a server to answer before repeating it again and again. You can use AHK to automate this. You want to define some loops to move the mouse to specific locations, click, and then move to the next location and click again. Enter a few wait statements so it will not break. You could even try reading the color of the pixels on the screen to see what's going on.

    One thing is for sure: your script probably will not be pretty. But AutoHotkey is not alright.




Source link