writeText
Category: Clipboard Operations
Namespace: web.clipboard.writeText
Description
Writes a string to the Windows clipboard. Once written, the text is available to paste into any application on the system — Word, Notepad, Excel, a browser, etc.
Syntax
// Options form
const success = await web.clipboard.writeText({ text: string });
// Simple string form
const success = await web.clipboard.writeText('text to copy');
Parameters
- text (required) - The string to place on the clipboard. Pass an empty string to clear clipboard text.
Returns
Promise<boolean> - Returns true if the text was written successfully. Throws on error.
Examples
Copy button for a code snippet
document.getElementById('btn-copy').addEventListener('click', async function() {
const code = document.getElementById('code-block').textContent;
await web.clipboard.writeText({ text: code });
this.textContent = 'Copied!';
setTimeout(() => this.textContent = 'Copy', 1500);
});
Copy current file path
async function copyFilePath(filePath) {
await web.clipboard.writeText({ text: filePath });
showToast('Path copied to clipboard');
}
Copy with error handling
try {
await web.clipboard.writeText({ text: document.getElementById('output').value });
alert('Copied to clipboard!');
} catch (error) {
alert('Could not copy: ' + error.message);
}
Use Cases
- "Copy to clipboard" buttons in text editors, code viewers, and note-taking apps
- Copying file paths, URLs, or generated output for use in other applications
- Exporting data (CSV rows, JSON snippets) to the clipboard for pasting elsewhere
- Keyboard shortcut handlers (e.g. Ctrl+C on selected content)
Notes
- Replaces whatever was previously on the clipboard
- Passing an empty string clears the clipboard text
- The text remains on the clipboard until overwritten by another copy operation or the system clears it
- For reading the clipboard, use
web.clipboard.readText()