Getting the HTML format from clipboard doesn't work when the function is run from this same script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by TheBeginner » 31 Dec 2022, 09:41

I'm getting a strange behavior with this code, when I run s:: it assigns the HTML format to the clipboard (I can see the change with inspectclipboard tool)
but when it tries to retrieve the HTML format from the clueboard ClipboardGet_HTML it returns as if there's no HTML format in the clipboard.
the stranger thing is when I ran only the ClipboardGet_HTML in its own ahk file it works (retrieves the HTML format that is still in the clipboard)

any idea why?

Code: Select all

s::
    a := "<b>test 123 test</b>"
    SetClipboardHTML(a)
    sleep 2000
    If ClipboardGet_HTML( DataInClip )
        MsgBox, % DataInClip
    Else MsgBox, no HTML format?
        Return

    SetClipboardHTML(HtmlBody, SourceURL="", AltText:="") { 
        Local F, Html, pMem, Bytes, hMemHTM:=0, hMemTXT:=0, Res1:=1, Res2:=1 
        Static CF_UNICODETEXT:=13, CFID:=DllCall("RegisterClipboardFormat", Str ,"HTML Format", UInt)

        If ! DllCall("OpenClipboard", "Ptr",A_ScriptHwnd)
            Return 0
        Else DllCall("EmptyClipboard")

        If (HtmlBody != "")
        {

            Html := "Version:0.9`r`nStartHTML:00000000`r`nEndHTML:00000000`r`nStartFragment"
            . ":00000000`r`nEndFragment:00000000" . SourceURL . "`r`n<html>`r`n<body>`r`n<!--StartFragment--><meta charset=""utf-8"">"
            . HtmlBody . "<!--EndFragment-->`r`n</body>`r`n</html>"

            Bytes := StrPut(Html, "utf-8")
            hMemHTM := DllCall("GlobalAlloc", "Int",0x42, "Ptr",Bytes+4, "Ptr")
            pMem := DllCall("GlobalLock", "Ptr",hMemHTM, "Ptr")
            StrPut(Html, pMem, Bytes, "utf-8")

            RegExMatch(Html, "mP)(*UCP)\X*(?=<html>)" , StartHTML)
            formattedString := Format("StartHTML:{:08d}", StartHTML)
            Html := StrReplace(Html, "StartHTML:00000000", formattedString)
            StrPut(Format("{:08}", StartHTML), pMem+23, 8, "utf-8")

            EndHTML := StrLen(Html)
            formattedString := Format("EndHTML:{:08d}", EndHTML)
            Html := StrReplace(Html, "EndHTML:00000000", formattedString)
            StrPut(Format("{:08}", EndHTML), pMem+41, 8, "utf-8")

            RegExMatch(Html, "mP)(*UCP)\X*<!--StartFragment-->" , StartFragment)
            formattedString := Format("StartFragment:{:08d}", StartFragment)
            Html := StrReplace(Html, "StartFragment:00000000", formattedString)
            StrPut(Format("{:08}", StartFragment), pMem+65, 8, "utf-8")

            RegExMatch(Html, "mP)(*UCP)\X*(?=<!--EndFragment-->)" , EndFragment)
            formattedString := Format("EndFragment:{:08d}", EndFragment)
            Html := StrReplace(Html, "EndFragment:00000000", formattedString)
            StrPut(Format("{:08}", EndFragment), pMem+87, 8, "utf-8")
            FileDelete, 111.html
            FileAppend, %Html%, 111.html,UTF-8
            ;Msgbox, % Html

            DllCall("GlobalUnlock", "Ptr",hMemHTM)
            Res1 := DllCall("SetClipboardData", "Int",CFID, "Ptr",hMemHTM)
            ;Msgbox, % Html

        }

        If (AltText != "")
        {
            Bytes := StrPut(AltText, "utf-16")
            hMemTXT := DllCall("GlobalAlloc", "Int",0x42, "Ptr",(Bytes*2)+8, "Ptr")
            pMem := DllCall("GlobalLock", "Ptr",hMemTXT, "Ptr")
            StrPut(AltText, pMem, Bytes, "utf-16")
            DllCall("GlobalUnlock", "Ptr",hMemTXT)
            Res2 := DllCall("SetClipboardData", "Int",CF_UNICODETEXT, "Ptr",hMemTXT)
        }

        DllCall("CloseClipboard")
        if (hMemHTM != 0) {
            DllCall("GlobalFree", "Ptr",hMemHTM)
            hMemHTM = 0
        }

        Return (Res1 & Res2)

    }

    ClipboardGet_HTML( byref DataInClip ) { ; www.autohotkey.com/forum/viewtopic.php?p=392624#392624
        If CBID := DllCall( "RegisterClipboardFormat", Str,"HTML Format", UInt )
            If DllCall( "IsClipboardFormatAvailable", UInt,CBID ) <> 0
            If DllCall( "OpenClipboard", UInt,0 ) <> 0
            If hData := DllCall( "GetClipboardData", UInt,CBID, UInt )
            DataL := DllCall( "GlobalSize", UInt,hData, UInt )
        , pData := DllCall( "GlobalLock", UInt,hData, UInt )
        , DataInClip := StrGet( pData, dataL, "UTF-8" )
        , DllCall( "GlobalUnlock", UInt,hData )
        DllCall( "CloseClipboard" )
        Return dataL ? dataL : 0
    }

