Copy Html text, modify it (ex change color of text), paste it back in google docs

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Copy Html text, modify it (ex change color of text), paste it back in google docs

12 Dec 2017, 07:55

Hi!
I would like to have a keyboard shorcut that will copy to the clipboard a text I have selected (in word, in google docs, in an HTML editor), change the color of the text and then paste it so that it replaces the selected text.

I have seen this post http://www.autohotkey.com/forum/viewtop ... 624#392624, but when I use it, it pastes the html code not the formated text. I have also seen this post https://autohotkey.com/board/topic/7692 ... functions/ but I have some difficulties to put everything together.

Thanks for your help!
Last edited by partof on 13 Dec 2017, 04:33, edited 1 time in total.
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: Copy Html text, modify it (ex change color of text), paste it back

12 Dec 2017, 11:39

I am getting closer. I am using WinClip from https://autohotkey.com/board/topic/7467 ... pulations/

But there are 2 problems:
1)It works if I paste in an HTML editor but I get this annoying line at the beginning, which I can't remove

Code: Select all

 Version:0.9 StartHTML:0000000105 EndHTML:0000001002 StartFragment:0000000141 EndFragment:0000000966
I have commented the lines 525 to 530 in WinClip.ahk (in the method in _setHTML ) but without succes.

2) On google docs, what is pasted isn't the modified clipboard but the first one

Code: Select all

!d::
WinClip.Copy()
t := WinClip.GetHTML()
msgbox, % t
t := RegExReplace( t, "color","")
;t := RegExReplace( t, "color:.*`;","")
t := RegExReplace( t, "<!--StartFragment-->", "<!--StartFragment--><div style=""color:red"">")
t := RegExReplace( t, "<!--EndFragment-->", "</div><!--EndFragment-->")
msgbox, % t
WinClip.SetHTML( t )
return
Thanks for your help!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Copy Html text, modify it (ex change color of text), paste it back

12 Dec 2017, 11:46

- Did you try putting RTF on the clipboard, but *not* putting HTML on the clipboard, for use with the HTML editor? I don't know if that if that would work but it's worth a try.
- Maybe the HTML editor thinks that it serves you better by treating HTML on the clipboard literally. But then, can the HTML editor handle formatted text? Isn't any formatted text in IDEs usually put there by the editor itself?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: Copy Html text, modify it (ex change color of text), paste it back

12 Dec 2017, 11:59

Thanks jeeswg for your help! I have tried RTF with WinClip.GetRTF and WinClip.SetRTF but I don't know how I can modify the color of the text after that (I can't display the RTF text in a msgbox)

Sorry but I don't understand your second point.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Copy Html text, modify it (ex change color of text), paste it back

12 Dec 2017, 12:36

- For tips with RTF, open WordPad, get the text to look the way you want it (bold, colours etc), then save as an RTF file, and open that file with Notepad.
- What I meant was, can an HTML editor actually handle formatted text? When you edit code or html, it's plaintext, so it sort of, doesn't make sense that you can format text in an HTML editor. Or am I missing something. If anything was to format text in an editor/IDE, it would be the editor itself, i.e. the editor would set the colour of certain words, syntax highlighting.
- You can create IE controls / RichEdit controls to display HTML / RTF. E.g.
control zoo (AHK v1.1) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=30652
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Guest

Re: Copy Html text, modify it (ex change color of text), paste it back

12 Dec 2017, 14:41

My guess is that because you assign it to a plain text variable that "winclip" code, the first six lines, is now part of a your text variable t which you are modifying, then you are telling winclip, this text variable t with those six lines IS HTML so it treats it as HTML. If you remove the six lines from your text variable t before modifying it and setting it back as HTML it probably works (not tested, fingers crossed)

Code: Select all

!d::
WinClip.Copy()
t := WinClip.GetHTML()
t := SubStr(t,InStr(t,"`n",,1,6)) ; remove first six lines from the t variable
msgbox, % t
t := RegExReplace( t, "color","")
;t := RegExReplace( t, "color:.*`;","")
t := RegExReplace( t, "<!--StartFragment-->", "<!--StartFragment--><div style=""color:red"">")
t := RegExReplace( t, "<!--EndFragment-->", "</div><!--EndFragment-->")
msgbox, % t
WinClip.SetHTML( t )
return
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

Re: Copy Html text, modify it (ex change color of text), paste it back

13 Dec 2017, 04:33

Thanks so much for your help!
I am getting closer and closer.

Here is my current code but I still have a problem. The Html copied contains UTF-8 characters that are replaced with things like è or é. In GetHtml from WinClip.ahk, I tried to replaced CPO by UTF-16 but it sends back chinese characters! Any idea how I can solve this?

Code: Select all

WinClip.Copy()
t := WinClip.GetHTML()
msgbox, % t
t := SubStr(t,InStr(t,"`n",,1,6)) ; remove first six lines from the t variable
t := RegExReplace( t, "color:#\w{6}","color:#FF0000")
t := RegExReplace( t, "id=""[\w-]*""", "") ; for google docs
msgbox, % t

WinClip.SetHTML( t )
return
Guest

Re: Copy Html text, modify it (ex change color of text), paste it back in google docs

13 Dec 2017, 06:09

Sounds to me like you're running the ansi version of AutoHotkey or perhaps your script isn't saved as unicode (just a wild guess).
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Copy Html text, modify it (ex change color of text), paste it back in google docs

19 Dec 2017, 11:36

In case you do need to convert UTF-8 bytes to Unicode text:

Code: Select all

q:: ;UTF-8 bytes to Unicode text
vText := "√" ;square root symbol
vSize := StrPut(vText, "CP0")
VarSetCapacity(vTemp, vSize)
StrPut(vText, &vTemp, "CP0")
MsgBox, % StrGet(&vTemp, "UTF-8")
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, iamMG, Theda and 170 guests