Copy text from HTML Viewer

I’m trying to copy the text from the content of an HTML viewer. I found this discussion from 2017:

Anyone know if this code still works? I added the code and it compiled fine. I then used:


… but no new text was placed in the clipboard to paste.

Public Sub CopyHTML(Extends viewer As DesktopHTMLViewer)
  #if TargetCocoa
    Declare Sub copyHTML Lib "Cocoa" Selector "copy:" (id As Ptr, sender As Ptr)
    copyHTML viewer.Handle, viewer.Handle
End Sub

Thanks. Just tried that but got:

Error: Expected (Ptr, Ptr, Ptr), but these arguments are (Integer, Ptr, Integer).

I’m using Xojo 2021 r2.1 so still HTMLViewer

Running on Big Sur

API2 changed a lot of pointers to integers. Change those and you should be OK.

It compiles fine with integer instead of ptr, but doesn’t work. Just pastes what is previously (already) in the clipboard. My usage is:



This recent discussion should help you.

If you want all of the HTML or text from a page loaded in HTMLViewer, have a look at this one:

1 Like

Thanks. That does work to obtain a copy of all of the HTML from the HTMLViewer – which is also a handy thing for me to have.

However, what I’m primarily trying to do is to get the equivalent of placing a cursor in the HTMLViewer window, doing Edit>SelectAll, Edit>Copy

Several people, present and past, have suggested use of the “Declare Sub copyHTML Lib “Cocoa” Selector “copy:” (id As Integer, sender As Integer)” approach; however I have not been able to get that to work.

Perhaps someone has a small project file for which this works successfully? I have been trying to get this to work by making simple changes to the SimpleBrowser app that comes in the Examples folder of Xojo.

As noted above, I’m currently using Xojo 2021 r2.1 on Big Sur

In the first link I posted, you will find exactly that in the solution. That code is also platform agnostic and will work beyond just macOS.

That is very helpful. I previously tried your second post, but the first post works. It does indeed copy the content as I desire, so that if I go to an editor and do a paste, I get exactly the text I’m looking for. However I’m still having difficulty in getting a paste to work in either a TextArea or a TextField.

These do not work for me:

Nor does this:
var c as New Clipboard
var s as String
s= c.Text
TextArea1.text = s
TextField2.text = s

Is there a way to extract and only paste the text?
If I do a paste directly with command-v into the TextArea1 or TextField2 after executing the select all and copy it does indeed paste text I’m looking for.

The call to Copy initiates an asynchronous operation, so it’s likely that the rest of your function continues before the data is returned. It’s not really intended to be used synchronously due to the nature of the HTMLViewer, so you’d need to use a different method. Swap out the Copy method with this one:

Public Sub Copy(extends h as DesktopHTMLViewer)
  var result as String = h.ExecuteJavaScriptSync( "window.getSelection().toString();" )
  var c as new Clipboard
  c.Text = result
End Sub


Public Sub Copy(extends h as DesktopHTMLViewer)
  Call h.ExecuteJavaScriptSync( "document.execCommand('copy');" )
End Sub

I would recommend using the first one as the second uses a deprecated JavaScript function to set the clipboard text.

1 Like

Yep! This works great. Better yet, it allows me to get the text result into a variable so that I don’t even need to pass it through the clipboard.

Thanks for your persistence and expertise.

Just happy to help. Don’t forget to mark a solution so that it’s easier for others with the same needs in the future.

This code has worked just great for me on Mac. I’m now compiling for windows and it fails. Is there conditional code I need for running on Windows?

If you’re using the Desktop API 1.0 HTMLViewer with a Renderer property set to Native, that may be why. If there is a Renderer property, change it to WebKit.

Yep. That worked. Thanks