Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ dirs = "^6"
windows-registry = "^0.6"
windows-result = "^0.4"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.31", features = ["user"] }

[target.'cfg(target_os = "macos")'.dependencies]
smappservice-rs = "0.1"
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
let auto = AutoLaunchBuilder::new()
.set_app_name("the-app")
.set_app_path("/path/to/the-app")
.set_macos_launch_mode(MacOSLaunchMode::LaunchAgent)
.set_macos_launch_mode(MacOSLaunchMode::LaunchAgentUser)
.build()
.unwrap();

Expand All @@ -40,9 +40,10 @@ fn main() {

Linux supports two ways to achieve auto launch:
- **XDG Autostart**: Uses `.desktop` files in `~/.config/autostart/` (default)
- **systemd**: Uses systemd user services in `~/.config/systemd/user/`
- Uses `systemctl --user enable` to enable the service.
- Requires `systemctl` to be available in the environment.
- **systemd user**: Uses systemd user services in `~/.config/systemd/user/`
- **systemd system**: Uses systemd system services in `/etc/systemd/system/`

Both systemd modes require `systemctl` to be available in the environment.

```rust
use auto_launch::{AutoLaunch, LinuxLaunchMode};
Expand All @@ -55,7 +56,10 @@ fn main() {
let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::XdgAutostart, &[] as &[&str]);

// Or use systemd user service
// let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::Systemd, &[] as &[&str]);
// let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::SystemdUser, &[] as &[&str]);

// Or use systemd system service
// let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::SystemdSystem, &[] as &[&str]);

// enable the auto launch
auto.enable().is_ok();
Expand All @@ -70,7 +74,8 @@ fn main() {
### macOS

macOS supports three ways to achieve auto launch:
- **Launch Agent**: Uses plist files in `~/Library/LaunchAgents/` (default)
- **Launch Agent (user)**: Uses plist files in `~/Library/LaunchAgents/` (default)
- **Launch Agent (system)**: Uses plist files in `/Library/LaunchAgents/`
- **AppleScript**: Uses AppleScript to add login items
- **SMAppService**: Uses the SMAppService API (macOS 13+)

Expand All @@ -88,8 +93,11 @@ fn main() {
let app_name = "the-app";
let app_path = "/path/to/the-app.app";

// Use Launch Agent (default method)
let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::LaunchAgent, &[] as &[&str], &[] as &[&str], "");
// Use Launch Agent for current user (default method)
let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::LaunchAgentUser, &[] as &[&str], &[] as &[&str], "");

// Or use Launch Agent for all users
// let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::LaunchAgentSystem, &[] as &[&str], &[] as &[&str], "");

// Or use AppleScript
// let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::AppleScript, &[] as &[&str], &[] as &[&str], "");
Expand Down
41 changes: 29 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! 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
//! // Use XDG Autostart by default, or use LinuxLaunchMode::SystemdUser for systemd
//! let auto = AutoLaunch::new(app_name, app_path, LinuxLaunchMode::XdgAutostart, args);
//!
//! // enable the auto launch
Expand All @@ -32,8 +32,9 @@
//!
//! ### macOS
//!
//! macOS supports two ways to achieve auto launch:
//! - **Launch Agent**: Uses plist files in `~/Library/LaunchAgents/` (default)
//! macOS supports four ways to achieve auto launch:
//! - **Launch Agent (User)**: Uses plist files in `~/Library/LaunchAgents/` (default)
//! - **Launch Agent (System)**: Uses plist files in `/Library/LaunchAgents/`
//! - **AppleScript**: Uses AppleScript to add login items
//!
//! **Note**:
Expand All @@ -52,7 +53,7 @@
//! 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, "");
//! let auto = AutoLaunch::new(app_name, app_path, MacOSLaunchMode::LaunchAgentUser, args, bundle_identifiers, "");
//!
//! // enable the auto launch
//! auto.enable().is_ok();
Expand Down Expand Up @@ -109,7 +110,7 @@
//! let auto = AutoLaunchBuilder::new()
//! .set_app_name("the-app")
//! .set_app_path("/path/to/the-app")
//! .set_macos_launch_mode(MacOSLaunchMode::LaunchAgent)
//! .set_macos_launch_mode(MacOSLaunchMode::LaunchAgentUser)
//! .set_args(&["--minimized"])
//! .build()?;
//!
Expand Down Expand Up @@ -178,7 +179,7 @@ mod windows;
/// # use auto_launch::{AutoLaunch, MacOSLaunchMode};
/// # let app_name = "the-app";
/// # let app_path = "/path/to/the-app";
/// # let launch_mode = MacOSLaunchMode::LaunchAgent;
/// # let launch_mode = MacOSLaunchMode::LaunchAgentUser;
/// # let args = &["--minimized"];
/// # let bundle_identifiers = &["com.github.auto-launch-test"];
/// AutoLaunch::new(app_name, app_path, launch_mode, args, bundle_identifiers, "");
Expand Down Expand Up @@ -214,7 +215,7 @@ pub struct AutoLaunch {
pub(crate) launch_mode: LinuxLaunchMode,

#[cfg(target_os = "macos")]
/// Launch mode for macOS (Launch Agent or AppleScript)
/// Launch mode for macOS (Launch Agent, AppleScript, or SMAppService)
pub(crate) launch_mode: MacOSLaunchMode,

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -280,7 +281,7 @@ impl AutoLaunch {
/// let auto = AutoLaunchBuilder::new()
/// .set_app_name("the-app")
/// .set_app_path("/path/to/the-app")
/// .set_macos_launch_mode(MacOSLaunchMode::LaunchAgent)
/// .set_macos_launch_mode(MacOSLaunchMode::LaunchAgentUser)
/// .set_args(&["--minimized"])
/// .build()?;
///
Expand Down Expand Up @@ -316,7 +317,9 @@ pub enum LinuxLaunchMode {
/// Use XDG Autostart (.desktop file in ~/.config/autostart/)
XdgAutostart,
/// Use systemd user service (~/.config/systemd/user/)
Systemd,
SystemdUser,
/// Use systemd system service (/etc/systemd/system/)
SystemdSystem,
}

impl Default for LinuxLaunchMode {
Expand All @@ -325,11 +328,19 @@ impl Default for LinuxLaunchMode {
}
}

impl LinuxLaunchMode {
#[deprecated(since = "0.6.0", note = "Use `LinuxLaunchMode::SystemdUser` instead")]
#[allow(non_upper_case_globals)]
pub const Systemd: Self = Self::SystemdUser;
}

/// Determines how the auto launch is enabled on macOS.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MacOSLaunchMode {
/// Use Launch Agent (plist file in ~/Library/LaunchAgents/)
LaunchAgent,
LaunchAgentUser,
/// Use Launch Agent (plist file in /Library/LaunchAgents/)
LaunchAgentSystem,
/// Use AppleScript to add login item
AppleScript,
/// User SMAppService API to enable the auto launch (macOS 13+)
Expand All @@ -338,10 +349,16 @@ pub enum MacOSLaunchMode {

impl Default for MacOSLaunchMode {
fn default() -> Self {
Self::LaunchAgent
Self::LaunchAgentUser
}
}

impl MacOSLaunchMode {
#[deprecated(since = "0.6.0", note = "Use `MacOSLaunchMode::LaunchAgentUser` instead")]
#[allow(non_upper_case_globals)]
pub const LaunchAgent: Self = Self::LaunchAgentUser;
}

/// Determines how the auto launch is enabled on Windows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowsEnableMode {
Expand Down Expand Up @@ -389,7 +406,7 @@ impl AutoLaunchBuilder {
#[deprecated(since = "0.6.0", note = "Use `set_macos_launch_mode` instead")]
pub fn set_use_launch_agent(&mut self, use_launch_agent: bool) -> &mut Self {
self.macos_launch_mode = if use_launch_agent {
MacOSLaunchMode::LaunchAgent
MacOSLaunchMode::LaunchAgentUser
} else {
MacOSLaunchMode::AppleScript
};
Expand Down
Loading
Loading