tweak formated text in clipboard? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

tweak formated text in clipboard?

Post by surfactant » 20 Mar 2023, 02:40

When I copy some formated text from a web page, for example some text in red, the source code should be something like:

Code: Select all

<span style="color: red;">TEXT</span>
Is it possible to tweak the formated text in the clipboard, e.g. replacing "red" with "green"? If yes, I would get a piece of green text when I paste it into Word.

Thank you!
Last edited by surfactant on 20 Mar 2023, 03:34, edited 1 time in total.

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: tweak formated text in clipboard?

Post by off » 20 Mar 2023, 03:19

Maybe using StrReplace()?
https://www.autohotkey.com/docs/v1/lib/StrReplace.htm

Code: Select all

^+v:: ; ctrl+shift+v after copying the formatted text
Clipboard:=StrReplace(Clipboard, "red", "green")
Send, %Clipboard%
Return
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: tweak formated text in clipboard?

Post by surfactant » 20 Mar 2023, 03:33

@off Thanks. The content of "Clipboard" is simply "red", but not the source code "<span style="color: red;">TEXT</span>".

(To make the question unambiguous, I modified the source code in the original post.)

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: tweak formated text in clipboard?

Post by surfactant » 21 Mar 2023, 07:08

Nobody has any idea? Thank you!

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: tweak formated text in clipboard?

Post by off » 21 Mar 2023, 07:42

Im stuck at getting the RTF into the Clipboard, Appending it to Clip.txt, and have no idea until now how to read the whole file as Clipboard again.. Since it has multiple line, even just "This is Red text" with red color in Microsoft Word has around 200+ lines containing the formats..

And reading the whole line, and pasting it will just send the whole format

I hope someone have more experiences with this
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: tweak formated text in clipboard?  Topic is solved

Post by mikeyww » 21 Mar 2023, 08:58

Extract HTML data: viewtopic.php?p=513257#p513257 by @teadrinker

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: tweak formated text in clipboard?

Post by surfactant » 21 Mar 2023, 09:16

@@off Thanks for your reply!

@mikeyww It looks great and should work. Thank you!

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: tweak formated text in clipboard?

Post by surfactant » 22 Mar 2023, 04:50

@mikeyww

I tried to come up with a better solution of pushing the modified content back to the clipboard, but failed, could you have a look and give some clues? Thank you!

https://www.autohotkey.com/docs/v1/lib/StrGet.htm
https://www.autohotkey.com/docs/v1/lib/StrPut.htm

Code: Select all

LWin::

  myHTML := Get_Clipboard_HTML()
  MsgBox, % myHTML

  myHTML := RegExReplace(myHTML, "AAA", "XXX")

  Put_Clipboard_HTML(myHTML)

  MsgBox, % Get_Clipboard_HTML()

return
;---------------------------------
Get_Clipboard_HTML() {
  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 )
          , html := StrGet( pData, dataL, "UTF-8" )
          , DllCall( "GlobalUnlock", UInt, hData )
  DllCall( "CloseClipboard" )

  Return html
}

Put_Clipboard_HTML(html) {
  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 )
          , StrPutVar( html, pData, "UTF-8" )
          , DllCall( "GlobalUnlock", UInt, hData )
  DllCall( "CloseClipboard" )
}

StrPutVar(string, ByRef var, encoding)
{
    ; Ensure capacity.
    VarSetCapacity( var, StrPut(string, encoding)
        ; StrPut returns char count, but VarSetCapacity needs bytes.
        * ((encoding="utf-16"||encoding="cp1200") ? 2 : 1) )
    ; Copy or convert the string.
    return StrPut(string, &var, encoding)
}

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: tweak formated text in clipboard?

Post by mikeyww » 22 Mar 2023, 05:41

It isn't my area of knowledge, so you'll need to call in the experts!

I use this one. viewtopic.php?p=513187#p513187

surfactant
Posts: 99
Joined: 28 Jun 2019, 01:07

Re: tweak formated text in clipboard?

Post by surfactant » 22 Mar 2023, 06:22

@mikeyww Thank you! The method you recommended would lose the url of the source web page.
Last edited by surfactant on 22 Mar 2023, 07:39, edited 2 times in total.

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: tweak formated text in clipboard?

Post by mikeyww » 22 Mar 2023, 06:46

Fair enough. Need some more serious coders here.

:eh:

Post Reply

Return to “Ask for Help (v1)”