Tuesday, November 14, 2017

Replace All that Really Means Replace All

When doing a find and replace operation in SDL Trados Studio, clicking Replace All right after entering your find and replace strings does nothing: you have to click Find Next first, then click Replace All.

This has long been a pet peeve of mine, so I thought I would try and find a way to make this behavior more similar to what happens in MS Word, where clicking Replace All really does mean that all found instances will be replaced at once, and I'm happy to report that I've found a solution that works well for me.

What I needed was a way to still click Replace All and have that automatically go click Find Next and then Replace All again, without me having to do anything else.

To accomplish this, I used AutoHotkey. Here's how you can implement this simple solution yourself.

Warning: This works for the English interface of Studio. If you use a different language and would like to adapt it, follow the steps at the end of this article.

Step 1: AutoHotkey should be installed (if needed, you can download it here)

Step 2: Download my Replace All AutoHotkey script, and load it by double-clicking on the file.

To use the script, open Find and Replace as usual, filling out the necessary fields and checking the necessary checkboxes. Once everything is ready, simply click Replace All and the script will take care of the rest, clicking Find Next, then Replace All again, as required by Studio.

Script breakdown

;------------------------------------------------------------------------------
;Replace All no longer requires "Find Next" first, by Nora Díaz
;------------------------------------------------------------------------------
~LButton::
ifWinActive Find and Replace
{
  MouseGetPos,,,,Ctrl

  if (Ctrl = "WindowsForms10.BUTTON.app.0.29531c8_r9_ad19")

  {

  Controlclick, Find &Next, Find and Replace
  Sleep 1000
  Controlclick, R&eplace All, Find and Replace

  }
}
Return

Explanation
The script first determines if the button being clicked is the Replace All button, which is designated by its ID WindowsForms10.BUTTON.app.0.29531c8_r9_ad19:

  MouseGetPos,,,,Ctrl

  if (Ctrl = "WindowsForms10.BUTTON.app.0.29531c8_r9_ad19")


If the Replace All button has been clicked, then the script triggers a series of events where it clicks the Find Next button in the Find and Replace window and then the Replace All button in the Find and Replace window.

  Controlclick, Find &Next, Find and Replace
  Sleep 1000
  Controlclick, R&eplace All, Find and Replace

I find that the delay between clicks is necessary to give the script the time to find the next instance of the text to be replaced. When the delay is too short, the two buttons are clicked quickly in succession and nothing happens, as Studio has not had enough time to find the first instance to be replaced.

Adapting the script to work with other languages
Since button and window names obviously change depending on SDL Trados Studio's interface language, the script above will only work for the English interface. However, this can be easily adapted to work with other languages, by replacing the information highlighted below with the appropriate language text.

;------------------------------------------------------------------------------
;Replace All no longer requires "Find Next" first, by Nora Díaz
;------------------------------------------------------------------------------
~LButton::
ifWinActive Find and Replace
{
  MouseGetPos,,,,Ctrl

  if (Ctrl = "WindowsForms10.BUTTON.app.0.29531c8_r9_ad19")

  {

  Controlclick, Find &NextFind and Replace
  Sleep 1000
  Controlclick, R&eplace All, Find and Replace

  }
}
Return

The yellow highlights are for the button names, while the blue highlights are for the window names.

To figure out what should be used to replace the highlighted text, have a look at your window in Studio. For example, for Spanish, the Find and Replace window looks like this:


The window is called Buscar y reemplazar
The buttons are called Buscar siguiente and Reemplazar todo

An ampersand (&) is needed right before the letter designated for the Alt shortcut in the button, which appears underlined in the buttons in the image above, so the text to be used in the script should be:

Buscar &siguiente
Ree&mplazar todo

Thus, the modified script for Spanish (which you can download here) would look like this:

;------------------------------------------------------------------------------
;Replace All no longer requires "Find Next" first, by Nora Díaz - Spanish
;------------------------------------------------------------------------------
~LButton::
ifWinActive Buscar y reemplazar
{
  MouseGetPos,,,,Ctrl

  if (Ctrl = "WindowsForms10.BUTTON.app.0.29531c8_r9_ad19")

  {

  Controlclick, Buscar &siguienteBuscar y reemplazar
  Sleep 1000
  Controlclick, Ree&mplazar todoBuscar y reemplazar

  }

Return

That's all it takes to adapt the script, but if, like me, you're learning about AutoHotkey and would like to know where this information came from, here's a little extra information.

While initially working on this script, I used AutoHotkey's Window Spy to determine the correct window and button names. To access this useful tool, right-click on the green AutoHotkey icon of any running script and select Window Spy, then click on the desired window, in this case Studio's Find and Replace window.

For the example below, I clicked on the Replace All (Reemplazar todo) button.


Tip: Use Win+A to freeze the display; otherwise, the information changes whenever you move the mouse. Use Win+A to unfreeze.

So, that's all. If you find this script useful and adapt it for other languages, feel free to share it in the Comments section for other users who may benefit from it.

And, of course, any improvements from more advanced AutoHotkey users are more than welcome!