2

I want to write an Autohotkey text to edit the text that the user selects and append four spaces at the beginning of every line.

If I would simply have a string with multiple lines the task would be easy.

I googled and found one solution at the official Autohotkey forum. That solution copies the text into the clipboard potentially deleting the clipboard content.

Is there a way to do this in autohotkey that doesn't use the clipboard where I can directly operate on the selected text?

Christian
  • 25,249
  • 40
  • 134
  • 225
  • 1
    Getting the text from a control is certainly possible. What control, though? You don't actually say. If the control is a web page edit box, for example, AHK lets you access the DOM and you can use selectors to get at the element and its content. But if you are worried about using the clipboard, why don't you just store the present contents and then do your thing and then restore the clipboard from the saved variable? – PGilm Mar 31 '16 at 16:12
  • @PGlim : Basically the DOM that has focus. Does a Webpage has fundamentally different DOM's than they editing text in Evernote? Is it possible to access both in the same way? – Christian Mar 31 '16 at 21:11
  • 1
    Duplicate of previously asked quesiton: http://stackoverflow.com/questions/36067563/get-selected-text-without-using-the-clipboard (not answered though) – phil294 Apr 04 '16 at 12:18
  • 1
    Possible duplicate of [Get Selected Text Without Using the Clipboard](https://stackoverflow.com/questions/36067563/get-selected-text-without-using-the-clipboard) – HaveSpacesuit Aug 24 '17 at 19:25

2 Answers2

2

Here are some basic Clipboard techniques:

ClipSaved := ClipboardAll   ;  Save the entire clipboard to a variable of your choice (in this case, ClipSaved).

;  Here is where you put your code

Clipboard := ClipSaved   ;  Restore the original clipboard.
ClipSaved =   ;  Free the memory in case the clipboard was very large.

More here: https://autohotkey.com/docs/misc/Clipboard.htm

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
PGilm
  • 2,262
  • 1
  • 14
  • 26
  • Alas, if by any chance you have an image copied to the clipboard or something else large, this makes the PC shortly freeze due to how slow copying/restoring the clipboard is in AHK. But, unfortunately, I am not aware of any other reliable way to get a selected text. – DDRRSS Oct 30 '21 at 19:32
  • 1
    Sure, true (images, e.g.). Note `ClipWait` has a flag for non-text stuff and some "persistent" clipboard utilities (including some written in AHK - check the help for Clipboard at bottom of https://www.autohotkey.com/docs/misc/Clipboard.htm) will have saved the image already to a clipboard stack so you can forgo that saving step step and just remove the space-check clip from the stack and you're back to beginning with image on top. – PGilm Nov 01 '21 at 14:11
1

Well, this one requires your mouse cursor to hover over the window with the selected text:

MouseGetPos,,,thiswindow,thiscontrol
ControlGet,VarX,selected,,%thiscontrol%,ahk_id  %thiswindow%
;Do something with "VarX" here.
Anonymous
  • 168
  • 12