Opener
此插件允许你在指定的或默认的应用程序中打开文件和 URL。它还支持在系统的文件资源管理器中“显示”文件。
支持的平台
此插件需要至少 1.77.2 版本的 Rust
平台 | 级别 | 注释 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | 仅允许通过 |
ios | | 仅允许通过 |
设置
安装 opener 插件以开始使用。
使用你的项目包管理器添加依赖
npm run tauri add opener
yarn run tauri add opener
pnpm tauri add opener
deno task tauri add opener
bun tauri add opener
cargo tauri add opener
-
在
src-tauri
文件夹中运行以下命令,将插件添加到Cargo.toml
项目的依赖项中cargo add tauri-plugin-opener -
修改
lib.rs
以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_opener::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用你首选的 JavaScript 包管理器安装 JavaScript Guest 绑定
npm install @tauri-apps/plugin-openeryarn add @tauri-apps/plugin-openerpnpm add @tauri-apps/plugin-openerdeno add npm:@tauri-apps/plugin-openerbun add @tauri-apps/plugin-opener
用法
opener 插件在 JavaScript 和 Rust 中均可用。
import { openPath } from '@tauri-apps/plugin-opener';// when using `"withGlobalTauri": true`, you may use// const { openPath } = window.__TAURI__.opener;
// opens a file using the default program:await openPath('/path/to/file');// opens a file using `vlc` command on Windows:await openPath('C:/path/to/file', 'vlc');
请注意,app
是 App
或 AppHandle
的实例。
use tauri_plugin_opener::OpenerExt;
// opens a file using the default program:app.opener().open_path("/path/to/file", None::<&str>);// opens a file using `vlc` command on Windows:app.opener().open_path("C:/path/to/file", Some("vlc"));
权限
默认情况下,所有潜在危险的插件命令和作用域都被阻止且无法访问。你必须修改 capabilities
配置中的权限以启用它们。
有关更多信息,请参阅 能力概述,并参阅 使用插件权限的分步指南。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "opener:allow-open-path", "allow": [ { "path": "/path/to/file" } ] } ]}
默认权限
此权限集允许使用默认应用程序打开 mailto:
、tel:
、https://
和 http://
URL,以及使用默认文件资源管理器显示目录中的文件
此默认权限集包括以下内容
allow-open-url
allow-reveal-item-in-dir
allow-default-urls
权限表
标识符 | 描述 |
---|---|
|
这允许使用默认应用程序打开 |
|
启用 |
|
拒绝 |
|
启用 |
|
拒绝 |
|
启用 |
|
拒绝 |
© 2025 Tauri 贡献者。CC-BY / MIT