Skip to content
Open
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
12 changes: 12 additions & 0 deletions HeliPort/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
<dict/>
</dict>
</array>
<key>SMAppService</key>
<dict>
<key>LoginItems</key>
<array>
<dict>
<key>Identifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)-Launcher</string>
<key>Enabled</key>
<true />
</dict>
</array>
</dict>
<key>NSSupportsAutomaticTermination</key>
<true/>
<key>NSSupportsSuddenTermination</key>
Expand Down
38 changes: 28 additions & 10 deletions HeliPort/LoginItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,38 @@ class LoginItemManager {
private static let launcherId = Bundle.main.bundleIdentifier! + "-Launcher"

public class func isEnabled() -> Bool {
if #available(macOS 13.0, *) {
return SMAppService.mainApp.status == .enabled
} else {
guard let jobs =
(LoginItemManager.self as DeprecationWarningWorkaround.Type).jobsDict
else {
return false
}

guard let jobs =
(LoginItemManager.self as DeprecationWarningWorkaround.Type).jobsDict
else {
return false
}

let job = jobs.first { $0["Label"] as? String? == launcherId }
let job = jobs.first { $0["Label"] as? String? == launcherId }

return job?["OnDemand"] as? Bool ?? false
return job?["OnDemand"] as? Bool ?? false
}
}

public class func setStatus(enabled: Bool) {
SMLoginItemSetEnabled(launcherId as CFString, enabled)
if #available(macOS 13.0, *) {
do {
if enabled {
if SMAppService.mainApp.status == .enabled {
try? SMAppService.mainApp.unregister()
}
try SMAppService.mainApp.register()
} else {
try SMAppService.mainApp.unregister()
}
} catch {
Log.error("Failed to \(enabled ? "enable" : "disable") login item: \(error.localizedDescription)")
}
} else {
SMLoginItemSetEnabled(launcherId as CFString, enabled)
}
}
}

Expand All @@ -48,4 +66,4 @@ extension LoginItemManager: DeprecationWarningWorkaround {
static var jobsDict: [[String: AnyObject]]? {
SMCopyAllJobDictionaries(kSMDomainUserLaunchd)?.takeRetainedValue() as? [[String: AnyObject]]
}
}
}