Shell
访问系统 shell。允许你生成子进程。
¥Access the system shell. Allows you to spawn child processes.
支持的平台
¥Supported Platforms
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | Only allows to open URLs via |
ios | | Only allows to open URLs via |
打开器
¥Opener
如果你正在寻找 shell.open
API 的文档,请查看新的 Opener 插件。
¥If you’re looking for documentation for the shell.open
API, check out the new Opener plugin instead.
设置
¥Setup
安装 shell 插件即可开始使用。
¥Install the shell plugin to get started.
使用项目的包管理器添加依赖:
¥Use your project’s package manager to add the dependency:
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
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-shell -
Modify
lib.rs
to initialize the plugin: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");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
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
使用
¥Usage
shell 插件在 JavaScript 和 Rust 中均可用。
¥The shell plugin is available in both JavaScript and 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());}
权限
¥Permissions
默认情况下,所有潜在危险的插件命令和范围都会被阻止,无法访问。你必须修改 capabilities
配置中的权限才能启用这些权限。
¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities
configuration to enable these.
有关更详细的说明,请参阅 功能概述。
¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "$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 } ] } ]}
Default Permission
This permission set configures which shell functionality is exposed by default.
Granted Permissions
It allows to use the open
functionality without any specific
scope pre-configured. It will allow opening http(s)://
,
tel:
and mailto:
links.
allow-open
Permission Table
Identifier | Description |
---|---|
|
Enables the execute command without any pre-configured scope. |
|
Denies the execute command without any pre-configured scope. |
|
Enables the kill command without any pre-configured scope. |
|
Denies the kill command without any pre-configured scope. |
|
Enables the open command without any pre-configured scope. |
|
Denies the open command without any pre-configured scope. |
|
Enables the spawn command without any pre-configured scope. |
|
Denies the spawn command without any pre-configured scope. |
|
Enables the stdin_write command without any pre-configured scope. |
|
Denies the stdin_write command without any pre-configured scope. |
Tauri v2.4 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站