Shell
存取系統 Shell。允許您衍生子進程。
支援平台
此外掛程式需要 Rust 版本至少 1.77.2
平台 | 層級 | 注意事項 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | 僅允許透過 |
ios | | 僅允許透過 |
開啟器
如果您正在尋找 shell.open
API 的文件,請改為查看新的Opener 外掛程式。
設定
安裝 shell 外掛程式以開始使用。
使用您的專案套件管理器新增依賴項
npm run tauri add shell
yarn run tauri add shell
pnpm tauri add shell
deno task tauri add shell
bun tauri add shell
cargo tauri add shell
-
在
src-tauri
資料夾中執行以下命令,將此外掛程式新增至Cargo.toml
專案的依賴項中cargo add tauri-plugin-shell -
修改
lib.rs
以初始化此外掛程式src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_shell::init()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您偏好的 JavaScript 套件管理器安裝 JavaScript Guest 綁定
npm install @tauri-apps/plugin-shellyarn add @tauri-apps/plugin-shellpnpm add @tauri-apps/plugin-shelldeno add npm:@tauri-apps/plugin-shellbun add @tauri-apps/plugin-shell
用法
shell 外掛程式同時提供 JavaScript 和 Rust 版本。
import { Command } from '@tauri-apps/plugin-shell';// when using `"withGlobalTauri": true`, you may use// const { Command } = window.__TAURI__.shell;
let result = await Command.create('exec-sh', [ '-c', "echo 'Hello World!'",]).execute();console.log(result);
use tauri_plugin_shell::ShellExt;
let shell = app_handle.shell();let output = tauri::async_runtime::block_on(async move { shell .command("echo") .args(["Hello from Rust!"]) .output() .await .unwrap()});if output.status.success() { println!("Result: {:?}", String::from_utf8(output.stdout));} else { println!("Exit with code: {}", output.status.code().unwrap());}
權限
預設情況下,所有潛在危險的外掛程式命令和範圍都會被封鎖且無法存取。您必須修改 capabilities
設定中的權限才能啟用這些。
請參閱功能總覽以取得更多資訊,以及使用外掛程式權限的逐步指南。
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ { "identifier": "shell:allow-execute", "allow": [ { "name": "exec-sh", "cmd": "sh", "args": [ "-c", { "validator": "\\S+" } ], "sidecar": false } ] } ]}
預設權限
此權限集設定預設公開哪些 shell 功能。
已授與權限
它允許使用 open
功能,而無需預先設定任何特定範圍。它將允許開啟 http(s)://
、tel:
和 mailto:
連結。
allow-open
權限表
識別碼 | 描述 |
---|---|
|
啟用 execute 命令,而無需任何預先設定的範圍。 |
|
拒絕 execute 命令,而無需任何預先設定的範圍。 |
|
啟用 kill 命令,而無需任何預先設定的範圍。 |
|
拒絕 kill 命令,而無需任何預先設定的範圍。 |
|
啟用 open 命令,而無需任何預先設定的範圍。 |
|
拒絕 open 命令,而無需任何預先設定的範圍。 |
|
啟用 spawn 命令,而無需任何預先設定的範圍。 |
|
拒絕 spawn 命令,而無需任何預先設定的範圍。 |
|
啟用 stdin_write 命令,而無需任何預先設定的範圍。 |
|
拒絕 stdin_write 命令,而無需任何預先設定的範圍。 |
© 2025 Tauri 貢獻者。CC-BY / MIT