Search code examples
textautohotkey

Quickly send long text in AutoHotkey


I've been trying to figure out how to insert/expand long text faster. The current keystroke method I'm using is quite time consuming and therefore something I would rather avoid.

Right now I am using the following method:

::abc::all bad cats

Or for longer text:

::li::
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

However the second method is a little slow.

Any suggestions for how I can avoid this slow expansion method? Perhaps by using the clipboard to copy and paste from the AHK script?


Solution

  • Try this:

    ::li::
    ClipSaved := ClipboardAll ; save the entire clipboard to the variable ClipSaved
    clipboard := ""           ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
    clipboard =               ; copy this text:
    (
    Lorem ipsum dolor ...
    line2
    ..
    )
    ClipWait, 2              ; wait max. 2 seconds for the clipboard to contain data. 
    if (!ErrorLevel)         ; If NOT ErrorLevel, ClipWait found data on the clipboard
        Send, ^v             ; paste the text
    Sleep, 300               ; don't change clipboard while pasting! (Sleep > 0)
    clipboard := ClipSaved   ; restore original clipboard
    VarSetCapacity(ClipSaved, 0) ; free the memory in case the clipboard was very large.
    return
    

    If you often have to send such a complex or long text, you can create a function, for not repeating the whole code every time:

    ::li::
    my_text =
    (
    Lorem ipsum dolor ...
    line2
    ...
    )
    Send(my_text)
    return
    
    
    Send(text){
        ClipSaved := ClipboardAll
        clipboard := ""
        clipboard := text
        ClipWait, 1
        If (!ErrorLevel)
            Send, ^v        
        Sleep, 300
        clipboard := ClipSaved
        VarSetCapacity(ClipSaved, 0)
    }
    

    See ClipboardAll and ClipWait