Crate auto_launch

source ·
Expand description

Auto launch any application or executable at startup. Supports Windows, Mac (via AppleScript or Launch Agent), and Linux.

Usage

The parameters of AutoLaunch::new are different on each platform. See the function definition or the demo below for details.

Or you can construct the AutoLaunch by using AutoLaunchBuilder.

use auto_launch::AutoLaunch;

fn main() {
    let app_name = "the-app";
    let app_path = "/path/to/the-app";
    let args = &["--minimized"];
    let auto = AutoLaunch::new(app_name, app_path, args);

    // enable the auto launch
    auto.enable().is_ok();
    auto.is_enabled().unwrap();

    // disable the auto launch
    auto.disable().is_ok();
    auto.is_enabled().unwrap();
}

macOS

macOS supports two ways to achieve auto launch (via AppleScript or Launch Agent). When the use_launch_agent is true, it will achieve by Launch Agent, otherwise by AppleScript.

Note:

  • The app_path should be a absolute path and exists. Otherwise, it will cause an error when enable.
  • In case using AppleScript, the app_name should be same as the basename of app_path, or it will be corrected automatically.
use auto_launch::AutoLaunch;

fn main() {
    let app_name = "the-app";
    let app_path = "/path/to/the-app.app";
    let args = &["--minimized"];
    let auto = AutoLaunch::new(app_name, app_path, false, args);

    // enable the auto launch
    auto.enable().is_ok();
    auto.is_enabled().unwrap();

    // disable the auto launch
    auto.disable().is_ok();
    auto.is_enabled().unwrap();
}

Windows

On Windows, it will add a registry entry under \HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.

use auto_launch::AutoLaunch;

fn main() {
    let app_name = "the-app";
    let app_path = "C:\\path\\to\\the-app.exe";
    let args = &["--minimized"];
    let auto = AutoLaunch::new(app_name, app_path, args);

    // enable the auto launch
    auto.enable().is_ok();
    auto.is_enabled().unwrap();

    // disable the auto launch
    auto.disable().is_ok();
    auto.is_enabled().unwrap();
}

Builder

AutoLaunch Builder helps to eliminate the constructor difference on various platforms.

use auto_launch::*;

fn main() {
    let auto = AutoLaunchBuilder::new()
        .set_app_name("the-app")
        .set_app_path("/path/to/the-app")
        .set_use_launch_agent(true)
        .set_args(&["--minimized"])
        .build()
        .unwrap();

    auto.enable().is_ok();
    auto.is_enabled().unwrap();

    auto.disable().is_ok();
    auto.is_enabled().unwrap();
}

Structs

  • The parameters of AutoLaunch::new are different on each platform.
  • AutoLaunch Builder helps to eliminate the constructor difference on various platforms.

Enums

Type Aliases