clipboard: remove individual clipboard formats + save to clp file

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 5748
Joined: 19 Dec 2016, 01:58
Location: UK

clipboard: remove individual clipboard formats + save to clp file

06 Nov 2017, 13:14

The clipboard consists of multiple clipboard formats, e.g. text, RTF, html, images. You can actually have a text format and an image format on the clipboard at the same time, which bear no relation to each other.

Some relevant utilities:
- NirSoft InsideClipboard
- Free Clipboard Viewer
These allow you to see which clipboard formats are on the clipboard.

Some examples of clipboard formats:

Code: Select all

;CF_TEXT := 0x1
;CF_BITMAP := 0x2
;CF_METAFILEPICT := 0x3
;CF_SYLK := 0x4
;CF_DIF := 0x5
;CF_TIFF := 0x6
;CF_OEMTEXT := 0x7
;CF_DIB := 0x8
;CF_PALETTE := 0x9
;CF_PENDATA := 0xA
;CF_RIFF := 0xB
;CF_WAVE := 0xC
;CF_UNICODETEXT := 0xD
;CF_ENHMETAFILE := 0xE
;CF_HDROP := 0xF
;CF_LOCALE := 0x10
;CF_DIBV5 := 0x11
;CF_OWNERDISPLAY := 0x80
;CF_DSPTEXT := 0x81
;CF_DSPBITMAP := 0x82
;CF_DSPMETAFILEPICT := 0x83
;CF_DSPENHMETAFILE := 0x8E
;CF_PRIVATEFIRST := 0x200
;CF_PRIVATELAST := 0x2FF
;CF_GDIOBJFIRST := 0x300
;CF_GDIOBJLAST := 0x3FF
;Rich Text Format (49365)
;HTML Format (49391)
It's easy to add a clipboard format to the clipboard, e.g. see:
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 34#p170834

But it's not easy to remove a clipboard format from the clipboard: you have to copy all of the clipboard formats to memory, clear the clipboard, and add the clipboard formats back to the clipboard, but missing out the clipboard formats that you don't want.

I have written two functions:
- JEE_ClipboardRemoveFormats: Remove specific clipboard formats from the clipboard. This is useful for seeing which clipboard formats a utility can handle, and which formats it falls back to, when certain clipboard formats are not available, e.g. RTF and HTML.
- JEE_FileClipboardToClp: Copy the contents of the clipboard to a clp file. AutoHotkey can already do this, but I just wanted to experiment.

Creating a clp file consists of creating a file with binary data with 3 chunks per clipboard format: clipboard format type, data size, data. And some trailing null bytes at the end.

Do make any suggestions or notify of any issues.

Code: Select all

q:: ;clipboard - remove formats (e.g. test this in WordPad before and after copying text from the Internet)
;Rich Text Format (49365) ;HTML Format (49391)
;vList := "49365,49391"
vList := "49365"
JEE_ClipboardRemoveFormats(vList)
return

w:: ;clipboard - save current contents of clipboard to a clp file
vPath = %A_Desktop%\z %A_Now%.clp
JEE_FileClipboardToClp(vPath)
Clipboard := ""
FileRead, Clipboard, % "*c " vPath
return

e:: ;clipboard - save current contents of clipboard to a clp file
vPath = %A_Desktop%\z %A_Now%.clp
FileAppend, % ClipboardAll, % vPath
Clipboard := ""
FileRead, Clipboard, % "*c " vPath
return

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

;vList: a comma-separated list
JEE_ClipboardRemoveFormats(vList)
{
	if !DllCall("user32\CountClipboardFormats")
		return
	vList := StrReplace(vList, ",", "|")
	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("kernel32\GetCurrentProcessId", UInt))
	DllCall("user32\OpenClipboard", Ptr,hWnd)
	vList2 := ""
	while vFormat := DllCall("user32\EnumClipboardFormats", UInt,vFormat?vFormat:0, UInt)
	{
		if (vFormat ~= "^(" vList ")$")
			continue
		vList2 .= vFormat ","
		hData := DllCall("user32\GetClipboardData", UInt,vFormat, Ptr)
		vSize%vFormat% := DllCall("kernel32\GlobalSize", Ptr,hData, UPtr)
		pData := DllCall("kernel32\GlobalLock", Ptr,hData, Ptr)
		VarSetCapacity(vData%vFormat%, vSize%vFormat%, 0)
		DllCall("kernel32\RtlMoveMemory", Ptr,&vData%vFormat%, Ptr,pData, UPtr,vSize%vFormat%)
		DllCall("kernel32\GlobalUnlock", Ptr,hData)
	}

	DllCall("user32\EmptyClipboard")
	vList2 := SubStr(vList2, 1, -1)
	Loop, Parse, vList2, % ","
	{
		vFormat := A_LoopField
		;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
		hData := DllCall("kernel32\GlobalAlloc", UInt,0x42, UPtr,vSize%vFormat%, Ptr)
		pData := DllCall("kernel32\GlobalLock", Ptr,hData, Ptr)
		DllCall("kernel32\RtlMoveMemory", Ptr,pData, Ptr,&vData%vFormat%, UPtr,vSize%vFormat%)
		DllCall("user32\SetClipboardData", UInt,vFormat, Ptr,hData, Ptr)
		DllCall("kernel32\GlobalUnlock", Ptr,hData)
	}
	DllCall("user32\SetClipboardData", UInt,vFormat, Ptr,hData, Ptr)
	DllCall("user32\CloseClipboard")
}

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

JEE_FileClipboardToClp(vPath)
{
	if !DllCall("user32\CountClipboardFormats")
	{
		FileAppend,, % vPath
		return
	}
	if !oFile := FileOpen(vPath, "w")
		return
	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("kernel32\GetCurrentProcessId", UInt))
	DllCall("user32\OpenClipboard", Ptr,hWnd)
	while vFormat := DllCall("user32\EnumClipboardFormats", UInt,vFormat?vFormat:0, UInt)
	{
		hData := DllCall("user32\GetClipboardData", UInt,vFormat, Ptr)
		vSize := DllCall("kernel32\GlobalSize", Ptr,hData, UPtr)
		pData := DllCall("kernel32\GlobalLock", Ptr,hData, Ptr)
		VarSetCapacity(vData, vSize, 0)
		DllCall("kernel32\RtlMoveMemory", Ptr,&vData, Ptr,pData, UPtr,vSize)
		DllCall("kernel32\GlobalUnlock", Ptr,hData)
		oFile.WriteUIntType(vFormat)
		oFile.WriteUIntType(vSize)
		oFile.RawWrite(&vData, vSize)
	}
	oFile.Close()
}

;==================================================
Links:
c++ - How to clear specified format data from clipboard? - Stack Overflow
https://stackoverflow.com/questions/144 ... -clipboard

- [EDIT:] Note: the variable ClipboardAll does not contain all of the clipboard formats, some of the clipboard formats are omitted. See the source code: var.cpp, check for Var::GetClipboardAll(.
- [EDIT:] Example two-way compatible functions, to do clipboard contents to file, and file to clipboard contents.
conversion logic, v1 = -> v1 := -> v2, two-way compatibility - Page 6 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 68#p191068
- [EDIT:] A further link:
Saving ClipboardAll to a nested object does not work - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=41927
Last edited by jeeswg on 01 Jan 2018, 18:57, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 3528
Joined: 17 Jul 2016, 01:02
Contact:

Re: clipboard: remove individual clipboard formats + save to clp file

08 Nov 2017, 12:26

Nice jeeswg! Thanks for sharing :wave:
Some examples of clipboard formats:
I was happily unaware :) :wtf:

Cheers!

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 24 guests