Crate auto_launch

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, LinuxLaunchMode};

fn main() {
    let app_name = "the-app";
    let app_path = "/path/to/the-app";
    let args = &["--minimized"];
    // Use XDG Autostart by default, or use LinuxLaunchMode::Systemd for systemd
    let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::XdgAutostart, 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:

  • Launch Agent: Uses plist files in ~/Library/LaunchAgents/ (default)
  • AppleScript: Uses AppleScript to add login items

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.
  • In case using AppleScript, only --hidden and --minimized in args are valid, which means that hide the app on launch.
use auto_launch::{AutoLaunch, MacOSLaunchMode};

fn main() {
    let app_name = "the-app";
    let app_path = "/path/to/the-app.app";
    let args = &["--minimized"];
    let bundle_identifiers = &["com.github.auto-launch-test"];
    // Use Launch Agent by default, or use MacOSLaunchMode::AppleScript
    let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::LaunchAgent, args, bundle_identifiers, "");

    // 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 either \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run (system-wide) or \HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run (current user only).

By default we try to apply the auto launch to the system registry, which requires admin privileges and applies the auto launch to any user in the system. If there’s no permission to do so, we fallback to enabling it to the current user only. To change this behavior, specify the WindowsEnableMode when creating the AutoLaunch instance.

use auto_launch::{AutoLaunch, WindowsEnableMode};

fn main() {
    let app_name = "the-app";
    let app_path = "C:\\path\\to\\the-app.exe";
    let args = &["--minimized"];
    let enable_mode = WindowsEnableMode::CurrentUser;
    let auto = AutoLaunch::new(app_name, app_path, enable_mode, 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::*;

let auto = AutoLaunchBuilder::new()
    .set_app_name("the-app")
    .set_app_path("/path/to/the-app")
    .set_macos_launch_mode(MacOSLaunchMode::LaunchAgent)
    .set_args(&["--minimized"])
    .build()?;

auto.enable()?;
auto.is_enabled()?;

auto.disable()?;
auto.is_enabled()?;

Structs§

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

Enums§

Error
LinuxLaunchMode
Determines how the auto launch is enabled on Linux.
MacOSLaunchMode
Determines how the auto launch is enabled on macOS.
WindowsEnableMode
Determines how the auto launch is enabled on Windows.

Type Aliases§

Result