Search & Replace for multiple terms/phrases

Hi,

I'm just responding to this thread in here with a possible AHK solution:

 RE: Search & replace multiple words/terms in pre-translated segments? 

^!h:: ; The hotkey for Ctrl+Alt+H to start the process
global filePaths := {} ; Create an associative array to store full file paths
global fileNames := "" ; Create a variable to store the file names
global PosX, PosY ; Create global variables to store the dropdown menu's position

Gui, Add, DropDownList, w450 vFilePath ; Add a dropdown list to the GUI with width of 400
Gui, Font, s12 ; Set the font size to 12

Loop, Read, c:\Users\pfilkin\Documents\SDL\AppStore\2023\Search_Replace_AHK\settings.txt ; Read the hardcoded settings file line by line
{
    SplitPath, A_LoopReadLine, name, dir, ext, name_no_ext, drive ; Split the file path into parts
    fileNames .= name_no_ext . "|" ; Add the file name (without extension) to the string, separated by "|"
    filePaths[name_no_ext] := A_LoopReadLine ; Store the full file path in the associative array
}

fileNames := SubStr(fileNames, 1, StrLen(fileNames) - 1) ; Remove the last "|"
GuiControl,, FilePath, %fileNames% ; Set the items of the dropdown list

WinGetPos, PosX, PosY,,, A ; Get the position of the active window
PosY := PosY - 150 ; Subtract 150 from PosY
Gui, Show, x%PosX% y%PosY% w500 h100, Select a file ; Show the GUI at the position of the active window with a larger window
WinSet, AlwaysOnTop, On, A ; Make the GUI always stay on top of other windows
return

GuiClose: ; When the GUI is closed
Gui, Submit, NoHide ; Store the dropdown list's current selection in the variable filePath
filePath := filePaths[filePath] ; Retrieve the full file path from the associative array
Gui, Destroy ; Destroy the GUI
GoSub, StartReplace ; Continue with the search and replace operation
return

StartReplace:
WinActivate, ahk_exe SDLTradosStudio.exe ; Activate SDL Trados Studio

Loop, Read, %filePath% ; Read the selected file line by line
{
    StringSplit, line, A_LoopReadLine, `t ; Split the line into "line1" (search term) and "line2" (target term)
    searchTerm := line1 ; Assign the search term
    targetTerm := line2 ; Assign the target term

    ; Click in the "Find what" field
    ControlClick, Edit2, ahk_exe SDLTradosStudio.exe
    Sleep, 100
    ; Select all text and delete it
    Send, {Home}+{End}{Del}
    Sleep, 100
    ; Send the search term keystrokes
    Send, {Text}%searchTerm%
    Sleep, 100

    ; Click in the "Replace with" field
    ControlClick, Edit1, ahk_exe SDLTradosStudio.exe
    Sleep, 100
    ; Select all text and delete it
    Send, {Home}+{End}{Del}
    Sleep, 100
    ; Send the target term keystrokes
    Send, {Text}%targetTerm%
    Sleep, 100

    ; Click the "Replace All" button
    ControlClick, WindowsForms10.BUTTON.app.0.ea119_r8_ad19, ahk_exe SDLTradosStudio.exe
    Sleep, 100
}

; PosY := PosY - 150 ; Subtract 150 from PosY
Gui, 2:New, +AlwaysOnTop ; Create a new GUI for our custom message box
Gui, 2:Font, s12 ; Set the font size to 12
Gui, 2:Add, Text, w500 h50, The search and replace operation is complete. ; Add a text element to the GUI
Gui, 2:Show, x%PosX% y%PosY% w500 h100, Operation Complete ; Show the GUI at the position of the dropdown menu with the title "Operation Complete"

Sleep, 500 ; Wait for 500 milliseconds (half a second)
WinActivate, Operation Complete ; Activate the GUI window using its unique title

return

2GuiClose: ; When the custom message box is closed
Gui, 2:Destroy ; Destroy the GUI
return

I definitely didn't write this on my own... I used ChatGPT by starting small and slowly enhancing the script until I had what I needed.  I think it took me around 3-hours from start to finish last night, but I think it's a good example of how we can use AI to help with our work.  I created a short video to explain how this works as it may not be obvious without some context.

emoji

Paul Filkin | RWS Group

________________________
Design your own training!

You've done the courses and still need to go a little further, or still not clear? 
Tell us what you need in our Community Solutions Hub

Parents Reply Children