SetClipboardHTML() for V1.1 & V2

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

SetClipboardHTML() for V1.1 & V2

06 Sep 2020, 05:15

SetClipboardHTML( HtmlBody[, HtmlHead, AltText] )
Sets CF_HTML data in clipboard which can then be pasted with Ctrl+v
Sample: SetClipboardHTML("<div><b>Welcome to AutoHotkey</b></div>",, "Welcome to AutoHotkey")

Parameters:
 
  • HtmlBody : Body of the HTML, without <body></body> tags. Paste will work only in html capable input.
  • HtmlHead : Optional Head content of the HTML, without <head></head> tags.
    You may use this for limited CSS styling with supported editors like MS Word.
    For browser input (Gmail/YahooMail etc.), you may use limited inline styling in Body.
  • AltText : Optional alternative (unicode) text for html incapable editors like Notepad, PSPad etc.
 
Example: These following snippets are a modified version of intro example given in docs under HotStrings V1 / Hotstrings V2.
 

Code: Select all

#Requires AutoHotkey v1.1.33+
#SingleInstance  Force
Hotstring(":*X:]d", "DatePaste")
Return

DatePaste:
  FormatTime, DTH,, '<i>'dddd'</i>', '<b>'dd-MMM-yyyy'</b>' h:mm tt
  FormatTime, DTT,, dddd, dd-MMM-yyyy h:mm tt
  SetClipboardHTML(DTH,, DTT)
  SendInput ^v
Return
 
 

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
Hotstring(":*X:]d", DatePaste)

DatePaste(*)
{
    Local  DTH  :=  FormatTime(,"'<i>'dddd'</i>', '<b>'dd-MMM-yyyy'</b>' h:mm tt")
        ,  DTT  :=  FormatTime(,"dddd, dd-MMM-yyyy h:mm tt")
    SetClipboardHTML(DTH,, DTT)
    SendInput("^v")
}
 
 
The function
 

Code: Select all

SetClipboardHTML(HtmlBody, HtmlHead := "", AltText := "")                ;  SetClipboardHTML() v0.72 by SKAN for ahk,ah2 on D393/D66T
{                                                                        ;                                 @ autohotkey.com/r?t=80706
    Static CF_UNICODETEXT  :=  13
         , CF_HTML         :=  DllCall("User32\RegisterClipboardFormat", "str","HTML Format")
         , Fix             :=  SubStr(A_AhkVersion, 1, 1) = 2 ? [,,1] : [,1]  ;           StrReplace() parameter fix for AHK V2 vs V1

    Local  pMem := 0,    Res1  := 1,    Bytes := 0,    LF  := "`n"
        ,  hMem := 0,    Res2  := 1,    Html  := ""

    If Not DllCall("User32\OpenClipboard", "ptr",A_ScriptHwnd)
           Return 0
    Else   DllCall("User32\EmptyClipboard")

    If  HtmlBody != ""
    {
        Html    :=  "Version:0.9" LF "StartHTML:000000000" LF "EndHTML:000000000" LF "StartFragment:000000000"
                 .  LF "EndFragment:000000000" LF "<!DOCTYPE>" LF "<html>" LF "<head>" LF HtmlHead LF "</head>" LF "<body>"
                 .  LF "<!--StartFragment -->" HtmlBody "<!--EndFragment -->" LF "</body>" LF "</html>"

     ,  Html    :=  StrReplace(Html, "StartHTML:000000000",     Format("StartHTML:{:09}",     InStr(Html, "<html>"))          , Fix*)
     ,  Html    :=  StrReplace(Html, "EndHTML:000000000",       Format("EndHTML:{:09}",       InStr(Html, "</html>"))         , Fix*)
     ,  Html    :=  StrReplace(Html, "StartFragment:000000000", Format("StartFragment:{:09}", InStr(Html, "<!--StartFrag"))   , Fix*)
     ,  Html    :=  StrReplace(Html, "EndFragment:000000000",   Format("EndFragment:{:09}",   InStr(Html, "<!--EndFragme"),,0), Fix*)

     ,  Bytes   :=  StrPut(Html, "utf-8")
     ,  hMem    :=  DllCall("Kernel32\GlobalAlloc", "int",0x42, "ptr",Bytes+4, "ptr")
     ,  pMem    :=  DllCall("Kernel32\GlobalLock", "ptr",hMem, "ptr")
     ,              StrPut(Html, pMem, Bytes, "utf-8")
     ,              DllCall("Kernel32\GlobalUnlock", "ptr",hMem)
     ,  Res1    :=  DllCall("User32\SetClipboardData", "int",CF_HTML, "ptr",hMem)
    }

    If  AltText != ""
    {
        Bytes   :=  StrPut(AltText, "utf-16")
     ,  hMem    :=  DllCall("Kernel32\GlobalAlloc", "int",0x42, "ptr",(Bytes*2)+8, "ptr")
     ,  pMem    :=  DllCall("Kernel32\GlobalLock", "ptr",hMem, "ptr")
     ,              StrPut(AltText, pMem, Bytes, "utf-16")
     ,              DllCall("Kernel32\GlobalUnlock", "ptr",hMem)
     ,  Res2    :=  DllCall("User32\SetClipboardData", "int",CF_UNICODETEXT, "ptr",hMem)
    }

    DllCall("User32\CloseClipboard")

Return !! (Res1 And Res2)
}
My Scripts and Functions: V1  V2
User avatar
Joe Glines
Posts: 771
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: SetClipboardHTML()

19 Jan 2021, 14:52

Thanks @SKAN! As someone who loves HotStrings, It's great to be able to have HTML format as a replacement! I'm going to mention it on today's AutoHotkey webinar as the script highlight! :)
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Joe Glines
Posts: 771
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: SetClipboardHTML()

02 Feb 2021, 21:57

@SKAN
Hi SKAN,
I really loved your script but I wanted to be able to save/restore the clipboard so I wrapped it with another function.
Unfortunately it isn't always setting the html content correctly. I tried playing with the sleeps on my end but am not sure what's causing the issue. At 6:15 I show an example of it "breaking".

If you have any thoughts, I'd love to hear them... Again, thank you for your code!

Code: Select all


::ta.::
HTMLHotstring("<a href='https://the-automator.com'>the-<span style='color:red'>Automator</span></a>",HTMLHead,"the-Automator.com",1)
return

HTMLHotstring(HTMLBody,HTMLHead,AltText,RestoreClipboard:=1){
	if (RestoreClipboard)
		ClipBackup:=ClipboardAll ;backup clipboard
	Clipboard:="" ;Blank the clipboard so can use ClipWait later
	SetClipboardHTML(HTMLBody,HTMLHead,AltText) ;Call function
	ClipWait,2 ;Wait up to 2 seconds for clipboard to have something.
	if ErrorLevel 	{ ;If nothing gets put onto clipboard
		MsgBox, Nothing was put in the clipboard`n`nRestoring original content & exiting ;explain something went wrong
		Clipboard:=ClipBackup ;restore clipboard to original content		
		return False
	}
	SendInput, ^v ;Send Control+V / Paste 
	if (RestoreClipboard){
		sleep, 100 ;needed to make sure pasted happens before restoring clipboard
		Clipboard:=ClipBackup ;restore clipboard to original content	
	}
	Return True
}

Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
rommmcek
Posts: 1479
Joined: 15 Aug 2014, 15:18

Re: SetClipboardHTML()

06 Feb 2021, 20:39

This works for me:

Code: Select all

DatePaste:
  ClipBackup:= ClipboardAll
  FormatTime, DTH,, '<div><i>'dddd'</i>', '<b>'dd-MMM-yyyy'</b>' h:mm tt'</div>'
  FormatTime, DTT,, dddd, dd-MMM-yyyy h:mm tt
  SetClipboardHTML(DTH,, DTT)
  SendInput ^v
  Sleep, 150
  while DllCall("user32\GetOpenClipboardWindow", "Ptr")
     Sleep, 150
  Clipboard:= ClipBackUp
