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

Children