我如何解析OS X上的 "默认读取 "的输出?defaults read
终端命令?
它似乎输出的是 "旧的 "NeXTSTEP plist格式;看起来像。
{
"Apple Global Domain" = {
AppleAntiAliasingThreshold = 4;
AppleCollationOrder = root;
我试着把输出写到一个文件里,然后用... plutil
但是,它窒息。
> defaults read > defaults.txt
> plutil -convert xml1 defaults.txt
2014-02-02 21:29:14.856 plutil[56896:707] CFPropertyListCreateFromXMLData(): Old-style
plist parser: missing semicolon in dictionary on line 10835. Parsing will be abandoned.
Break on _CFPropertyListMissingSemicolon to debug.
defaults.txt: Property List error: Unexpected character { at line 1 / JSON error: No
value for key in object around character 28.
我想把默认值存储在git中,这样我就可以在应用更改后保留一个更改设置和差异的记录,但似乎序列化的过程中的 defaults read
是不'行序稳定'的:字典不会按照一致的顺序转储它们的键,导致大量的噪音。defaults read
然后,我可以通过一个顺序一致的序列器将数据管出来。
你是个幸运的家伙,就在几天前,有人在PyPi上发布了一个NeXTSTEP plist格式的解析器----。nsplist.
这里是一个直接读取默认值的脚本,而不是解析defaults命令的输出。
#!/usr/bin/swift
import Foundation
var args = CommandLine.arguments
args.removeFirst()
if let suite = args.first, let defaults = UserDefaults(suiteName: suite) {
args.removeFirst()
if let keyPath = args.first, let value = defaults.value(forKeyPath: keyPath) {
print(value)
} else {
print(defaults.dictionaryRepresentation())
}
}
如果你把这个文件保存为一个叫 dread
, chmod +x dread
然后你可以读取一个应用程序的默认值,并像这样打印出来。
dread com.apple.dt.Xcode IDEApplicationwideBuildSettings
因为这是一个字典,你可以使用字典中的一个键来读取。
dread com.apple.dt.Xcode IDEApplicationwideBuildSettings.SYMROOT
以此类推
你有没有试过 defaults export
? 这将输出一个二进制文件,然后可以使用plutil将其转换为.plist。