Core Concepts

Clipboard

Copy text to and read text from the system clipboard for automation workflows.

What you can do

Copy Text

Copy text to the system clipboard

clipboard.setContent(text)

Read Text

Read the current text content from the system clipboard

clipboard.getContent()

Quick Reference

setContent

clipboard.setContent(text: string)
Promise<void>

Copy a given text to the system clipboard

getContent

clipboard.getContent()
Promise<string>

Read the current text content of the system clipboard


Copying Text

Use setContent to copy text to the system clipboard. This is equivalent to a user pressing Ctrl+C / Cmd+C after selecting text.

typescript
import { clipboard } from "@nut-tree/nut-js";

// Copy text to the clipboard
await clipboard.setContent("Hello, world!");

Reading Text

Use getContent to retrieve the current text stored in the system clipboard.

typescript
import { clipboard } from "@nut-tree/nut-js";

// Read clipboard content
const content = await clipboard.getContent();
console.log(content);

Text Only

The clipboard API is limited to plain text content. Images and other clipboard data types are not supported.

Examples

Paste Text into a Field

Set clipboard content and paste it using keyboard shortcuts

Scenario: You need to enter a long string into an input field quickly.

typescript
import { clipboard, keyboard, Key } from "@nut-tree/nut-js";

// Copy the text to clipboard
await clipboard.setContent("user@example.com");

// Paste it using keyboard shortcut
await keyboard.pressKey(Key.LeftControl, Key.V);
await keyboard.releaseKey(Key.LeftControl, Key.V);

Read and Verify Clipboard

Capture selected text via clipboard for verification

Scenario: You want to verify that a UI element contains the expected text.

typescript
import { clipboard, keyboard, Key } from "@nut-tree/nut-js";

// Select all text in the focused field
await keyboard.pressKey(Key.LeftControl, Key.A);
await keyboard.releaseKey(Key.LeftControl, Key.A);

// Copy selection to clipboard
await keyboard.pressKey(Key.LeftControl, Key.C);
await keyboard.releaseKey(Key.LeftControl, Key.C);

// Read and check the content
const content = await clipboard.getContent();
console.log("Field contains:", content);

Transfer Text Between Applications

Use the clipboard to move data between apps

Scenario: You need to copy a value from one application and paste it into another.

typescript
import { clipboard, keyboard, Key } from "@nut-tree/nut-js";

// Copy from source application (assuming text is already selected)
await keyboard.pressKey(Key.LeftControl, Key.C);
await keyboard.releaseKey(Key.LeftControl, Key.C);

// Read what was copied
const data = await clipboard.getContent();
console.log("Copied:", data);

// ... switch to target application ...

// Paste into target
await keyboard.pressKey(Key.LeftControl, Key.V);
await keyboard.releaseKey(Key.LeftControl, Key.V);

Next Steps

Now that you understand clipboard operations, learn about the different ways to search for content on screen:

Was this page helpful?