Getting Translators to Code III – Create Your Very Own AutoCorrect

In previous posts, we covered an introduction to Autohotkey, with a small script to automate steps to email many recipients, and we created a practical search script to speed up research.

This time, we are going to see how easy it can be to use hotstrings to create your very own autocorrect feature. Technically, this is not coding, but you will see that we are going to touch on some general ideas that are useful for writing code.

Hotstrings

Hotstrings are mainly used to expand text, like abbreviations, a long product name, or a list of email addresses. To turn any string into a hotstring in Autohotkey, you need to enclose it between two pairs of colons. For example:

::asap:: as soon as possible

When you type asap (no need to type the colons), the string will trigger the expansion “as soon as possible”. This happens automatically when an ending character is typed. Ending characters are:

-()[]{}':;"/\,.?! Enter Tab

I’m sure that if you spent many hours of your life translating, editing, post-editing, etc., you remember some long phrase that you had to type over and over. For cases like those, hotstrings can save time and headaches.

Hotstrings can trigger actions

You can also use strings to trigger some actions, like launching your browser and opening your favorite reference site. And this is so simple that it can be done with three short lines of code. In the example below, we are going to use the string “mw” to open the Merriam-Webster dictionary.

:*:mw::  ;this is your hotstring, what you want to type to trigger the action. The * means no ending character is required to trigger the action

Run, % "http://www.merriam-webster.com";this translates as open my browser to open the URL enclosed in quotes. Of course you can choose any other URL you like.

Return; my script ends here, go ahead and run the code!

If you try this, you will notice how much faster and efficient this is compared to using your mouse. The characters you type for the hotstring are automatically deleted, so there’s no need to remove them manually.

Your Own AutoCorrect

Autocorrect is a feature in any program that allows you to automatically correct text you type. Some modern CAT tools include this feature already. MS Word does too. However, it does make sense to take a look at how we can build an autocorrect with words of your choice that works in any program you may want to use, from Notepad to Memoq.

The idea is simple: you can use a common misspelling as a hotstring and have it trigger the right spelling. A few quick examples:

:: acheive:: achieve

::vigourous::vigorous

::monday::Monday

How can you create your own Autocorrect then? All you need to do is to put all your hoststrings together in a text file, save it with the .ahk extension (myfile.ahk), and just double-click on it to activate it. The process is the same for all Autohotkey scripts. Remember that if you put the script in your Windows Startup folder, it will be active every time you start your computer.

There is an existing script created by the AHK community we can learn a lot from. They did a couple of smart things here. One is that they took a list of common misspellings from Wikipedia, and turned each entry into a hotstring. Genius – you don’t need to manually enter common misspellings.

Also, an option was added to highlight a word and add it to your AutoCorrect list:

hotspell

Let’s take a look at the code and see how it works; it is annotated (and I’m adding a few comments for clarity) so it should be fairly easy to understand:

#h:: ;this means “run the code when Win + h are pressed”

AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.

ClipboardOld = %ClipboardAll%

Clipboard =  ; clipboard must be empty for detection to work.

Send ^c      ; this simulates pressing CTRL+c, i.e., copy

ClipWait 1   ; this gives a little bit of time for the clipboard to work

    return

StringReplace, Hotstring, Clipboard, ``, ````, All  ; replace quotes with double quotes in the text copied to the clipboard and store it in a variable named Hotstring - do this replacement first to avoid interfering with others below.

StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; replace line breaks with carriage returns

StringReplace, Hotstring, Hotstring, `n, ``r, All ; ; replace line breaks with carriage returns

StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All ; replace tabs

StringReplace, Hotstring, Hotstring, `;, ```;, All ;replace semi-colons

Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard.




; Show an input box, providing the default hotstring:

InputBox, Hotstring, New Hotstring, Provide the corrected word on the right side. You can also edit the left side if you wish.`n`nExample entry:`n::teh::the,,,,,,,, ::%Hotstring%::%Hotstring%




if ErrorLevel <> 0  ; if user presses Cancel

    return

; Otherwise, add the hotstring and reload the script:

FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Add the hotstring to the file. Put a `n (line break) at the beginning in case file lacks a blank line at its end.

Reload ; reload the script

Return

 

I seriously encourage to give this a try. Not only using this script for AutoCorrect, but also to take a look at how it is written. You will see some interesting things, for example, how a general rule to change “ign” into “ing” is used, with exceptions for words like foreign and align.

 

 

Advertisements

2 Comments Add yours

  1. Michel Marques says:

    Interesting, I also use one script for that. I thought you might be interested in this glossary script: https://t.co/gAJPXdq2Q7 .

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s