Clipboard
Use the Clipboard APIs to work with text from your clipboard and current selection. You can write contents to the clipboard through copyTextToClipboard and clear it through clearClipboard. The convenience action CopyToClipboardAction can be used to copy content of a selected list item to the clipboard.The getSelectedText API allows to get the current text selection of the frontmost application. This can be handy if you need to transform or act on the selection. The pasteText function inserts text at the current cursor position. We use this in the Clipboard History to paste an entry to your frontmost app. You can use the PasteAction to add this functionality to your list or form.

API Reference

clearClipboard

Clears the current clipboard contents.

Signature

1
async function clearClipboard(): Promise<void>
Copied!

Example

1
import { clearClipboard } from "@raycast/api";
2
3
export default async () => {
4
await clearClipboard();
5
};
Copied!

Return

A promise that resolves when the clipboard is cleared.

copyTextToClipboard

Copies text to the clipboard.

Signature

1
async function copyTextToClipboard(text: string): Promise<void>
Copied!

Example

1
import { copyTextToClipboard } from "@raycast/api";
2
3
export default async () => {
4
await copyTextToClipboard("https://raycast.com");
5
};
Copied!

Parameters

Name
Type
Required
Description
text
string
Yes
The text to copy to the clipboard.

Return

A promise that resolves when the text got copied to the clipboard.

getSelectedText

Gets the selected text of the frontmost application.

Signature

1
async function getSelectedText(): Promise<string>
Copied!

Example

1
import { getSelectedText, pasteText, showToast, ToastStyle } from "@raycast/api";
2
3
export default async () => {
4
try {
5
const selectedText = await getSelectedText();
6
const transformedText = selectedText.toUpperCase();
7
await pasteText(transformedText);
8
} catch (error) {
9
await showToast(ToastStyle.Failure, "Cannot transform text", String(error));
10
}
11
};
Copied!

Return

Returns a promise that resolves with the selected text.

pasteText

Pastes text to the current selection of the frontmost application.

Signature

1
async function pasteText(text: string): Promise<void>
Copied!

Example

1
import { pasteText } from "@raycast/api";
2
3
export default async () => {
4
await pasteText("I really like Raycast's API");
5
};
Copied!

Parameters

Name
Type
Required
Description
text
string
Yes
The text to insert at the cursor.

Return

A promise that resolves when the text got pasted.
Last modified 9d ago