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 访客绑定
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:
链接。
此默认权限集包括以下内容
允许打开
权限表
标识符 | 描述 |
---|---|
|
启用执行命令,不带任何预配置范围。 |
|
禁用执行命令,不带任何预配置范围。 |
|
启用 kill 命令,不带任何预配置范围。 |
|
禁用 kill 命令,不带任何预配置范围。 |
|
启用打开命令,不带任何预配置范围。 |
|
禁用打开命令,不带任何预配置范围。 |
|
启用 spawn 命令,不带任何预配置范围。 |
|
禁用 spawn 命令,不带任何预配置范围。 |
|
启用 stdin_write 命令,不带任何预配置范围。 |
|
禁用 stdin_write 命令,不带任何预配置范围。 |
© 2025 Tauri 贡献者。CC-BY / MIT