Skip to content

Instantly share code, notes, and snippets.

@ftiff
Last active March 22, 2017 07:21
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 ftiff/d3470f158aa219936dbf591b041c1342 to your computer and use it in GitHub Desktop.
Save ftiff/d3470f158aa219936dbf591b041c1342 to your computer and use it in GitHub Desktop.
get json from preferences
//
// main.swift
// prefs
//
// Created by Francois Levaux on 21.03.17.
// Copyright © 2017 Francois Levaux. All rights reserved.
//
// Similar to $ plutil -convert json /Library/Preferences/io.fti.SplashBuddy.plist -o - -r
import Foundation
let app = "io.fti.SplashBuddy"
func preferencesToDict(array: CFArray) -> [Dictionary<String, Any>] {
return lookupPreferencesValues(array: (array as! Array<String>))
}
func lookupPreferencesValues(array: Array<String>) -> [Dictionary<String, Any>] {
return array.map({[$0: CFPreferencesCopyAppValue($0 as CFString, app as CFString) as Any]})
}
guard let prefs = CFPreferencesCopyKeyList(app as CFString,
kCFPreferencesAnyUser,
kCFPreferencesAnyHost).map(preferencesToDict) else {
print("nil")
exit(1)
}
do {
guard JSONSerialization.isValidJSONObject(prefs) else {
print("Invalid object")
exit(1)
}
let json = try JSONSerialization.data(withJSONObject: prefs as Any, options: [.prettyPrinted])
if let jsonString = String(data: json, encoding: .utf8) {
print(jsonString)
}
} catch {
dump(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment