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
#endif
End Sub
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
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:
TextArea1.Paste
TextField2.paste
Nor does this:
var c as New Clipboard
var s as String
s= c.Text
c.close
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
c.Close
End Sub
Or:
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.
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.