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