GTtC IV – Scripts for Faster Post-editing

This is the fourth post in the Getting Translators to Code series. If you missed the previous one, I encourage you to check them out.

In this post, perhaps one of the last ones about Autohotkey before we move to Python, we are going to write some scripts to make post-editing easier. We covered already how we can use shortcuts to trigger actions, like looking a term up in a dictionary; now we are going to do something similar to manipulate words in a sentence.

If you are an experienced post-editor, you are aware by now that post-editing involves a lot of switching, moving words around.  Some common examples:

  • You have  “car red” in a segment, so you’d typically delete one word and then type it in the right place.  This is not the most efficient way – just switching the order of these two words would be way faster. And you can do that with a script. Similar scenarios with three words are not uncommon.
  • You have something like “It is the car most comfortable and safe“, where car needs to go at the end of the sentence. You’d typically cut car, place your cursor at the end of the sentence and paste. Again, it’s what we all do, but there’s a more efficient way. You probably guessed: a script that can move the selected word(s) to the end of the sentence! A very similar scenario is having to move word(s) to the beginning of the sentence. We can do that too.

We are introducing a very important general concept here: regular expressions. If you are not familiar with regex, think of them as wildcards (these you probably know from using Word or any other text editor). Basically, you can use operators to describe a search pattern. For example, if you wanted to look for the word “file” but not others like “files” or “fileshare“, you could use the boundary operator \b to enclose the word and only consider “file” in your search. You probably do this same thing when you check the “whole words only” in your search box.

In a nutshell, regular expressions give flexibility to your searches. There are many good tutorials on how to use them out there. is a good starting point.

So, we are ready now to see how we can write these scripts in AutoHotkey.

Switching two words:

^,::                           ; pressing CONTROL + , (I chose comma) triggers the script
clipboard =                    ; we start off with an empty clipboard
 prevClipboard := ClipboardAll ; keep everything in the Clipboard, including formatting
 SendInput, ^c                 ; we simulate pressing CTRL + C (copy)
 ClipWait, 1                   ; we give the process some time to perform the copy
 Clipboard := RegExReplace(Clipboard, "(\w+)\s(\w+)","$2 $1")  ; we do a search and replace operation using regular expressions - we look for a word (\w+) followed by a space (\s) and another word, and we replace that with the second word ($2) followed by the first word ($1) - we just switched two words; they are still in our Clipboard though
 SendInput, {Backspace}        ; simulate a Backspace to delete the words we selected
 Sleep 50                      ; we wait a few miliseconds for the process to take place
 SendInput, ^v                 ; we paste the Clipboard contents, sending a CTRL + v
return                         ; and that's all

Switching three words:

^.::                           ; pressing CONTROL + . launches the script
clipboard = 
 prevClipboard := ClipboardAll
 SendInput, ^c 
 ClipWait, 1
Clipboard := RegExReplace(Clipboard, "(\w+)\s(\w+)\s(\w+)","$3 $2 $1") ; just like in the script above, we use regular expressions, this time with three words - then we switch the order with $3, $2, $1
SendInput, {Backspace}
 Sleep 50
 SendInput, ^v 

Moving text to the beginning:

^#Left::                          ; CTRL + WIN + Left arrow to run the script
clipboard = 
 prevClipboard := ClipboardAll
 SendInput, ^x                    ; the highlighted text is cut (CTRL+X)
 ClipWait, 1
 SendInput, {Backspace}           ; we delete the selected text
 SendInput, {Home}                ; cursor is placed at the beginning of the sentence
 Sleep 30
 SendInput, ^v                    ; we paste the text we selected before, from the clipboard
 SendInput, {Space}               ; we add a space so it's separated from the next word

You can easily reuse the code above and, changing only two lines, you can have a script that moves text to the end of the sentence.

Can you figure out which two lines you need to change? Can you come up with other ideas for similar time-saving scripts? If so, go ahead, leave your comments below!


Leave a Reply

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

You are commenting using your 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