-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add "Launch at login" item for macOS 13+
- Loading branch information
1 parent
44c880d
commit d313af1
Showing
5 changed files
with
75 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Cocoa | ||
import ServiceManagement | ||
|
||
@available(macOS 13.0, *) | ||
final class LaunchAtLogin { | ||
static let shared = LaunchAtLogin() | ||
private(set) var menuItem: NSMenuItem | ||
|
||
private init() { | ||
menuItem = NSMenuItem(title: "Launch at login", action: #selector(LaunchAtLogin.toggle), keyEquivalent: "") | ||
menuItem.target = self | ||
menuItem.state = state | ||
} | ||
|
||
private func set(_ launch: Bool) { | ||
if launch { | ||
if SMAppService.mainApp.status == .enabled { | ||
try? SMAppService.mainApp.unregister() | ||
} | ||
try? SMAppService.mainApp.register() | ||
} else { | ||
try? SMAppService.mainApp.unregister() | ||
} | ||
} | ||
|
||
private var state: NSControl.StateValue { | ||
switch SMAppService.mainApp.status { | ||
case .notRegistered: return .off | ||
case .enabled: return .on | ||
case .requiresApproval: return .mixed | ||
case .notFound: return .off | ||
@unknown default: return .off | ||
} | ||
} | ||
|
||
@objc | ||
func toggle() { | ||
switch state { | ||
case .on: set(false) | ||
case .off: set(true) | ||
default: SMAppService.openSystemSettingsLoginItems() | ||
} | ||
menuItem.state = state | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters