Copy highlighted text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Copy highlighted text

16 Aug 2019, 13:27

Is there a way to copy highlighted text?

This is in a browser. A solution for just IE (Com Interface?) would do just fine. However i am looking for a general solution.

1. Click find.
2. Enter a string (mine is quite unique so if found it finds only one case)
3: Send and the found string is highlighted.
3. Copy the highlighted text.

Plan B if highlighted text cannot be copied: If "find" function finds the word, is there a return value that can be read? (Much like Errorlevel?)

Thanks

PS: I thought highlighted text as a result of a"Find" search was the same as highlighted text marked by hand with the mouse. Apparently not.
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy highlighted text

16 Aug 2019, 15:42

Hi,
just a question: Why do you want to copy the text that got highlighted as a result of a search for text that you entered?
Wouldn't that just be the same text? :think:
:eh: :think:
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Copy highlighted text

17 Aug 2019, 01:19

Sir Teddy the First wrote:
16 Aug 2019, 15:42
Hi,
just a question: Why do you want to copy the text that got highlighted as a result of a search for text that you entered?
Wouldn't that just be the same text? :think:
NO. If the search is case-insensitive the returned text could be with a different case than the searched text.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy highlighted text

17 Aug 2019, 02:26

So, couldn't you just check in the Source-Code of the webpage if the text with your preferred case is present?
I'm sorry if I'm asking dumb questions, I just try to understand your problem :think:
:eh: :think:
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 02:38

Do the following (just an example):

1. open IE
2. navigate to https://www.roboform.com/filling-test-all-fields
3. press Ctrl+f
4. type 'Middle initial'

The text is highlighted in the page. I want to copy that text. Preferably with Ctrl+c but it doesn't work.

Neither the mouse arrow or the marker are at the text.
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy highlighted text

17 Aug 2019, 02:42

So,
I now understand what you want to do, but I don't understand why?

Code: Select all

StringUpper, OutputVar, InputVar , T
would do the same thing. And you have to know what text you want to search for, why don't you just capitalize that?
:eh: :think:
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 02:43

Sir Teddy the First wrote:
16 Aug 2019, 15:42
Hi,
just a question: Why do you want to copy the text that got highlighted as a result of a search for text that you entered?
Wouldn't that just be the same text? :think:
Good point. I was unclear.

I search for a text that may or may not be in the page. If it worked to copy with Ctrl+c (which it does not) then an empty Clipboard would indicate that the string wasn't found.
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 02:45

Sir Teddy the First wrote:
17 Aug 2019, 02:42
So,
I now understand what you want to do, but I don't understand why?

Code: Select all

StringUpper, OutputVar, InputVar , T
would do the same thing. And you have to know what text you want to search for, why don't you just capitalize that?
Actually it doesn't matter that is is lower or upper case. I just want to check if the text exists in the page or not.

If you repeat by hand the process i described above the string will be highlighted. Do Ctrl+c by hand and paste in Notepad. The string has not been copied. :crazy:
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy highlighted text

17 Aug 2019, 02:48

Hi,
just another suggestion, if you don't want to do it that way, we're going your way.
I just checked that, if you operate via COM, every name of those fields is contained in the HTML-class named "rfcontent clearfix".
So if you read this class into a variable, you can always check with "InStr" (even with case-sensitive search turned on) if it is present.

If we're talking not only about this page, but in gerneral, you would just have to get the source code via "Ctrl+U" and search that.

Is that an option?
:eh: :think:
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 02:58

Sir Teddy the First wrote:
17 Aug 2019, 02:48
Hi,
just another suggestion, if you don't want to do it that way, we're going your way.
I just checked that, if you operate via COM, every name of those fields is contained in the HTML-class named "rfcontent clearfix".
So if you read this class into a variable, you can always check with "InStr" (even with case-sensitive search turned on) if it is present.

If we're talking not only about this page, but in gerneral, you would just have to get the source code via "Ctrl+U" and search that.

Is that an option?
Thanks. It is a good alternative option.

I had solved it temporarily with WinGetText storing the returned value in a variable and searching in it.

Still I would like to have the easiest solution which is to copy with Ctrl+c the highlighted text. The string I am searching for is so specific that I know if it exists, it will be found only once.

Funny enough after Ctrl+u I do a Ctrl+f in the new page with the search string which is highlighted and as in the first case it CAN NOT be copied with Ctrl+c :shock:
Last edited by aircooled on 17 Aug 2019, 03:08, edited 2 times in total.
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 03:02

Odlanir wrote:
17 Aug 2019, 01:19
Sir Teddy the First wrote:
16 Aug 2019, 15:42
Hi,
just a question: Why do you want to copy the text that got highlighted as a result of a search for text that you entered?
Wouldn't that just be the same text? :think:
NO. If the search is case-insensitive the returned text could be with a different case than the searched text.
Correct. And the case -upper or lower- is totally irrelevant in my scenario. I just need to check whether the string exists or not.
Last edited by aircooled on 17 Aug 2019, 03:07, edited 1 time in total.
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 03:05

As an alternative plan:

If Ctrl+f finds a string. Is there any returned system value (like Errorlevel?) that indicates FOUND or NOT FOUND? That would be good enough for me.
User avatar
Sir Teddy the First
Posts: 94
Joined: 05 Aug 2019, 12:31
Contact:

Re: Copy highlighted text

17 Aug 2019, 03:26

Hi,
I just tested something:

If you use your method and search for the text and it is highlighted and you are sure that it appears only once, what I did was the following:

I searched for it via Ctrl+F (it got highlighted), I pressed "Next" (or Shorcut "Enter") which did not really do anything because it appears only once, but afterwards I was able to copy it with Ctrl+C, sometimes I had to press that twice, but that is something that AHK can do as well, i.e. "if the Clipboard is still empty -> Copy again" or something like that.

But it actually worked the way you wanted it to.
:eh: :think:
aircooled
Posts: 80
Joined: 01 Dec 2018, 08:51

Re: Copy highlighted text

17 Aug 2019, 03:58

Sir Teddy the First wrote:
17 Aug 2019, 03:26
Hi,
I just tested something:

If you use your method and search for the text and it is highlighted and you are sure that it appears only once, what I did was the following:

I searched for it via Ctrl+F (it got highlighted), I pressed "Next" (or Shorcut "Enter") which did not really do anything because it appears only once, but afterwards I was able to copy it with Ctrl+C, sometimes I had to press that twice, but that is something that AHK can do as well, i.e. "if the Clipboard is still empty -> Copy again" or something like that.

But it actually worked the way you wanted it to.
Brilliant! It did the job and brought tears to my eyes...

And allows me to implement the simplest solution that's always the best.

Thank you! :superhappy:
teadrinker
Posts: 4344
Joined: 29 Mar 2015, 09:41
Contact:

Re: Copy highlighted text

17 Aug 2019, 06:36

@aircooled, also you could find, select and copy some text in IE by this way:

Code: Select all

url := "https://www.roboform.com/filling-test-all-fields"
text := "middle initial"

if !wb := GetIePagePwbByUrl(url) {
   wb := ComObjCreate("InternetExplorer.Application")
   wb.Visible := true
   wb.Navigate(url)
   while (wb.Busy || wb.ReadyState != 4)
      Sleep 10
}

if !SelectTextInIE(wb, text)
   MsgBox, Text not found
else
   Clipboard := GetSelected(wb)

SelectTextInIE(wb, text) {
   rng := wb.document.body.createTextRange()
   if !rng.findText(text)
      Return
   rng.select()
   Return true
}

GetSelected(wb) {
   static IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}"
        , VT_DISPATCH := 9, F_OWNVALUE := 1
   pWin := ComObjQuery(wb, IID_IHTMLWindow2, IID_IHTMLWindow2)
   window := ComObject(VT_DISPATCH, pWin, F_OWNVALUE)
   res := window.eval("window.getSelection().toString()")
   ObjRelease(pWin)
   Return res
}

GetIePagePwbByUrl(url) {
   for window in ComObjCreate("Shell.Application").Windows
      if InStr(window.FullName, "iexplore.exe") && RegExMatch(url . "/", "i)^\Q" . window.LocationURL . "\E/*$")
         Return window
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 240 guests