打开器
此插件允许你在指定或默认的应用中打开文件和 URL。它还支持系统文件资源管理器中的 “revealing” 文件。
¥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.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
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
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-opener -
Modify
lib.rs
to initialize the plugin: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");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
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
shell 插件在 JavaScript 和 Rust 中均可用。
¥The shell plugin is available in both JavaScript and 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
的实例。
¥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"));
权限
¥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.
{ "$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. |
Tauri 中文网 - 粤ICP备13048890号