42 lines
1.5 KiB
Swift
42 lines
1.5 KiB
Swift
|
|
import Foundation
|
||
|
|
|
||
|
|
struct LoginItemManager {
|
||
|
|
private static let plistPath: String = {
|
||
|
|
let home = FileManager.default.homeDirectoryForCurrentUser.path
|
||
|
|
return "\(home)/Library/LaunchAgents/com.grosfrumos.hudini.plist"
|
||
|
|
}()
|
||
|
|
|
||
|
|
static var isEnabled: Bool {
|
||
|
|
FileManager.default.fileExists(atPath: plistPath)
|
||
|
|
}
|
||
|
|
|
||
|
|
static func setEnabled(_ enabled: Bool, appPath: String? = nil) {
|
||
|
|
if enabled {
|
||
|
|
let path = appPath ?? Bundle.main.bundlePath
|
||
|
|
let plist = """
|
||
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||
|
|
<plist version="1.0">
|
||
|
|
<dict>
|
||
|
|
<key>Label</key>
|
||
|
|
<string>com.grosfrumos.hudini</string>
|
||
|
|
<key>ProgramArguments</key>
|
||
|
|
<array>
|
||
|
|
<string>open</string>
|
||
|
|
<string>\(path)</string>
|
||
|
|
</array>
|
||
|
|
<key>RunAtLoad</key>
|
||
|
|
<true/>
|
||
|
|
</dict>
|
||
|
|
</plist>
|
||
|
|
"""
|
||
|
|
// Ensure LaunchAgents dir exists
|
||
|
|
let dir = (plistPath as NSString).deletingLastPathComponent
|
||
|
|
try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)
|
||
|
|
try? plist.write(toFile: plistPath, atomically: true, encoding: .utf8)
|
||
|
|
} else {
|
||
|
|
try? FileManager.default.removeItem(atPath: plistPath)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|