plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

[autostart] Auto Start not working on Windows platform

Open akshatjain777 opened this issue 3 years ago • 4 comments

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();
}

akshatjain777 avatar Jul 24 '22 05:07 akshatjain777

Makes sense, you are using MacosLauncher

PringlePot avatar Sep 05 '22 06:09 PringlePot

Nope, the first argument is ignored on non-macos platforms.

FabianLars avatar Sep 05 '22 08:09 FabianLars

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(&current_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");
}

Hellager avatar Sep 13 '22 09:09 Hellager

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();
    }
  }
}

G07cha avatar Apr 30 '23 07:04 G07cha

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.

FabianLars avatar Aug 07 '24 12:08 FabianLars