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
使用方式
shell 插件在 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