Help with a multiple text replacement script

Even though this forum so far has been for sharing scripts and not for writing them, I was wondering if I could pick the brains of AHK experts ( and  come to mind right now), to figure something out.

I'm trying to put together a script to do multiple text replacements at a segment level, i.e., for the active segment only, so I can easily see what has been changed, without calling up the Find & Replace window.

I've managed to put together this script (silly examples included):

#r::
ClipSaved := ClipboardAll
Clipboard =
SendInput, ^a^c
ClipWait, 30
FixString := Clipboard
vList := " ;continuation section
(
dog perro

house casa
¿ ¿
/ /
, ,
? ?
. .
pie 2 pie2
m 2 m2
)"
Loop, Parse, vList, `n
{
oTemp := StrSplit(A_LoopField, "`t")
FixString := StrReplace(FixString, oTemp.1, oTemp.2)
}
oTemp := ""
Clipboard := FixString ; load the new string to clipboard
Sleep 200
Send ^v
Return

This works fine in segments with no tags, but when there are tags, they get stripped at some point during the replacement operation and the text that is pasted back into the segment has all the necessary replacements but no tags. Is there any way of preserving the tags in the clipboard?

I came up with a very clumsy workaround for this, which involves using Studio's Delete to Next Tag shortcut, so instead of Select All-Copy, the script would do Delete to Next Tag-Undo-Copy:

#r::
ClipSaved := ClipboardAll
Clipboard =
;SendInput, ^a^c
;ClipWait, 30
Send ^+D ;delete to next tag
Sleep 100
Send ^z ;undo
Sleep 50
Send ^c
ClipWait, 30
FixString := Clipboard
vList := " ;continuation section
(
organisation organization
¿ ¿
/ /
, ,
? ?
. .
pie 2 pie2
m 2 m2
)"
Loop, Parse, vList, `n
{
oTemp := StrSplit(A_LoopField, "`t")
FixString := StrReplace(FixString, oTemp.1, oTemp.2)
}
oTemp := ""
Clipboard := FixString ; load the new string to clipboard
Sleep 200
Send ^v
Return

While this also works in segments with no tags, I would like to optimize it.

My second question is: how would I go about creating a list of all these replacements (CSV? Excel?) and getting the script to take them from there instead of having to add them manually to the script? I've been reading up on arrays but I'm still far away from being able to implement what I need.

I have another simpler script attempt with just multiple StringReplace lines (see below), but again, that would require creating possibly hundreds of replacement lines and I imagine it's not the best solution.

#p:: 
Send, ^a
Send, ^c
StringReplace, clipboard, clipboard, dog, perro, All
StringReplace, clipboard, clipboard, cat, gato, All
StringReplace, clipboard, clipboard, raining, lloviendo, All
Send ^v
Return

So, any help with this would be greatly appreciated.

Thank you!

Parents
  • Hi Nora,

    I'll start with some suggestions to your second question about how to better implement the search and replacement items:

    Separating search and replacement parts by a tab is smart, but you don't need to put these into your script. It is way easier to maintain a simple text file where you add them and then read that text file into a variable like this:

    FileRead, vList, vListFile.txt

    In order to run your search and replace, just use a simple loop:

    Send, ^a
    Send, ^c

    Loop, Parse, vList, `r, `n
    {
        oTemp := StrSplit(A_LoopField, "`t")
        FixString := StrReplace(FixString, oTemp.1, oTemp.2)
    }

    SendInput, %FixString%

    As for the tag question, I need to look a bit deeper into this, but I fear it won't be easy without (again) the help of Studio APIs.

    Kind regards,
    Raphaël

Reply
  • Hi Nora,

    I'll start with some suggestions to your second question about how to better implement the search and replacement items:

    Separating search and replacement parts by a tab is smart, but you don't need to put these into your script. It is way easier to maintain a simple text file where you add them and then read that text file into a variable like this:

    FileRead, vList, vListFile.txt

    In order to run your search and replace, just use a simple loop:

    Send, ^a
    Send, ^c

    Loop, Parse, vList, `r, `n
    {
        oTemp := StrSplit(A_LoopField, "`t")
        FixString := StrReplace(FixString, oTemp.1, oTemp.2)
    }

    SendInput, %FixString%

    As for the tag question, I need to look a bit deeper into this, but I fear it won't be easy without (again) the help of Studio APIs.

    Kind regards,
    Raphaël

Children