指定打开应用
此插件允许你使用特定或者默认的应用程序打开文件或者 URL。它还可以在系统文件管理器中“显示”这些文件。
支持的平台
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | Only allows to open URLs via |
ios | | Only allows to open URLs via |
设置
首先安装“指定打开应用”插件。
使用项目的包管理器来添加依赖:
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
文件的dependencies
字段中: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
用法
此插件有 JavaScript 和 Rust 两种调用方式。
import { openPath } from '@tauri-apps/plugin-opener';// 当配置为 `"withGlobalTauri": true` 时,你可以使用下面方式引入此插件// const { openPath } = window.__TAURI__.opener;
// 使用默认的应用来打开文件await openPath('/path/to/file');// 在 Windows 上使用 `vlc` 打开文件await openPath('C:/path/to/file', 'vlc');
下面代码中的 app
变量是 App
或者 AppHandle
的实例。
use tauri_plugin_opener::OpenerExt;
// 使用默认的应用来打开文件:app.opener().open_path("/path/to/file", None::<&str>);// 在 Windows 上使用 `vlc` 打开文件: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" } ] } ]}
Default Permission
This permission set allows opening mailto:
, tel:
, https://
and http://
urls using their default application
as well as reveal file in directories using default file explorer
allow-open-url
allow-reveal-item-in-dir
allow-default-urls
Permission Table
Identifier | Description |
---|---|
|
This enables opening |
|
Enables the open_path command without any pre-configured scope. |
|
Denies the open_path command without any pre-configured scope. |
|
Enables the open_url command without any pre-configured scope. |
|
Denies the open_url command without any pre-configured scope. |
|
Enables the reveal_item_in_dir command without any pre-configured scope. |
|
Denies the reveal_item_in_dir command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT