Clipboard
Read and write to the system clipboard using the clipboard plugin.
Setup
Install the clipboard plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add clipboard-manager
yarn run tauri add clipboard-manager
pnpm tauri add clipboard-manager
bun tauri add clipboard-manager
cargo tauri add clipboard-manager
-
Run
cargo add tauri-plugin-clipboard-manager
to add the plugin to the project’s dependencies inCargo.toml
. -
Modify
lib.rs
to initialize the plugin:lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default()// Initialize the plugin.plugin(tauri_plugin_clipboard_manager::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
If you’d like to manage the clipboard in JavaScript then install the npm package as well:
npm install @tauri-apps/plugin-clipboard-manageryarn add @tauri-apps/plugin-clipboard-managerpnpm add @tauri-apps/plugin-clipboard-managerbun add @tauri-apps/plugin-clipboard-manager
Usage
The clipboard plugin is available in both JavaScript and Rust.
import { writeText, readText } from '@tauri-apps/plugin-clipboard-manager';// when using `"withGlobalTauri": true`, you may use// const { writeText, readText } = window.__TAURI_PLUGIN_CLIPBOARD_MANAGER__;
// Write content to clipboardawait writeText('Tauri is awesome!');
// Read content from clipboardconst content = await readText();console.log(content);// Prints "Tauri is awesome!" to the console
use tauri_plugin_clipboard_manager::ClipboardExt;
// Write content to clipboardlet clipboard_content = tauri_plugin_clipboard_manager::ClipKind::PlainText { label: Some("Label".to_string()), text: "Tauri is awesome!".to_string(),};app.clipboard().write(clipboard_content).unwrap();
// Read content from clipboardlet content = app.clipboard().read();println!("{:?}", content.unwrap());// Prints "Tauri is awesome!" to the terminal
Default Permission
No features are enabled by default, as we believe the clipboard can be inherently dangerous and it is application specific if read and/or write access is needed.
Clipboard interaction needs to be explicitly enabled.
Permission Table
Identifier | Description |
---|---|
|
Enables the clear command without any pre-configured scope. |
|
Denies the clear command without any pre-configured scope. |
|
Enables the read_image command without any pre-configured scope. |
|
Denies the read_image command without any pre-configured scope. |
|
Enables the read_text command without any pre-configured scope. |
|
Denies the read_text command without any pre-configured scope. |
|
Enables the write_html command without any pre-configured scope. |
|
Denies the write_html command without any pre-configured scope. |
|
Enables the write_image command without any pre-configured scope. |
|
Denies the write_image command without any pre-configured scope. |
|
Enables the write_text command without any pre-configured scope. |
|
Denies the write_text command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT