Clipboard

GetClipboard

There is one clipboard per machine, so GPAL.Clipboard is standalone rather than something hanging off a browser or an application. GetClipboard says what to take and SaveTo says where to put it. GPAL supports six clipboard types. Text is plain characters. Html and Rtf are markup: the same words with the bold, the links and the colours still on them, which is what an application puts alongside the plain text when the words were formatted. Csv is a delimited table. FileList is the files themselves rather than anything about them, which is what a copy in a folder puts on. Image is a bitmap. Text is what GetClipboard takes when nothing is named. SaveTo decides what you get back. A string gives the text, and an image base64 encoded. A grid splits lines into rows and tabs into cells, so a range copied out of a spreadsheet arrives as rows and cells with no parsing. A file writes whatever was taken, including the image itself. The STA thread and the retries while another process holds the clipboard are GPAL's problem, not yours.

NOTE

This is application automation, where the text comes from UIAutomation rather than from a DOM. A control that offers only UIAutomation's ValuePattern truncates what it hands back, commonly at 4096 characters, and nothing reports that it happened, so GetGrid can return part of a long document looking complete. Select all and copy inside the application, by keystroke or by invoking its own Select All and Copy commands, then read the clipboard, and the whole document comes back.

Examples

GPAL Fluent: High-level fluent C# API

//GetClipboard takes an optional ClipboardType of Text, Html, Rtf, Csv, FileList or Image, and Text is used when none is given. SaveTo decides the shape: a string for the text, a grid for tab separated lines, a file for anything including an image. An image saved to a string comes back base64 encoded as a PNG, and saved to a file it is written in the format the extension asks for.

// text, which is what it takes when no type is given

GPAL.Clipboard

.GetClipboard()

.SaveTo(out string text);


// a copied spreadsheet range, straight into a grid

GPAL.Clipboard

.GetClipboard()

.SaveTo(out IGPALGrid<string> rows);


// the html a browser copy also put there

GPAL.Clipboard

.GetClipboard(ClipboardType.Html)

.SaveTo(out string markup);


// an image, written in the format the extension asks for

GPAL.Clipboard

.GetClipboard(ClipboardType.Image)

.SaveTo("shot.png");


// the same image as base64, for an API body or a data URI

GPAL.Clipboard

.GetClipboard(ClipboardType.Image)

.SaveTo(out string encoded);


// the whole text of a control that truncates through ValuePattern.

// the same can be done by invoking the application's own Select All

// and Copy commands off its menu instead of sending keystrokes

application

.WithSelector(editorPane)

.LeftClick()

.PressModifierKey(ModifierKeys.Control)

.SendString("a")

.SendString("c")

.ReleaseModifierKey(ModifierKeys.Control);


GPAL.Clipboard

.GetClipboard()

.SaveTo(out string wholeDocument);

💬 Ask GPAL