自动启动
在系统启动时自动启动应用程序。
支持的平台
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | ||
linux | ||
macos | ||
android | | |
ios | |
设置
从安装 autostart 插件开始。
使用项目的包管理器来添加依赖。
npm run tauri add autostart
yarn run tauri add autostart
pnpm tauri add autostart
deno task tauri add autostart
bun tauri add autostart
cargo tauri add autostart
-
在
src-tauri
文件夹中运行以下命令,将插件添加到Cargo.toml
中的项目依赖项中。cargo add tauri-plugin-autostart --target 'cfg(any(target_os = "macos", windows, target_os = "linux"))' -
修改
lib.rs
来初始化插件。src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(desktop)]app.handle().plugin(tauri_plugin_autostart::init(tauri_plugin_autostart::MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]) /* 传递给应用程序的任意数量的参数 */));Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
你可以使用你喜欢的 JavaScript 包管理器来安装 JavaScript Guest 绑定。
npm install @tauri-apps/plugin-autostartyarn add @tauri-apps/plugin-autostartpnpm add @tauri-apps/plugin-autostartdeno add npm:@tauri-apps/plugin-autostartbun add @tauri-apps/plugin-autostart
用法
autostart 插件有 JavaScript 和 Rust 两种版本。
import { enable, isEnabled, disable } from '@tauri-apps/plugin-autostart';
// 启用 autostartawait enable();// 检查 enable 状态console.log(`registered for autostart? ${await isEnabled()}`);// 禁用 autostartdisable();
use tauri_plugin_autostart::MacosLauncher;use tauri_plugin_autostart::ManagerExt;
#[cfg_attr(mobile, tauri::mobile_entry_point)]fn run() { tauri::Builder::default() .plugin(tauri_plugin_autostart::init( MacosLauncher::LaunchAgent, Some(vec!["--flag1", "--flag2"]), )) .setup(|app| { // 获取自动启动管理器 let autostart_manager = app.autolaunch(); // 启用 autostart let _ = autostart_manager.enable(); // 检查 enable 状态 println!("registered for autostart? {}", autostart_manager.is_enabled().unwrap()); // 禁用 autostart let _ = autostart_manager.disable(); Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
权限
默认情况下,所有具有潜在危险的插件命令和范围都会被阻止且无法访问。您必须修改 capabilities
文件夹中的配置来启用它们。
参见能力概览以获取更多信息,以及插件的分步导览来调整插件权限。
{ "permissions": [ ..., "autostart:allow-enable", "autostart:allow-disable", "autostart:allow-is-enabled" ]}
Default Permission
This permission set configures if your application can enable or disable auto starting the application on boot.
Granted Permissions
It allows all to check, enable and disable the automatic start on boot.
This default permission set includes the following:
allow-enable
allow-disable
allow-is-enabled
Permission Table
Identifier | Description |
---|---|
|
Enables the disable command without any pre-configured scope. |
|
Denies the disable command without any pre-configured scope. |
|
Enables the enable command without any pre-configured scope. |
|
Denies the enable command without any pre-configured scope. |
|
Enables the is_enabled command without any pre-configured scope. |
|
Denies the is_enabled command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT