Shell
访问系统shell。允许您使用默认应用程序启动子进程并管理文件和URL。
支持的平台
该插件需要至少Rust版本的1.77.2
平台 | 级别 | 备注 |
---|---|---|
Windows | ||
Linux | ||
macOS | ||
Android | | 仅允许通过 |
iOS | | 仅允许通过 |
设置
安装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客户绑定
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
权限表
标识符 | 描述 |
---|---|
|
启用没有预配置作用域的执行命令。 |
|
拒绝没有预配置作用域的执行命令。 |
|
允许在任何预先配置的范围之外使用kill命令。 |
|
可以拒绝在没有预先配置范围的情况下使用kill命令。 |
|
允许在任何预先配置的范围之外使用open命令。 |
|
可以拒绝在没有预先配置范围的情况下使用open命令。 |
|
允许在任何预先配置的范围之外使用spawn命令。 |
|
可以拒绝在没有预先配置范围的情况下使用spawn命令。 |
|
允许在任何预先配置的范围之外使用stdin_write命令。 |
|
可以拒绝在没有预先配置范围的情况下使用stdin_write命令。 |
© 2025 Tauri 贡献者。CC-BY / MIT