开启器
此插件允许您在指定或默认应用程序中打开文件和 URL。它还支持在系统的文件管理器中“显示”文件。
此插件需要 Rust 版本至少为 **1.77.2**
平台 | 级别 | 备注 |
---|---|---|
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 访客绑定
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
配置中的权限才能启用这些功能。
有关更多信息,请参阅功能概述,并参阅分步指南以使用插件权限。
以下是两个示例范围配置。path
和 url
都使用 glob 模式语法 来定义允许的文件路径和 URL。
首先,一个如何为 openPath()
函数添加特定路径权限的示例
{ "$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" }, { "path": "$APPDATA/file" } ] } ]}
最后,一个如何为精确的 https://tauri.org.cn
URL 和所有自定义协议(必须是操作系统已知的)上的 URL 添加权限的示例,用于 openUrl()
函数
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "opener:allow-open-url", "allow": [ { "url": "https://tauri.org.cn" }, { "url": "custom:*" } ] } ]}
默认权限
此权限集允许使用默认应用程序打开 mailto:
、tel:
、https://
和 http://
URL,以及使用默认文件管理器显示目录中的文件。
此默认权限集包括以下内容
允许打开网址
允许在目录中显示项目
允许默认网址
权限表
标识符 | 描述 |
---|---|
|
这允许使用默认应用程序打开 |
|
启用 open_path 命令,无需任何预配置范围。 |
|
拒绝 open_path 命令,无需任何预配置范围。 |
|
启用 open_url 命令,无需任何预配置范围。 |
|
拒绝 open_url 命令,无需任何预配置范围。 |
|
启用 reveal_item_in_dir 命令,无需任何预配置范围。 |
|
拒绝 reveal_item_in_dir 命令,无需任何预配置范围。 |
© 2025 Tauri 贡献者。CC-BY / MIT