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 …

Reply
  • 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 …

Children
  • Thank you Jesus, but what I need to do is fill in a client's custom form (a log report in Excel), where the source and the target may not be pasted adjacently (for example, sometimes they need to be pasted on either side of a comments column), and where only segments with issues need to be reported, so no need for a full export of the file.