[autostart] Auto Start not working on Windows platform
I have added the below code to main.rs, and its working for macOS but not on windows.
use tauri_plugin_autostart::MacosLauncher;
fn main() {
tauri::Builder::default()
.plugin(tauri_plugin_autostart::init(MacosLauncher::LaunchAgent, false /* hidden flag */))
.run();
}
Makes sense, you are using MacosLauncher
Nope, the first argument is ignored on non-macos platforms.
try using auto-launch directly like this, it works well for me on window10
// cargo.toml
[dependencies]
....
auto-launch = "0.4.0"
// main.rs
use auto_launch::*;
use std::env::current_exe;
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.setup(|app| {
let app_name = &app.package_info().name;
let current_exe = current_exe().unwrap();
let auto_start = AutoLaunchBuilder::new()
.set_app_name(&app_name)
.set_app_path(¤t_exe.to_str().unwrap())
.set_use_launch_agent(true)
.build()
.unwrap();
auto_start.enable().unwrap();
println!("is autostart {}", auto_start.is_enabled().unwrap());
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
I understand that it won't work until you enable it by importing and running enable function from tauri-plugin-autostart-api on frontend. One issue I've faced with specifically on Windows is that disabling already disabled autostart will lead to an error so one should explicitly check with isEnabled function:
import * as autostart from 'tauri-plugin-autostart-api';
// To toggle autostart
const setAutostart = async (newValue) => {
if (newValue !== (await autostart.isEnabled())) {
if (newValue) {
await autostart.enable();
} else {
await autostart.disable();
}
}
}
We've seen tons of apps use it and only you reported this issue. Since this was 2 years ago i will close this issue. If you still face this problem, please provide us with a minimal reproduction repository and the output of the tauri info command.