Return
[Edit]: Putting "Wait for Clipboard" before Paste too, increases reliability further more.
[Edit2]: Not needed anymore due to fix by SKAN (thanks to report by tdalon).
Last edited by rommmcek on 10 Feb 2021, 17:15, edited 2 times in total.
User avatar
Joe Glines
Posts: 771
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: SetClipboardHTML()

08 Feb 2021, 11:25

@rommmcekThanks! I'll play with it some more later this week.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
tdalon
Posts: 44
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

Re: SetClipboardHTML()

09 Feb 2021, 01:32

This is amazing. Thanks @skan.
Also the hint by @rommmcek looks like I will get rid of these timing issue.
I was using WinClip https tdalon.blogspot.com /2020/02/autohotkey-clipboard-handling.html (Broken Link for safety) in the past but it has some issues.
Could you also provide a GetClipboardHTML function?

You can find my Clip library wrapping these functions here https://github.com/tdalon/ahk/blob/master/Lib/Clip.ahk.

Simple example of Hotstring using it:

Code: Select all

::teamsme:: ; Teams Me Chat Link (rtf)
sText = Chat with me in Teams
sLink :=  "https://teams.microsoft.com/l/chat/0/0?users="  
Clip_PasteHtml(sLink,sText,True)	
return
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 09 Feb 2021, 01:45, edited 1 time in total.
Reason: Personal email address removed from public view.
User avatar
tdalon
Posts: 44
Joined: 21 Apr 2017, 07:19
Location: Germany
Contact:

Re: SetClipboardHTML()

10 Feb 2021, 14:24

