secretive/Secretive/Controllers/LaunchAgentController.swift

31 lines
953 B
Swift
Raw Normal View History

2020-04-04 22:16:31 +00:00
import Foundation
import ServiceManagement
import AppKit
import OSLog
2020-04-04 22:16:31 +00:00
struct LaunchAgentController {
func install() -> Bool {
Logger().debug("Installing agent")
2020-04-04 22:16:31 +00:00
_ = setEnabled(false)
2020-09-22 06:12:50 +00:00
return setEnabled(true)
2020-04-04 22:16:31 +00:00
}
func forceLaunch() {
Logger().debug("Agent is not running, attempting to force launch")
let url = Bundle.main.bundleURL.appendingPathComponent("Contents/Library/LoginItems/SecretAgent.app")
NSWorkspace.shared.openApplication(at: url, configuration: NSWorkspace.OpenConfiguration()) { app, error in
if let error = error {
Logger().error("Error force launching \(error.localizedDescription)")
} else {
Logger().debug("Agent force launched")
}
}
}
2020-04-04 22:16:31 +00:00
private func setEnabled(_ enabled: Bool) -> Bool {
SMLoginItemSetEnabled("com.maxgoedjen.Secretive.SecretAgent" as CFString, enabled)
}
}