打开器
此插件允许你在指定的或默认的应用中打开文件和网址。它还支持在系统的文件管理器中“显示”文件。
🌐 This plugin allows you to open files and URLs in a specified, or the default, application. It also supports “revealing” files in the system’s file explorer.
🌐 Supported Platforms
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 |
🌐 Setup
安装 opener 插件即可开始使用。
🌐 Install the opener plugin to get started.
使用你项目的包管理器添加依赖:
npm run tauri add openeryarn run tauri add openerpnpm tauri add openerdeno task tauri add openerbun tauri add openercargo 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
🌐 Usage
Opener 插件支持 JavaScript 和 Rust 语言。
🌐 The opener plugin is available in both JavaScript and Rust.
import { openPath, openUrl } 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');// opens a URL using the default program:await openUrl('https://tauri.nodejs.cn');请注意,app 是 App 或 AppHandle 的一个实例。
🌐 Note that app is an instance of App or 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"));// opens a URL using the default program:app.opener().open_url("https://tauri.nodejs.cn", None::<&str>);🌐 Permissions
默认情况下,所有潜在危险的插件命令和作用域都会被阻止,无法访问。你必须在 capabilities 配置中修改权限以启用这些功能。
🌐 By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.
有关更多信息,请参见功能概览,有关使用插件权限的分步指南,请参见分步指南。
🌐 See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
下面是两个示例范围配置。path 和 url 都使用 glob 模式语法 来定义允许的文件路径和 URL。
🌐 Below are two example scope configurations. Both path and url use the glob pattern syntax to define allowed file paths and URLs.
首先,举一个如何为 openPath() 函数向特定路径添加权限的例子:
🌐 First, an example on how to add permissions to specific paths for the openPath() function:
{ "$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" } ] } ]}最后,给出一个示例,说明如何为 openUrl() 功能添加对精确 https://tauri.nodejs.cn URL 以及自定义协议(必须被操作系统识别)的所有 URL 的权限:
🌐 Lastly, an example on how to add permissions for the exact https://tauri.nodejs.cn URL and all URLs on a custom protocol (must be known to the OS) for the openUrl() function:
{ "$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.nodejs.cn" }, { "url": "custom:*" } ] } ]}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
This default permission set includes the following:
allow-open-urlallow-reveal-item-in-dirallow-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. |
Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站