Copy source text segment with AutoHotkey

The following AutoHotkey script copies the source text segment into the Clipboard.

Requirement: segment must be unlocked.

 

; ------------------------
; Copy source text segment
; CTRL + SHIFT + C
; ------------------------
^+c::
    CheckKey_CONTROL_Up()
    CheckKey_SHIFT_Up()
    
    ; Select all in TARGET
    SelectAll()
    ; Copy all (text + tags) from TARGET
    Copy()
    Target := ClipboardAll    
    
    ; Copy from SOURCE to TARGET
    SendInput !{Insert}
    ; Select all + Copy text
    SelectAll()
    Copy()
    Source := Clipboard
    
    ; Restore TARGET
    length := StrLen(Target)
    if (length)
    {
        Undo()
        SendInput {Right}
    }
    ; Delete the following 2 lines (else + SendInput)
    ; if you want to insert SOURCE > TARGET
    ; whenever TARGET is empty
    else
        SendInput !{Delete}  
    
    ; Copy SOURCE text to Clipboard
    Clipboard := Source
return


Copy()
{
    clipboard = ; Empty Clipboard
    SendInput {control down}c{control up}
    ClipWait, 0
}

Paste()
{
    SendInput {control down}v{control up}
    Sleep, 200
}

SelectAll()
{
    SendInput {ctrl down}a{ctrl up}
}

Undo()
{
    SendInput {ctrl down}z{ctrl up}
}

; Check if CONTROL key is UP
CheckKey_CONTROL_Up()
{
    while GetKeyState("Control", P)
        Sleep, 20
}

; Check if SHIFT key is UP
CheckKey_SHIFT_Up()
{
    while GetKeyState("Shift", P)
        Sleep, 20
}

Parents
  • Hi Jesus,

    I'm trying to make this work for a very specific use case. I need to copy-paste the source and target text for some segments in an Excel report for a client. I must be doing something wrong, because when I go to the Excel file and enter Ctrl+V to paste the text, the target text is pasted instead of the source. What could I be missing?
  • Hi Nora,
    I've just tested the script with 6 segments in Trados Studio 2017 and it has worked for me.
    Wondering if you are using the same script or a modified one to copy source+target at the same time. If it's the latter, maybe you can share it with me and I'll have a look.
    … Jesús Prieto …
  • Thank you Jesús, I used it as is (was intending to modify it but first wanted to test it by itself). I'm thinking I may need to play with the wait times.
  • Yes, I'd say so as well.
    I'm curious to know the reason if you find out.
    Best,
    … Jesús Prieto …
  • Hi Jesús,

    Finally had some time to look more closely at the script. The problem was that I don't use Alt+Insert to copy source to target, but a custom shortcut (Alt+W). Once I spotted that and changed it, your script is now working perfectly for me.

    Now I have a question: how would I need to modify the script so that the source and target are stored in separate variables that I can then insert with consecutive Ctrl+V presses?

    In other words, I want to copy both source and target to the clipboard, then go to Excel, press Ctrl+V once to paste the source text and then press Ctrl+V again to paste the target, then clear the clipboard to repeat the operation.

     

    Edit: To clarify, I don't need to export the entire file to Excel, I need a script that will work on a select segment basis, that is, for the active Studio segment only.

  • I think that something like this will work:

    ExportedFile := "Exported file.txt"

    SourceAndTarget  := Source .  "`t" . Target  . "`r`n"

    FileAppend, %SourceAndTarget  %, %ExportedFile%

    It'll export Source and Target to a tab separated TXT file in the same folder as the AHK script.

    If you can loop all the segments, you've got it!

    Notes:

    • Not compulsory, but you might want to use the function FileDelete to initialize the exported file.
    • I've had issues with carriage returns in the Target (they go to another line in Excel). I didn't find out a solution, sorry… Be careful with them.
    • Anyway, are you sure that the SDLXLIFF converter for Microsoft Office plugin is not enough? Just some column and tag cleaning in Word or Excel might be faster & easier.

    Please let me know how it goes

    … Jesús Prieto …

  • Hi Nora!

    I have put something simple and straightforward together which should suit your needs. The hotkeys can of course be customized as needed. Have a look and let me know if it works for you or how it can be improved.

    #IfWinActive, ahk_exe SDLTradosStudio.exe

    #!c:: ;- [Win]+[Alt]+[ C ]

    OriginalCb := ClipboardAll ;~ back up current clipboard content

    Clipboard := "" ;~ Empty clipboard

    SendInput, {F6}^+{Down} ;~ [F6] to activate source; [Shift]+[Ctrl]+[Down] to select whole paragraph/segment (since [Ctrl]+[ A ] doesn't work in source)

    SendInput, ^c

    ClipWait, 3 ;~ Wait for centent to appear on clipboard; after 3 seconds, give error message

    If ErrorLevel = 1

    {

    ToolTip, Error`nNo text was copied to the clipbard.

    SetTimer, ToolTipOff, 3000

    Return

    }

    Source := Clipboard ;~ copy source text to variable "Source"

    Clipboard := ""

    SendInput, {Left}

    SendInput, {F6}^a

    SendInput, ^c

    ClipWait, 3

    If ErrorLevel = 1

        Return

    Target := Clipboard ;~ copy target text to variable "Target"

    Clipboard := OriginalCb ;~ restore original clipboard content

    SendInput, {Left}

    X:= A_CaretX

    Y := A_CaretY+40

    ToolTip, Source [Win]+[Alt]+[ S ]:`n%Source%`n`nTarget [Win]+[Alt]+[T]:`n%Target%, %X%, %Y% ;~ display source and target (as well as keyboard shortcuts for pasting them) in tooltip

    Return

     

    #IfWinActive

    #!s::

    ToolTip

    SendInput, %Source%

    Return

     

    #!t::

    ToolTip

    SendInput, %Target%

    Return

     

    ToolTipOff:

    SetTimer, ToolTipOff, Off

    ToolTip

    Return

Reply
  • Hi Nora!

    I have put something simple and straightforward together which should suit your needs. The hotkeys can of course be customized as needed. Have a look and let me know if it works for you or how it can be improved.

    #IfWinActive, ahk_exe SDLTradosStudio.exe

    #!c:: ;- [Win]+[Alt]+[ C ]

    OriginalCb := ClipboardAll ;~ back up current clipboard content

    Clipboard := "" ;~ Empty clipboard

    SendInput, {F6}^+{Down} ;~ [F6] to activate source; [Shift]+[Ctrl]+[Down] to select whole paragraph/segment (since [Ctrl]+[ A ] doesn't work in source)

    SendInput, ^c

    ClipWait, 3 ;~ Wait for centent to appear on clipboard; after 3 seconds, give error message

    If ErrorLevel = 1

    {

    ToolTip, Error`nNo text was copied to the clipbard.

    SetTimer, ToolTipOff, 3000

    Return

    }

    Source := Clipboard ;~ copy source text to variable "Source"

    Clipboard := ""

    SendInput, {Left}

    SendInput, {F6}^a

    SendInput, ^c

    ClipWait, 3

    If ErrorLevel = 1

        Return

    Target := Clipboard ;~ copy target text to variable "Target"

    Clipboard := OriginalCb ;~ restore original clipboard content

    SendInput, {Left}

    X:= A_CaretX

    Y := A_CaretY+40

    ToolTip, Source [Win]+[Alt]+[ S ]:`n%Source%`n`nTarget [Win]+[Alt]+[T]:`n%Target%, %X%, %Y% ;~ display source and target (as well as keyboard shortcuts for pasting them) in tooltip

    Return

     

    #IfWinActive

    #!s::

    ToolTip

    SendInput, %Source%

    Return

     

    #!t::

    ToolTip

    SendInput, %Target%

    Return

     

    ToolTipOff:

    SetTimer, ToolTipOff, Off

    ToolTip

    Return

Children