esc::
ExitApp,
return

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by wetware05 » 31 Dec 2022, 10:13

Hi, TheBeginner

That's not what he's doing.The script is short of some code and two functions SetClipboardHTML() and ClipboardGet_HTML() . The code does not read the clipboard, but reads the content that is in the variable a a := "<b>test 123 test</b>" and the first function converts it to html (or tries to, because in my case does not happen: it creates a pseudo-code Html). The second function checks if the content of the variable is html, but they contain a variable that is not declared DataInClip (it seems to me, let the experts say so).

What do you want or try to do?, by default ClipboardAll saves the content in html.

Whatever clipboard you have on the system reads its own information, which is the clipboard, not what this script is doing.

just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by just me » 31 Dec 2022, 10:16

Code: Select all

        DllCall("CloseClipboard")
        if (hMemHTM != 0) {
            DllCall("GlobalFree", "Ptr",hMemHTM)
            hMemHTM = 0
        }
:arrow: SetClipboardData
If SetClipboardData succeeds, the system owns the object identified by the hMem parameter. The application may not write to or free the data once ownership has been transferred to the system, but it can lock and read from the data until the CloseClipboard function is called. (The memory must be unlocked before the Clipboard is closed.) If the hMem parameter identifies a memory object, the object must have been allocated using the function with the GMEM_MOVEABLE flag.
?

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by wetware05 » 31 Dec 2022, 10:32

hi, just me. :wave:

I don't know who you are addressing, nor if what you are asking "TheBeginner" is to add that code that you have posted. The whole script is wrong. Download and run it (although I think just reading it is enough).

just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script  Topic is solved

Post by just me » 31 Dec 2022, 10:48

Hi @wetware05,
wetware05 wrote:Download and run it ...
I just did it. It seems to do what it should if you remove

Code: Select all

        if (hMemHTM != 0) {
            DllCall("GlobalFree", "Ptr",hMemHTM)
            hMemHTM = 0
        }
from SetClipboardHTML().

Happy new year!

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by wetware05 » 31 Dec 2022, 11:06

just me wrote:
31 Dec 2022, 10:48
Hi @wetware05,
wetware05 wrote:Download and run it ...
I just did it. It seems to do what it should if you remove

Code: Select all

        if (hMemHTM != 0) {
            DllCall("GlobalFree", "Ptr",hMemHTM)
            hMemHTM = 0
        }
from SetClipboardHTML().

Happy new year!
If this code is removed, it gives an error in the function returns. It's not my battle, I let it be. Let TheBeginner speak.

Happy new year! :angel:

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by TheBeginner » 31 Dec 2022, 11:24

I see what happened, once I freed the memory the same process couldn't access it anymore? is that what you're saying @just me ?
that means I'll need to free the clipboard after all processing is completed, yes?

when I remove it it does seem to work 👍👍


@wetware05 I didn't encounter any errors when running it, did you get an error when you tried it?

just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by just me » 31 Dec 2022, 11:37

@TheBeginner,

just must not free the HGLOBAL handle after you passed it to the clipboard. You just need to unlock it before you close the clipboard, if necessary.

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by TheBeginner » 31 Dec 2022, 13:32

I think it's there It's part of the process (DllCall( "GlobalUnlock", UInt,hData )) 👍

but there is another strange thing I don't know why its happening, I get the clipboard HTML format change change it and then set it back to the clipboard, but when
I try to paste the content in google Docs in particular, it pastes whatever was previously and the clipboard before the change

but if I use the clipboard command to change the clipboard, the change to the clipboard does get detected and the updated clipboard is pasted

example of what I mean (when using this the updated clipboard will be pasted)

Code: Select all

w::
    Clipboard := ""
    send ^c
    ClipWait, 
    test := Clipboard 
    test := test . " 123"
    Clipboard  := test
sleep 50
    send ^v
Return
even tried to tell top level windows that the clipboard has changed but it made no difference, but when I use the function SetClipboardHTML() to set a new clipboard for some reason it's not detected
@just me do you know why one would work and the other will fail?

Code: Select all

PostMessage, 0x031D, 0, 0, , A

just me
Posts: 9576
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by just me » 01 Jan 2023, 05:17

TheBeginner wrote: I try to paste the content in google Docs in particular, it pastes whatever was previously and the clipboard before the change
but if I use the clipboard command to change the clipboard, the change to the clipboard does get detected and the updated clipboard is pasted
Your sample adds only TEXT formats to the clipboard, no HTML. So how do you get and change the HTML format contained in the clipboard and how do you store it back?

TheBeginner
Posts: 91
Joined: 19 Oct 2018, 12:05

Re: Getting the HTML format from clipboard doesn't work when the function is run from this same script

Post by TheBeginner » 02 Jan 2023, 03:46

First thank you both for your answers

it helped me understand better what what is happening in solve the issue, regarding the second issue it was unrelated to the code
the code works fine 🔥👍🔥

Post Reply

Return to “Ask for Help (v1)”