@SKAN
If you run the function and immediately access the clipboard e.g. sClip := Clipboard
AutoHotkey will crash on Error: GlobalLock.
It seems the function does not complete with the clipboard being accessible. :(
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetClipboardHTML()

10 Feb 2021, 16:38

tdalon wrote:
10 Feb 2021, 14:24
@SKAN
If you run the function and immediately access the clipboard e.g. sClip := Clipboard
AutoHotkey will crash on Error: GlobalLock.
It seems the function does not complete with the clipboard being accessible. :(
Thanks for reporting this. 👍

Allocated (Global) memory for CF_UNICODETEXT shouldn't be freed by the function and let the OS do it. Ref: MSDN
While this doesn't seem to be a problem with AHK-ANSI, Unicode version crashes.
:arrow: This has been fixed in v0.67
User avatar
rommmcek
Posts: 1479
Joined: 15 Aug 2014, 15:18

Re: SetClipboardHTML()

10 Feb 2021, 17:12

That's why I proposed "Wait for Clipboard" before paste too, now redundant! Thanks to both of you!
valuex
Posts: 86
Joined: 01 Nov 2014, 08:17

Re: SetClipboardHTML()

13 Feb 2021, 08:01

@SKAN
It seems not going well with base64 image. Here is my case.
Any comment will be appreciated.

Code: Select all

   html=
    (       
        <img src="data:image/jpeg;charset=utf-8;base64, %base64string%">
        <p> description about the image</p>           
    )
    SetClipboardHTML( html )    
while the base64string is as following,
/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkS
Ew8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJ
CQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy
MjIyMjIyMjIyMjIyMjL/wAARCAAvAIYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEA
AAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIh
MUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6
Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZ
mqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx
8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA
AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAV
YnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hp
anN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPE
xcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDx
2iiigAooooAKKKKACiiigAooooAKKKnms7q2hhmntpoopxuid4yokHqpPUfSgLkF
FFFABRRRQAUUUUAdF4IttLu/EsUWreUYSjGNJn2o8n8KsfQ1peLJbyC0ez1Pwjp+
myFwYLqzh8se43DIfI9653RYNJubxot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lrqkl9YzW5ie6jtjG0ZbuEfOcEChRTlHnfTv6+ZF2ufkX4feVdR8KWSaR
c6jo2uw6pHaEC5QQNEyAnAIBJ3DPeuXr0XxH4ma50S7hh8cnURIAv2M6SIi6kjI3
7RjA5/CvOqzqJKXu/wBfma0nJx97+vwQUUUVBqFFFFABRRRQAUUUUAFFFFABRRRQ
AUUUUAFFFFABRRRQAUUUUAf/2Q==
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetClipboardHTML()

13 Feb 2021, 09:31

@valuex

The following works fine for me:
 

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

base64string := "
( Join
/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkS
Ew8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJ
CQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIy
MjIyMjIyMjIyMjIyMjL/wAARCAAvAIYDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEA
AAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIh
MUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6
Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZ
mqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx
8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA
AgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAV
YnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2h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1hoZZEDi
yhGxULH5yqscnr3zwKseMIJYdI0Iprx1XTzHItqTa+T5aqVUjnk/j6VzVhDbXF9D
FeXf2S3Y4efyzJsHrtHJrsdaXwvc+GdPsrfxR5k+mxTeWv8AZ8o89nbcBzwvpnmp
VnTl69/6/I1fuziv0/r8x3w91zUfOuNG+0f8S/7LPJ5OxfvbeucZ/WuV0LRp9e1N
bKGSOIbTJJLIcLGg6sfpXSeC38O6W39o3/iDyLmSCWFrX7FI2zcCAd4yDxzUegar
pXhHxZ5lrqkl9YzW5ie6jtjG0ZbuEfOcEChRTlHnfTv6+ZF2ufkX4feVdR8KWSaR
c6jo2uw6pHaEC5QQNEyAnAIBJ3DPeuXr0XxH4ma50S7hh8cnURIAv2M6SIi6kjI3
7RjA5/CvOqzqJKXu/wBfma0nJx97+vwQUUUVBqFFFFABRRRQAUUUUAFFFFABRRRQ
AUUUUAFFFFABRRRQAUUUUAf/2Q==
)"

html := "
( LTrim
  <img src=""data:image/jpeg;charset=utf-8;base64," . base64string . """>
  <p> description about the image</p>
)"

SetClipboardHTML( html )
william_ahk
Posts: 501
Joined: 03 Dec 2018, 20:02

Re: SetClipboardHTML()

25 Jan 2023, 07:24

Hey @SKAN, when I use this function it always pastes an extra space in the end, and I see in your examples you were countering this problem by wrapping the HTML content in a <div>. However when I manually copy HTML content from the browser there's no div wrapping the content. Then I compared the two, and noticed that there's no line break between the start and end fragment comments in the browser's copy. I removed these line breaks in the function and confirmed that they were causing the extra space.
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: SetClipboardHTML()

01 Mar 2023, 08:06

This function was generated by ChatGPT. It looks very simple and works for me. Who's version is better?

Code: Select all


html := "<html><body><h1>Hello World!</h1></body></html>"
ChatGPT_SetHtmlToClipboard(html)


ChatGPT_SetHtmlToClipboard(html) {
    htmlFile := ComObjCreate("HTMLfile")
    htmlFile.write(html)
    bodyRange := htmlFile.body.createTextRange()
    bodyRange.select()
    bodyRange.execCommand("Copy")
    bodyRange := ""
    htmlFile := ""
}
william_ahk
Posts: 501
Joined: 03 Dec 2018, 20:02

Re: SetClipboardHTML()

02 Mar 2023, 07:18

oldbrother wrote:
01 Mar 2023, 08:06
This function was generated by ChatGPT. It looks very simple and works for me. Who's version is better?

Code: Select all


html := "<html><body><h1>Hello World!</h1></body></html>"
ChatGPT_SetHtmlToClipboard(html)


ChatGPT_SetHtmlToClipboard(html) {
    htmlFile := ComObjCreate("HTMLfile")
    htmlFile.write(html)
    bodyRange := htmlFile.body.createTextRange()
    bodyRange.select()
    bodyRange.execCommand("Copy")
    bodyRange := ""
    htmlFile := ""
}
This whole function was generated by ChatGPT, unchanged? Very impressive.

SKAN's version is certainly much faster. But the IE version sets the plain text in addition, which would have to be parsed with an HTML parser otherwise if one wants to set both HTML and plain text automatically. So, this might actually be a better implementation! :trollface:
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: SetClipboardHTML()

02 Mar 2023, 21:58

william_ahk wrote:
02 Mar 2023, 07:18
This whole function was generated by ChatGPT, unchanged? Very impressive.
Yes, I only removed the comments. Thank you!
dox
Posts: 16
Joined: 03 Apr 2017, 12:23

Re: SetClipboardHTML()

10 Mar 2023, 14:39

A question please, if knowledgeable users wouldn't mind:

If I have:

Code: Select all

String := "||Title||: This is some text ..."
How would I go about using SetClipboardHTML() or alternative to just set the ||Title|| substring to HTML bold?

Using Skan's function, I could paste the entire clipboard string to HTML-aware windows such as in a manner below:

SetClipboardHTML("<span style='font-family:Verdana; color:#000000; font-size:12pt; font-weight:bold; font-style:italic'>This is some bold italic text in 12pt Verdana</span>",,"")

I wish to know how I might modify the function to selectively style a portion of the text in the clipboard with HTML tags.

Many thanks in advance!
dox
Posts: 16
Joined: 03 Apr 2017, 12:23

Re: SetClipboardHTML()

10 Mar 2023, 17:08

Now of course, I could update HTMLBody to something like:

Code: Select all

SetClipboardHTML("<span style='font-family:Verdana; color:#000000; font-size:10pt; font-weight:bold; font-style:italic'>Title in bold italic 10pt Verdana:</span> <span style='font-family:Georgia; color:#000000; font-size:12pt'>Text in regular 12pt Georgia</span>",,"")
In MS Word, it would display:

image.png
image.png (5.41 KiB) Viewed 3297 times

However, what I would like is a way to parse any arbitrary string/text in the clipboard through the likes of RegExReplace() or StrReplace(), so as to be able to selectively apply HTML formatting tags to "needles", if possible. Thanks.
dox
Posts: 16
Joined: 03 Apr 2017, 12:23

Re: SetClipboardHTML()

10 Mar 2023, 19:17

Quick follow-up: I was mistaken in thinking that something needs to be done inside SKAN's function. It turns out that it is quite easy, after reading in the file, to wrap necessary HTML tags around the elements I want to format with RegExReplace. Then StrReplace the "`r`n" with "<br />".

Good to go. Tested both in MS Word and on my HTML-aware web app I have to use for work.

BTW, the ChatGPT routine sure is short and sweet. But doesn't quite work right ... 98% there. But not all the way on the web app I have to use.

Thanks SKAN for introducing your function!
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: SetClipboardHTML()

17 Mar 2023, 16:32

I used ChatGPT revised the function. It is supposed to be more reliable.

Code: Select all

ChatGPT_SetHtmlToClipboard(html) {
	htmlDoc := ComObjCreate("Htmlfile")
	htmlDoc.write(html)
	body := htmlDoc.getElementsByTagName("body")[0]
	range := htmlDoc.body.createTextRange()
	range.moveToElementText(body)
	range.select()
	range.execCommand("Copy")
	htmlDoc := ""
	Range := ""
	Body := ""
}
User avatar
JoeSchmoe
Posts: 129
Joined: 08 Dec 2014, 08:58

Re: SetClipboardHTML()

20 Mar 2023, 10:21

Hi @SKAN, this looks great. Would you consider making an AHK 2 version? I'm looking for a fast and robust function that does what this does.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 102 guests