6
votes

I am trying to create a pretty basic text wrapper in AutoHotKey for use when programming. I got it to work using the clipboard to copy the selected text, modify it, then paste it, but I am trying to refrain from using the clipboard since it does not work well in conjunction with my Clipboard Manager. Does anyone know how to do this?

!r:: ;Alt+R+%Char% = Wrap Text with Input Characters
    ClipSave := ClipboardAll
    Send ^c
    Input, Char, L1
    if ("" . Char = "{")
    {
        clipboard = {%clipboard%}
    }
    else if ("" . Char = "[")
    {
        clipboard = [%clipboard%]
    }
    else if ("" . Char = "(")
    {
        clipboard = (%clipboard%)
    }
    else
    {
        clipboard = %Char%%clipboard%%Char%
    }
    StringReplace, clipboard, clipboard,%A_SPACE%",", All
    Send ^v
    Clipboard := ClipSave
    ClipSave = 
return

Note: I have seen ControlGet, text, Selected and attempted to implement it, but it did not work (no error, just no action). If anyone has a solution to this, that would fix my issue.

2
Where is the textfield from which you want to copy text? Is it a normal windows forms field; can you see it's classNN using the window spy utility? - Forivin
@2501 Both attempt and problem are contained in the question. - Michelfrancis Bustillos
@Forivin I is usually within a Word or Notepad document, but sometimes is text on a webpage, in a PDF, etc. - Michelfrancis Bustillos
The question is fine. He's not asking for a code rewrite. He's simply asking how to get the text selection without using the clipboard. That is specific enough if you ask me. That being said, I don't know the solution. Reading text selections from controls that have a classNN is easy. But for custom controls there is no native AHK way. I'm currently trying to find out how this works. Depending on how complicated it is, I might write an AHK function to do it. - Forivin
@MichelfrancisBustillos Okay it is possible using UIAutomation. Someone already wrote a set of classes for autohotkey that could take care of this: github.com/neptercn/UIAutomation/blob/master/UIA.ahk But man is this complicated. You probably have to read into this stuff quite a bit. But from what I can tell you will need to create a IUIAutomationElement instance, call GetCurrentPattern(patternId) on that (not sure how to retrieve patternId) and then call Text_GetSelection() on the result of that. - Forivin

2 Answers

2
votes

Credit to Solar on the AutoHotkey forums for proposing the following solution

Get text from edit controls with ControlGet

This method is a bit unreliable, as it will only work for specific control types. However, it may be the solution you are looking for, as it does not use the clipboard at all.

WinActive("A")                           ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Edit\d+"))       ; attempt copying without clipboard
    ControlGet, text, Selected,, %ctrl%
}

Here's a proposed solution which attempts to copy text with ControlSend, but falls back to using the clipboard as a backup if needed.

WinActive("A")                           ; sets last found window
ControlGetFocus, ctrl
if (RegExMatch(ctrl, "A)Edit\d+"))       ; attempt copying without clipboard
    ControlGet, text, Selected,, %ctrl%  
else {                                   ; fallback solution
    clipboardOld := Clipboard            ; backup clipboard
    Send, ^c                             ; copy selected text to clipboard
    if (Clipboard != clipboardOld) {
        text := Clipboard                ; store selected text
        Clipboard := clipboardOld        ; restore clipboard contents
    }
}
MsgBox % text
-1
votes

Your "clipboard manager" will most probably work with ctrl+c. Add the $ option, so it won't get triggered by your alt+r-hotkey, thus not intervened.

$^c::
     ....