Is it possible to stringreplace rtf to clipboard? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
itsfloody
Posts: 4
Joined: 18 Aug 2016, 22:06

Is it possible to stringreplace rtf to clipboard?

07 Mar 2018, 16:54

Hi everyone,
I have a blob of formatted text ,for example:
Car Survey Report

Registration []
Make: []
Model: []
Year: []
Engine: []cc

I want to be able to copy that text to the clipboard,
Read that text line by line and manipulate/replace strings ([] if present),
Then paste the entire clipboard whilst still retaining formatting.

Car Survey Report

Registration EXM357Y
Make:
Model:
Year: 1994
Engine: 3000cc

Is that possible? Many thanks in advance!! Here's my code so far but all I get is plain text

Code: Select all

F8::
clipboard =
Txtline2 =
blank = 0
sleep 200
send ^a^c{Home}						;copies all text to clipboard
sleep 400
n = 300

Loop, %n% 
{
	linet =
	needle =
	Number := (A_Index)
	CurrentEdit := (Edit%Number%)
	StringSplit, line, clipboard, `r`n		;splits clipboard into lines
	line := % line%Number%
	if line = 			
	{
		blank := (%blank%+1)
		if blank >3
		{
			break
		}
		continue
	}
	needle := "[]"						;look for []
	If InStr(line, Needle, True)
	{
		StringReplace, line, line, [],, All
	}
	Txtline2 = %Txtline2%`n%line%			;combines lines together again
}
clipboard = %Txtline2%
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Is it possible to stringreplace rtf to clipboard?

08 Mar 2018, 01:41

Well, the AHK help is your friend - have you already checked ClipBoardAll ?
itsfloody
Posts: 4
Joined: 18 Aug 2016, 22:06

Re: Is it possible to stringreplace rtf to clipboard?

08 Mar 2018, 15:57

Hey BoBo,
Thanks for the reply. Yeah tried ClipBoardAll and end up with what looks like chinese lettering

I might be able to do something with winClip...
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Is it possible to stringreplace rtf to clipboard?  Topic is solved

08 Mar 2018, 21:52

This worked for me. Have WordPad open with some formatted text ready, including the word 'qwerty'.

Code: Select all

q:: ;copy RTF to clipboard, edit RTF, paste
WinActivate, ahk_class WordPadClass
Clipboard := ""
Send, ^a^c
ClipWait, 3
if ErrorLevel
{
	MsgBox, % "error"
	return
}
vRtf := JEE_ClipboardGetRtf()
;MsgBox, % "[" vRtf "]"
vRtf := StrReplace(vRtf, "qwerty", "QWERTY")
Clipboard := ""
;MsgBox, % "[" vRtf "]"
JEE_ClipboardSetRtf(vRtf)
Send, ^v
return

;==================================================

JEE_ClipboardGetText(vFormat:=0xD, vEnc:="")
{
	;CF_LOCALE := 0x10 ;CF_UNICODETEXT := 0xD
	;CF_OEMTEXT := 0x7 ;CF_TEXT := 0x1
	if !DllCall("user32\IsClipboardFormatAvailable", UInt,vFormat)
		return
	if !DllCall("user32\OpenClipboard", Ptr,0)
		return
	if !hBuf := DllCall("user32\GetClipboardData", UInt,vFormat, Ptr)
	{
		DllCall("user32\CloseClipboard")
		return
	}

	pBuf := DllCall("kernel32\GlobalLock", Ptr,hBuf, Ptr)
	vSize := DllCall("kernel32\GlobalSize", Ptr,hBuf, UPtr)
	VarSetCapacity(vOutput, vSize, 0)
	DllCall("msvcrt\memcpy", Ptr,&vOutput, Ptr,pBuf, UPtr,vSize, "Cdecl Int")
	if !(vEnc = "")
		vOutput := StrGet(&vOutput, vSize, vEnc)
	VarSetCapacity(vOutput, -1)

	DllCall("kernel32\GlobalUnlock", Ptr,hBuf)
	DllCall("user32\CloseClipboard")
	return vOutput
}

;==================================================

JEE_ClipboardGetRtf(vEnc:="CP0")
{
	vFormat := DllCall("user32\RegisterClipboardFormat", Str,"Rich Text Format", UInt)
	return JEE_ClipboardGetText(vFormat, vEnc)
}

;==================================================

;q::
;FormatTime, vDate
;vRtf := "{\rtf{\b " vDate "}}"
;JEE_ClipboardSetRtf(vRtf, "e")
;vHtml := "<b>" vDate "</b>"
;JEE_ClipboardSetHtml(vHtml)
;SendInput, ^v
;return

;vOpt: e (empty clipboard before setting contents)
JEE_ClipboardSetRtf(vRtf, vOpt:="")
{
	vFormat := DllCall("user32\RegisterClipboardFormat", Str,"Rich Text Format", UInt)
	vSize := StrPut(vRtf, "CP0")
	VarSetCapacity(vData, vSize, 0)
	StrPut(vRtf, &vData, vSize, "CP0")

	;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
	hBuf := DllCall("kernel32\GlobalAlloc", UInt,0x42, UPtr,vSize, Ptr)
	pBuf := DllCall("kernel32\GlobalLock", Ptr,hBuf, Ptr)
	;DllCall("kernel32\RtlMoveMemory", Ptr,pBuf, Ptr,&vData, UPtr,vSize)
	DllCall("msvcrt\memcpy", Ptr,pBuf, Ptr,&vData, UPtr,vSize, "Cdecl Int")

	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("kernel32\GetCurrentProcessId", UInt))
	DllCall("kernel32\GlobalUnlock", Ptr,hBuf)
	DllCall("user32\OpenClipboard", Ptr,hWnd)
	if InStr(vOpt, "e")
		DllCall("user32\EmptyClipboard")
	DllCall("user32\SetClipboardData", UInt,vFormat, Ptr,hBuf, Ptr)
	DllCall("user32\CloseClipboard")
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
itsfloody
Posts: 4
Joined: 18 Aug 2016, 22:06

Re: Is it possible to stringreplace rtf to clipboard?

09 Mar 2018, 00:28

Perfect! Just what I needed. Thanks jeeswg!
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

Re: Is it possible to stringreplace rtf to clipboard?

10 Mar 2018, 17:09

Very nice indeed, jeeswg, it will be useful. Thank you!
Edit: Even more useful than I thought. If I replace

Code: Select all

WinActivate, ahk_class WordPadClass
with

Code: Select all

WinActivate, ahk_class OpusApp
it works with Word for .docx and .rtf, and I wasn't sure it would.
So a little extra code and it'll work with any editor that handles .docx or .rtf, I'm hoping.
Regards,
burque505
Rikk03
Posts: 192
Joined: 12 Oct 2020, 02:44

Re: Is it possible to stringreplace rtf to clipboard?

29 Aug 2021, 12:57

Is this possible with a copied webpage?

Its been a few years since this post, is there a better way of doing this now?

I tried copying one and pasting it to Wordpad but it does not retain the images as docx would.

I have exactly the same scenario listed above but rather I need to retain all formatting from a copied webpage as would be found on clipboardall, I just need to modify the text with a few regex.

Your help is appreciated.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: -Elaphe-, downstairs, Frogrammer and 205 guests