Wednesday, July 3, 2019

Create Your Own Macros with AutoHotkey

While AutoHotkey is a powerful tool that can be used to automate tasks in any program, in this article, aimed at translators, I will use examples of applications in SDL Trados Studio, simply because that's my main CAT tool. Hopefully, however, the basic principles explained here can be easily transferred to any other program.

Let's start with an example of what AutoHotkey can help us achieve. Let's say that we often need to copy source to target and then confirm the segment.

This usually requires two steps:

                                                1. Copy source to target: Ctrl + Insert
                                                2. Confirm segment: Ctrl + Enter

What if we could combine them into a single shortcut? This AutoHotkey script will allow us to do just that. By pressing Alt+e, the Ctrl+Insert and Ctrl+Enter steps will be executed.

                        !e::
                        Send ^{Insert}
                        Sleep 200
                        Send ^{Enter}
                        Return

By looking at the information above and then the script, you can quickly figure out the following:

  • "!" represents the Alt key
  • "^" represents the Control key
  • Key names need to be enclosed in curly brackets when they are in the body of the script
  • The "Send" command is used for key presses
  • A "Sleep" command is used to wait between executing the steps (in this case 200 ms) 
  • The "Return" command indicates the end of the script

The first line in the script (!e::) is the hotkey, or shortcut, that we will use to trigger the actions in the script. Hotkeys are selected by the user and must always end with a double colon.

How do I get started?

Follow the steps below to create your first AutoHotkey script.

1. Download AutoHotkey (www.autohotkey.com) and install it. Once installed, you won't see anything happen, that's normal. AutoHotkey runs in the background and allows you to run your own scripts (macros).

2. Go to a folder in Windows Explorer where you would like to save your script. I have a folder called AutoHotkey Scripts just to keep them all in one place. Right-click on an empty space in the folder and select New-AutoHotkey Script. Give a name to your script and save it. The extension of an AutoHotkey script file is ahk.

So far, you have the empty "skeleton" of a script. Now you need to enter the actions you want it to execute.

3. Right-click on the script and select Open, then open it with a text editor, such as Notepad (I prefer Notepad++, available for free).

4. Once the file is open, you will see that there's already four lines of code in it. Enter your script code in a new line, below the existing code.


Note that I've added a directive in line 6 (#IfWinActive ahk_exe SDLTradosStudio.exe). This tells AutoHotkey that this hotkey is only valid in SDL Trados Studio. Otherwise, Alt+e would trigger Ctrl+Insert followed by Ctrl+Enter in every program on my computer.

5. Save the file. Now go to the folder where the file is saved, and double-click the file. This will load the script. Look for a green square with a white H in it in your system tray, which indicates that the script is active.

6. Now that the script is active, go to SDL Trados Studio, press Alt+e in the target column of a segment, and see what happens!

Adding scripts to an existing file

Now that we understand the basics of AutoHotkey, let's add more scripts/macros to our file.

A single ahk file can contain a single script or multiple scripts. Here we will add a second script to the same file we have just created.

The IfWinActive directive applies to all the scripts below it, so no need to add it again. With the file open in Notepad++, we will just go to line 14 and start adding the new script there.

For the second example, I suggest we add a script that deletes everything from the position of the cursor to the end of the segment.

To accomplish this, we would need the following steps:

1. Press Control+Shift+Page Down
2. Press Delete

In the following key list, we can see the names of the keys we need.


Download a PDF version of the key list here.

I will use Control+d as the hotkey/shortcut for this new script, so it will look like this:


After adding the new code and saving the file, I need to reload it for the new script to become available. I can do this either by double-clicking on the file in Windows Explorer, or by right-clicking on the green square AutoHotkey icon in the system tray and then selecting "Reload This Script".


To test the new script, go to SDL Trados Studio, place your cursor in the middle of some target text and press Ctrl+d. Doesn't that feel like magic?

While there's a lot more to AutoHotkey than the simple explanation included here, hopefully these basic steps will get you started to create your own macros for repetitive actions.

Resources
  • The AutoHotkey forum in the SDL Community has lots of scripts shared by fellow translators that are ready to use. That's also a great place to ask for help with your own scripts.
  • The AutoHotkey documentation can be a bit overwhelming for new users, but that's the place to find everything you could possibly want to know about AutoHotkey.
  • Paul Filkin's blog, Multifarious, has some great articles on AutoHotkey. 
  • If you read Spanish, be sure to check out Jesus Prieto's blog, Gonduana, where you will find lots of detailed information about AutoHotkey.