Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active April 26, 2021 15:38
Show Gist options
  • Save JoeGlines/3c3643c535a85aff7d037d083482bd02 to your computer and use it in GitHub Desktop.
Save JoeGlines/3c3643c535a85aff7d037d083482bd02 to your computer and use it in GitHub Desktop.
Creating a Hotstring which has HTML encoding
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
#SingleInstance,Force
;**************************************
::ta.::
HTMLHotstring("<a href='https://the-automator.com'>the-<span style='color:red'>Automator</span></a>",HTMLHead,"the-Automator.com",1)
return
;********************HTML Hotstring***********************************
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
}
/*
v0.66 by SKAN on D393/D396 https://www.autohotkey.com/boards/viewtopic.php?f=6&t=80706
• 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.
*/
SetClipboardHTML(HtmlBody, HtmlHead:="", AltText:="") {
Local F, Html, pMem, Bytes, hMemHTM:=0, hMemTXT:=0, Res1:=1, Res2:=1 ; @ tiny.cc/t80706
Static CF_UNICODETEXT:=13, CFID:=DllCall("RegisterClipboardFormat", "Str","HTML Format")
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`r`n<!DOCTYPE>`r`n<html>`r`n<head>`r`n"
. HtmlHead . "`r`n</head>`r`n<body>`r`n<!--StartFragment -->`r`n"
. HtmlBody . "`r`n<!--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")
F := DllCall("Shlwapi.dll\StrStrA", "Ptr",pMem, "AStr","<html>", "Ptr") - pMem
StrPut(Format("{:08}", F), pMem+23, 8, "utf-8")
F := DllCall("Shlwapi.dll\StrStrA", "Ptr",pMem, "AStr","</html>", "Ptr") - pMem
StrPut(Format("{:08}", F), pMem+41, 8, "utf-8")
F := DllCall("Shlwapi.dll\StrStrA", "Ptr",pMem, "AStr","<!--StartFra", "Ptr") - pMem
StrPut(Format("{:08}", F), pMem+65, 8, "utf-8")
F := DllCall("Shlwapi.dll\StrStrA", "Ptr",pMem, "AStr","<!--EndFragm", "Ptr") - pMem
StrPut(Format("{:08}", F), pMem+87, 8, "utf-8")
DllCall("GlobalUnlock", "Ptr",hMemHTM)
Res1 := DllCall("SetClipboardData", "Int",CFID, "Ptr",hMemHTM)
}
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",hMemHTM)
Res2 := DllCall("SetClipboardData", "Int",CF_UNICODETEXT, "Ptr",hMemTXT)
}
DllCall("CloseClipboard")
hMemHTM := hMemHTM ? DllCall("GlobalFree", "Ptr",hMemHTM) : 0
hMemTXT := hMemTXT ? DllCall("GlobalFree", "Ptr",hMemTXT) : 0
Return (Res1 & Res2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment