Last active
March 22, 2017 07:21
-
-
Save ftiff/d3470f158aa219936dbf591b041c1342 to your computer and use it in GitHub Desktop.
get json from preferences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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