get the selected text in any active window Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:01

The code fail to get the selected text in any active window. How can it be fixed? Thank you!

Code: Select all

$^+F12::

   WinActive("A")
   ControlGetFocus, my_Control
   ControlGet, my_Text, Selected,, %my_Control%
   MsgBox % "my_Text = " . my_Text

return

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:06

Code: Select all

F3::
ControlGet, sel, Selected,, Edit1, ahk_exe notepad.exe
MsgBox, 64, Selected text, %sel%
Return
image220112-0106-001_cr.png
image220112-0106-001_cr.png (32.68 KiB) Viewed 5538 times

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:11

Hi @mikeyww Thank you for your quick reply! Is it possible to make it work in any desktop applications, e.g. Notepad, Word, Excel, Evernote ...?\\

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:15

Retrieves the selected text in an Edit control.
Try it and see. Some controls might not be standard edit controls.

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:16

mikeyww wrote:
12 Jan 2022, 01:15
Retrieves the selected text in an Edit control.
Try it and see. Some controls might not be standard edit controls.
I just tried to retrieve the control in my_Control in the code in my original post. Could you have a look at it? Thanks!

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:19

image220112-0119-001_cr.png
image220112-0119-001_cr.png (41.48 KiB) Viewed 5503 times

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:20

Code: Select all

   ControlGetFocus, my_Control
   MsgBox % "my_Control = " . my_Control
In Evernote, this is what I get
Attachments
Image.png
Image.png (2.25 KiB) Viewed 5502 times

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:21

mikeyww wrote:
12 Jan 2022, 01:19
image220112-0119-001_cr.png
The code failed in Evernote.

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:22

As far as I know, it is not an edit control, but I have not experimented much with it. You may want to try Acc. https://www.autohotkey.com/boards/viewtopic.php?t=40590

My short test did not actually work with Acc, but I did not test it much. You could search the forum for Evernote, as there are some other posts about it.
Last edited by mikeyww on 12 Jan 2022, 01:25, edited 1 time in total.

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:24

mikeyww wrote:
12 Jan 2022, 01:22
As far as I know, it is not an edit control, but I have not experimented much with it. You may want to try Acc. https://www.autohotkey.com/boards/viewtopic.php?t=40590
And it does not work in Chrome. Is there any solution? Thank you!

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:25

You could search the forum for Evernote. I do not use it. I believe that there are some posts about Evernote. Acc works in Chrome depending on the need; see the forum posts about Chrome-- lots of them.
Last edited by mikeyww on 12 Jan 2022, 01:26, edited 1 time in total.

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:26

mikeyww wrote:
12 Jan 2022, 01:25
You could search the forum for Evernote. I do not use it. I believe that there are some posts about Evernote.
OK. Thank you!

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 01:27

There could be hundreds or thousands of AHK forum posts about Chrome-- would have a look.

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 01:28

mikeyww wrote:
12 Jan 2022, 01:27
There could be hundreds or thousands of AHK forum posts about Chrome-- would have a look.
I would try to see. Thank you!

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: get the selected text in any active window  Topic is solved

Post by swagfag » 12 Jan 2022, 06:33

u can straight up abandon the idea of having one solution that would work across any application universally. some applications use win32 controls, some do not. some implement MSAA/UIA fully, some implement accessibility features incorrectly, some dont even bother. for those, ure left with memory reading which is implemented on a case by case basis

the closest thing u can get to a universal solution, would be capturing the screen, isolating the selected text via img manipulation and OCRing the text. this, of course, limits u to what u can see on screen, and while there may be ways to retrieve the offscreen contents, surprise surprise, they arent universal. needless to say OCRing is also very error prone

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: get the selected text in any active window

Post by RussF » 12 Jan 2022, 07:23

Code: Select all

F3::
   clipboard := ""
   Sleep 100
   Send ^c
   ClipWait, 1
   my_Text := clipboard
   MsgBox % "my_Text = " . my_Text
return
Russ

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 07:35

I like that one! :) Save 100 ms by removing the sleep.

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: get the selected text in any active window

Post by RussF » 12 Jan 2022, 07:43

What's 100 ms among friends? :D
I grabbed that code from an existing script I had and I think I put that in because I had seen so many posts where the clipboard took a while to stabilize and people weren't gettin the results they expected. You can use ClipWait to determine when content goes INTO the clipboard, but it doesn't work to check it the clipboard is empty. Perhaps it's not necessary, but doesn't really slow me down too much. :)

Russ

User avatar
mikeyww
Posts: 27370
Joined: 09 Sep 2014, 18:38

Re: get the selected text in any active window

Post by mikeyww » 12 Jan 2022, 07:55

If clipboard is empty, then ErrorLevel is set. ClipWait does the sleep for you. The benefit is that there is no minimum amount of time needed.

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: get the selected text in any active window

Post by surfactant » 12 Jan 2022, 08:07

@swagfag @RussF @mikeyww Thank you all for your comments!

Yes I currently use the solution based on clipboard. But as suggested in your comments, sometimes it seems to be tricky to get the clipboard work exactly expected. And I hoped that the clipboard would be kept unaffected by the script.

@swagfag "u can straight up abandon the idea of having one solution". Yes, let me forget it.

Post Reply

Return to “Ask for Help (v1)”