SDL Trados Studio
SDL Trados GroupShare
SDL Trados Business Manager
SDL Trados Live
SDL MultiTerm
SDL Passolo
SDL Speech to Text
SDL Managed Translation - Enterprise
SDL MultiTrans
SDL TMS
SDL WorldServer
Translation Management Connectors
SDL LiveContent S1000D
SDL Contenta S1000D
SDL XPP
SDL Tridion Docs
SDL Tridion Sites
SDL Content Assistant
SDL Machine Translation Cloud
SDL Machine Translation Connectors
SDL Machine Translation Edge
Language Developers
Tridion Developers
Tridion Docs Developers
Xopus Developers
Community Help
SDL User Experience
Language Products - GCS Internal Community
SDL Community Internal Group
SDL Access Customer Portal
SDL Professional Services
SDL Training & Certification
Style Guides
Language Technology Partner Group
SDL Academic Partners
SDL Enterprise Technology Partners
XyUser Group
ETUG (European Trados User Group) Public Information
Machine Translation User Group
Nordic SDL Tridion Docs User Group
SDL Tridion UK Meetup
SDL Tridion User Group New England
SDL Tridion West Coast User Group
SDL WorldServer User Group
Tridion Docs Europe & APAC User Group
Tridion User Group Benelux
Tridion User Group Ohio Valley
SDL MultiTerm Ideas
SDL Passolo Ideas
SDL Trados GroupShare Ideas
SDL Trados Studio Ideas
SDL Machine Translation Cloud Ideas
SDL Machine Translation Edge Ideas
SDL Language Cloud TMS Ideas
SDL Language Cloud Terminology Ideas
SDL Language Cloud Online Editor Ideas
SDL Managed Translation - Enterprise Ideas
SDL TMS Ideas
SDL WorldServer Ideas
SDL Tridion Docs Ideas
SDL Tridion Sites Ideas
SDL LiveContent S1000D Ideas
SDL XPP Ideas
Events & Webinars
To SDL Documentation
To SDL Support
What's New in SDL
Detecting language please wait for.......
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 (Former Member and Raphaël Toussaint 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 := ClipboardvList := " ;continuation section(dog perro
#r::
ClipSaved := ClipboardAll
Clipboard =
SendInput, ^a^c
ClipWait, 30
FixString := Clipboard
vList := " ;continuation section
(
dog perro
house casa¿ ¿ / / , , ? ? . .pie 2 pie2m 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 clipboardSleep 200Send ^vReturn
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 tagSleep 100Send ^z ;undoSleep 50Send ^cClipWait, 30FixString := ClipboardvList := " ;continuation section(organisation organization¿ ¿ / / , , ? ? . .pie 2 pie2m 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 clipboardSleep 200Send ^vReturn
;SendInput, ^a^c
;ClipWait, 30
Send ^+D ;delete to next tag
Sleep 100
Send ^z ;undo
Sleep 50
Send ^c
organisation organization
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, ^aSend, ^cStringReplace, clipboard, clipboard, dog, perro, AllStringReplace, clipboard, clipboard, cat, gato, AllStringReplace, clipboard, clipboard, raining, lloviendo, AllSend ^vReturn
#p::
Send, ^a
Send, ^c
StringReplace, clipboard, clipboard, dog, perro, All
StringReplace, clipboard, clipboard, cat, gato, All
StringReplace, clipboard, clipboard, raining, lloviendo, All
So, any help with this would be greatly appreciated.
Thank you!
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, ^aSend, ^cLoop, Parse, vList, `r, `n{ oTemp := StrSplit(A_LoopField, "`t") FixString := StrReplace(FixString, oTemp.1, oTemp.2)}SendInput, %FixString%
Loop, Parse, vList, `r,
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
By the way, do you know this Studio app, TermInjector? I haven't played around with it for a very long time, but depending on what the exact purpose of your script is, the app might also do the trick natively, and then tags should be fine.
1) If you don't provide the complete path of the file in the script, but only its name, then it needs to be in the same folder as the script, otherwise wherever you want:
FileRead, vList, C:\Users\ndia\Documents\vListFile.txt
If you move the file later on, then you have to update the file location accordingly in your script.
2) That is up to you too, since you define the separator yourself in the script:
StrSplit(A_LoopField, A_Tab)
Edit: typos
Since you use ClipboardAll at the start of your script, you know that it is different from Clipboard: ClipboardAll is binary, while Clipboard is just text.
See https://autohotkey.com/docs/misc/Clipboard.htm
When you use "FixString := Clipboard" in your script, you have already reduced your string to text, i.e. the tags are gone.
The AutoHotKey webpage above indicates that "altering a binary-clipboard variable (by means such as StringReplace) will revert it to a normal variable, resulting in the loss of its clipboard data", so even if you use "FixString := ClipboardAll", subsequent StringReplace commands would reduce FixString to text.
The webpage also states that "binary-clipboard variables may be passed to functions by value (formerly they only worked ByRef)", so if you could figure out the binary format used you might be able to write your own routine to make changes to the clipboard data, but I suspect it wouldn't be all that easy.
I have never tried to deal with this binary data, but maybe one of the AutoHotKey experts has some experience in this area ...
Best regards,Bruce CampbellASAP Language Services
About the special characters ñ and á, I had a similar problem when I processed strings in a Dragon command.
The problem occurred because the string was being converted from Unicode to ASCII.
I was using the Windows registry to pass strings between Dragon commands. Unfortunately strings in the Windows registry are ASCII only, so when another command retrieved the string the special characters had been converted to regular characters without the accents.
I had to write a more complicated interface that converted each Unicode character in the string into three permissible "ASCII characters" (excluding ASCII zero, which is used as the terminating byte for the registry string) and then reversed the conversion when the string was retrieved.
Maybe a similar Unicode to ASCII conversion is happening somewhere in your situation.
I just noticed that you are using a text file. (Sorry, I am not following all the details of what you are doing...)
Are you saving strings to a text file and then retrieving them?
If so, maybe check the encoding of the text file.
Check by opening the file, and then opening the "Save As" dialog (File->Save As).
At the bottom of the dialog box you will see a drop-down list for "Encoding:"
I think "ANSI" is the default. Try changing it to "Unicode" and see whether this helps.
Good morning, Nora!
Here we go:
ControlSend
Send, ^v
Don't hesitate to get back to me if anything is still unclear ;-)
Have a great day!
Raphaël