Skip to content

Instantly share code, notes, and snippets.

@chilcote
Created June 23, 2022 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilcote/5d53239ae485c20418e4027fa365619d to your computer and use it in GitHub Desktop.
Save chilcote/5d53239ae485c20418e4027fa365619d to your computer and use it in GitHub Desktop.
Use this to list out all the launchd plists in LaunchDaemons and LaunchAgents, and report the SMAppService.statusForLegacyPlist status.
import Foundation
import ServiceManagement
func printStatus(plistPath: String) -> String {
let plistURL = URL(fileURLWithPath: plistPath)
let status = SMAppService.statusForLegacyPlist(at: plistURL)
switch status {
case .notRegistered:
return plistPath + " is not registered"
case .enabled:
return plistPath + " is enabled"
case .requiresApproval:
return plistPath + " is requires approval"
case .notFound:
return plistPath + " is not found"
@unknown default:
return plistPath + " is unknown"
}
}
let localFileManager = FileManager()
let daemonsDir = "/Library/LaunchDaemons"
let daemonsEnum = localFileManager.enumerator(atPath: daemonsDir)
let agentsDir = "/Library/LaunchAgents"
let agentsEnum = localFileManager.enumerator(atPath: agentsDir)
print("LaunchDaemons:")
while let file = daemonsEnum?.nextObject() as? String {
if file.hasSuffix(".plist") {
// print(daemonsDir.appending("/\(file)"))
let status = printStatus(plistPath: daemonsDir.appending("/\(file)"))
print(status)
}
}
print("LaunchAgents:")
while let file = agentsEnum?.nextObject() as? String {
if file.hasSuffix(".plist") {
// print(agentsDir.appending("/\(file)"))
let status = printStatus(plistPath: agentsDir.appending("/\(file)"))
print(status)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment