Getting Translators to Code II – A Powerful (yet simple) Search Script

In the previous post, I introduced AutoHotkey and showed you how to create hotstrings and a quick script to automate the process of sending an email to a list of recipients.

In this post, we are going to focus on creating a search script. Why? The obvious reason: translators have to do research all the time – definitions, images, meanings, collocations, synonyms, references, terminology, etc… The not-so-obvious reason: research takes up a lot of time, and by partially automating this process, you’ll simply be working in a more efficient way.

In a nutshell, the idea is trying to replace your regular search process (opening your browser, typing a URL, typing/copying/pasting in your keywords) with just a keyboard shortcut. And we can make this happen really easily with Autohotkey.

Let’s then write a script that, after highlighting text, when we press CONTROL + G (1), will copy such text (2), open a browser window and append the text to a URL (3), in this case, Google, and show us the results:

^g::                                  ;CONTROL + g launches our script (1)
 clipboard =                          ;we make sure our clipboard is empty
 prevClipboard := ClipboardAll        ;we create a variable called prevClipboard 
                                      to store the contents we want to copy.
                               ClipboardAll contains everything on the clipboard.
 SendInput, ^c                        ;we simulate pressing CONTROL+C (copy) (2)
 ClipWait, 1                          ;wait until the clipboard contains data
 Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "(^\s+|\s+$)")
                                      ;we are using a regular expression here to 
                                      replace line breaks (\r = carriage return, 
                                      \n = line feed) and one or more (+) spaces
                                      (\s) at the beginning (^) or end ($) of the
                                      text string
 Clipboard := RegExReplace(Clipboard, "("")","")   ;we also want to remove quotes
Run, % "http://www.google.com/search?hl=en&q=" Clipboard   
                                      ;now we add our copied text (stored in 
                                      Clipboard) to Google's search URL, and use 
                                      Run to put it in our browser (3)
Clipboard := prevClipboard            ;empty the Clipboard
return                                ;we indicate our code finishes here

This same script could include, for example, a Google Images search. The only thing you’d need to do is to replace

Run, % "http://www.google.com/search?hl=en&q=" Clipboard

with

Run, % "http://images.google.com/images?hl=en&q=" Clipboard

And of course, you may want to change the shortcut, for example, to Control + i, so you can choose to do a regular or an image search with the same script.

Based on this same concept, you can run any other searches you want, like a dictionary search:

Run, % "http://www.merriam-webster.com/dictionary/" Clipboard

 

You can even send a Google Translate query with this method:

Run, % "https://translate.google.com/#en/es/" Clipboard

Here, note that I’m specifying the languages (#en/es).

By observing the URL a site uses for a search, you can simply replace any keywords with “Clipboard” to get the expected results.

zxzx

So, how do you use this again? Your script needs to be running in order to use it. You need to either a ) double-click the script file (remember that you can include several scripts in the same file, and you need to change the file extension to “.ahk” for it to work with Autohotkey), or b) add the script file to your Startup folder in Windows. The latter will ensure the scripts in that file can be run every time you start your computer.

startup

 

Advertisements

One Comment Add yours

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