Wednesday, November 9, 2016

Adding Terms to a Secondary Termbase Using an AutoHotkey Script

A few months ago I wrote about using Dragon to add terms to a secondary termbase. While this is great for Dragon users, it doesn't help those who either don't use Dragon or want to use their keyboard to accomplish this. Luckily, there's a great tool to automate practically any task one can think of: AutoHotkey.

So, below is a very simple AutoHotkey script to help achieve something that's not currently possible in SDL Trados Studio: adding a term to a secondary termbase while we work.

If you don't have it yet, you will first need to download AutoHotkey and install it.

To get started quickly, you will also want to download my Add Term to Secondary Termbase script. The script is an ahk file that must be saved where you can find it later. Please note that I've only recently started learning about AutoHotkey, so I'm aware that this is not the most sophisticated or elegant solution to this problem, but it works for me, so I thought I'd share it in case others find it useful.

A general note about ahk files: AutoHotkey files can be easily inspected and edited by opening them in a text editor. I like Notepad++. To make the script active, once AutoHotkey has been installed, all you need to do is double-click the ahk file. To disable the script, go to the system tray and locate the green icon with a white H, right-click and select Exit. You can have several scripts active at once. Hovering over each of the green icons will display a tooltip indicating the name of the appropriate script/ahk file.

What this script does
This script launches a series of steps that one would perform manually to add a term to a termbase that is open in MultiTerm. Note that this works in the Flags layout in MultiTerm. Other layouts (such as the Default layout that will be available with MultiTerm 2017), may require a different sequence of steps.

Hotkey
The hotkey used to deploy this script from within Studio is F12. This is just a personal preference. Please note that activating the script means that F12 will no longer do whatever it does now in Studio, so if you're using F12 for something else, you can change the hotkey to one that won't create any conflicts. To do that, you simply open the ahk file in a text editor, replace F12:: with your own preferred hotkey, for example F9:: (the two colons designate the hotkey in AutoHotkey), save the file and double-click on it again to activate the changes.

These are the steps that are launched after pressing the hotkey:

1. The selected text is copied to the clipboard.
    Note: The source term should be selected prior to hitting the hotkey.

2. Focus is changed from Studio to MultiTerm.
    For this to work, MultiTerm must be open, with the appropriate termbase where terms are to be added open as well.

3. The cursor is placed in the source field.

4. The contents of the clipboard are pasted into the source field.

5. The cursor is placed in the target field.

At this point, the target term must be entered manually. After you've finished typing in the target term, hit F12 again, and the following will happen:

6. The entry will be saved to the termbase

7. Focus will be returned to Studio

8. The cursor will be placed in the target column of the Studio Editor

Dissecting the script
This is the content of the script, with a brief explanation of each line.


So, to summarize what you need to do to start adding terms to a secondary termbase as you work in Studio:

1. Open Multiterm and open your secondary termbase
2. From within Studio, when you want to add a new term to your secondary termbase, select the source term and press F12
3. When the cursor appears in the target field in Multiterm, enter your target term.
4. Press F12

And here's a short video demonstrating the process:



Final words
Like I said earlier, I only know the bare minimum about AutoHotkey, although seeing its potential, I plan to keep learning. If you'd also like to learn more, the AutoHotkey website is a great resource, and I can't recommend these two blogs enough for specific use cases relevant to translators:

Multifarious: Paul Filkin has two great posts about AutoHotkey that can help us get started with the tool. 

Gonduana: If you read Spanish, Jesús Prieto's series of posts about AutoHotkey are both enjoyable to read and very educational.

So there you have it, a not-so-elegant but working solution to our little problem. I hope you find it useful!

Update
Thanks to Jesús Prieto's suggestions, there is now a Version 2 of the script, which you can download here. With this new and improved script you can now also copy the target term to the clipboard. After doing that, focus changes to MultiTerm, where both terms are entered into the appropriate fields, the entry is saved and focus is returned to Studio.

To use this new script, load it by double clicking on the file and then:

1. In Studio, select your source term, then press F12. A message will pop-up asking you to select the target term.

2. Select the target term.

3. Click OK to close the message box. A new message box will pop up displaying the source and target terms, click OK (or press Enter).

After doing this, the script will switch focus to MultiTerm, where your secondary termbase must be open, paste the terms in the appropriate fields and save the entry, all without any user intervention. Focus will then be returned to Studio.

See it in action here:












6 comments:

  1. Great idea, Nora!

    I took the freedom to modify the first part of your script a bit to make it more robust:

    #IfWinActive ahk_exe SDLTradosStudio.exe
    F12::
    Clipboard := "" ; empty the clipboard
    Send, {Control Down}{c}{Control Up}
    ClipWait, 3 ; wait for text to appear on the clipboard, if there isn't any after 3 seconds display error message
    If ErrorLevel
    {
    ToolTip, Please make sure to select text before pressing the hotkey.
    SetTimer, RemoveToolTip, 3000
    Return

    RemoveToolTip:
    SetTimer, RemoveToolTip, Off
    ToolTip
    Return
    }

    WinActivate, SDL MultiTerm
    Sleep, 500
    Send, {F3}{Tab}
    Sleep, 200
    Send, {Tab}{Control Down}{v}{Control Up}
    Sleep, 500
    Send, {Tab}
    Return

    #IfWinActive ahk_exe MultiTerm.exe
    F12::
    Send {F12}
    Sleep 500
    WinActivate, SDL Trados Studio
    Send {F6}
    Return

    I'm currently also looking into using the application programming interface (API) of MultiTerm to connect add terms instead of relying on "tab" pressing since it would make the script more universal and not depending on one specific termbase definition.

    Best regards,
    Raphaël

    ReplyDelete
    Replies
    1. This is great Raphaël! Thank you for taking the time to enhance it. At my current level of non-proficiency, I would never have been able to make these enhancements, and beyond the improvements to this script, your additions will help me learn more about AutoHotkey. I love the idea of using the MultiTerm API to make the script more universal. Thanks again for sharing your knowledge on this!

      Delete
  2. Nice script, Nora!

    One more idea:

    This script will get the source & target terms using a king of Wizard:

    ; status: source term selected
    F12::

    Clipboard =
    Send, {Control Down}{c}{Control Up}
    ClipWait, 0
    stringSource = %Clipboard%

    msgbox, Please move to the target segment in Studio, select the term, and then click OK

    Clipboard =
    Send, {Control Down}{c}{Control Up}
    ClipWait, 0
    stringTarget = %Clipboard%

    ; Just to confirm both variables are fine
    MsgBox, %stringSource%`r`n%stringTarget%

    return


    Regards,

    … Jesús Prieto …

    ReplyDelete
    Replies
    1. Thank you Jesús! I've updated the post with an improved script based on your contribution.

      Delete