-3

I would like to select non-adjacent words while holding down some shortcut key, append them (space-separated) to one single string and put the final, appended string into the clipboard.

Something like:

{Control_down}::

OnDoubleClick{ my_appended_string += Str(current_text_selection) }

{Control_up}

Clipboard := my_appended_string 
return

Just so it works :-)

Can you help?

Pingui
  • 1,312
  • 4
  • 15
  • 28

1 Answers1

0
; LCtrl+DoubleClick on words in text
<^LButton Up::
    if (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey < DllCall("GetDoubleClickTime"))
    {
        ; KeyWait, LCtrl            ; after releasing LCtrl 
        clip_old := clipboard       ; save the clipboard to the variable clip_old (as text))
        clipboard := ""             ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
        Click 2                     ; select a word by doubleclick
        Send, {Ctrl down}c{Ctrl up} ; copy the selected word
        ClipWait 1                  ; wait for the clipboard to contain data. 
        If (!ErrorLevel)            ; If NOT ErrorLevel ClipWait found data on the clipboard
        {
            clip_new := clip_old A_Space clipboard              ; append the new copied text to clip_old 
            clipboard := clip_new 
        }
        else
        {
            MsgBox, No text copied
            clipboard := clip_old               
        }
        ToolTip, %clipboard%
        Sleep, 1000
        ToolTip
    }
Return

EDIT:

Try also this

; Press F1 after putting the mouse pointer over the word to copy

F1 Up::
    clip_old := clipboard       ; save the clipboard to the variable clip_old (as text))
    clipboard := ""             ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
    Click ; on the word under the mouse pointer
    Send, {Ctrl Down}{Left}{Ctrl Up}{Ctrl Down}{Shift Down}{Right}{Shift Up}{Ctrl Up} ; select the word
    Sleep, 300
    Send, {Ctrl down}c{Ctrl up} ; copy the selected word
    ClipWait 1                  ; wait for the clipboard to contain data. 
    If (!ErrorLevel)            ; If NOT ErrorLevel ClipWait found data on the clipboard
    {
        clipboard := Trim(clipboard) ; remove leading/trailing spaces and tabs
        clip_new := clip_old A_Space clipboard ; append the new copied text to clip_old 
        clipboard := clip_new 
    }
    else
    {
        MsgBox, No text copied
        clipboard := clip_old               
    }
    ToolTip, %clipboard%
    Sleep, 1000
    ToolTip
Return
user3419297
  • 9,537
  • 2
  • 15
  • 24
  • Thank you, that looks like you are really knowledgeable not just about AHK and you also did nice commenting. Unfortunately, however, I always get the `No text copied`-error message, never worked for me. Would you be so nice and look into this as it would be a real shame if that great piece of work failed for some minor reason. It goes without saying that I'll mark this as answered then :-) – Pingui Feb 14 '23 at 13:05
  • Works fine here. Run it first in a stand-one script (Use only this code in the script and close all other scripts that may interfere). Uncomment the line `; KeyWait, LCtrl` in the edited answer to copy after releasing LCtrl, if it can be of any help. You can also use another hotkey eg. `F1 & LButton Up::` (F1+DoubleClick). – user3419297 Feb 14 '23 at 13:53
  • I'm sorry to say that I tried everything proposed, but it still doesn't work on my end. Do you mean that you can successfully [hold down ctrl, then click e.g. the words "Works" "fine" "here", release the ctrl-key again] and subsequently see them in the Windows clipboard history (Win+v)? I see there neither one string nor the single words. – Pingui Feb 14 '23 at 19:03
  • Try also the second code I added. – user3419297 Feb 14 '23 at 19:30
  • Thank you very much, but the new code just opens the help file and if I try the same code but with F5 instead of F1, nothing happens at all (no entries in Win+v thereafter). I will nevertheless upvote your answer for your effort, and you say for you it works. – Pingui Feb 15 '23 at 13:05
  • You can check every line of the code by adding a message box after it: _After F1 Up::_ `MsgBox, F1 Up`, _after clip_old := clipboard_ `MsgBox, %clip_old%` etc. Try alco to [run the script as administrator](https://stackoverflow.com/a/43299069/3419297) and add `Process, Priority, ,High` on top. – user3419297 Feb 16 '23 at 07:43
  • And [exclude Autohotkey.exe from Windows Defender](https://support.microsoft.com/en-us/windows/add-an-exclusion-to-windows-security-811816c0-4dfd-af4a-47e4-c301afe13b26). – user3419297 Feb 16 '23 at 07:51