Property List を手で書きたいときは JSON から変換する
Old-Style ASCII Property List は手で書くときにとても書きやすいが、表現力に制約があるので代替案を探した otofune.icon
ASCII property lists support the four primary property list data types: NSString, NSData, NSArray, and NSDictionary. The following sections describe the ASCII syntax for each of these types.
問題
アプリケーションの設定を保持するためのファイルとして使われているイメージ
設定を変更する際に、手で plist を書きたくなることがある
defaults コマンドで行うにしても、配列に NSObject を追加したくなると必須
どうやっても一発で -array-add しないと追加できないから
単に NSObject をセットするだけでも複数行に渡って object をちょこまか組み立てたくないから欲しくなる
plist フォーマットとして許容されているものは Old-Style ASCII, XML, Binary
しかし、最も簡単に手で書ける Old-Style ASCII 形式は、かなり古く表現力が追いついていない
Old-Style ASCII Property List は NSString, NSData, NSArray, NSDictionary のみをサポートしている
現行の Property List では加えて NSDate, NSNumber (bool, integer, float) をサポートしている
特によく使われる NSNumber のサポートがないのが痛い
どうする?
ただし NSDate が含まれると変換できないと思われるので、100% 互換があるわけではない
A Foundation object that may be converted to JSON must have the following properties:
The top level object is an NSArray or NSDictionary.
All objects are instances of NSString, NSNumber, NSArray, NSDictionary, or NSNull.
All dictionary keys are instances of NSString.
Numbers are not NaN or infinity.
このためだけにプログラムを書く必要はなくて plutil コマンドで相互変換が可能 標準入力から JSON を受けつけ、xml に変換して標準出力に流すなら次のように書ける
$ plutil -convert xml1 - -o -
詳しくは man plutil をチェック!
具体例
code:add-app-to-dock.sh
app_path=/Applications/Safari.app/
payload='{
"tile-data": {
"file-data": {
"_CFURLString": "file://'; payload+=$app_path; payload+='",
"_CFURLStringType": 15
}
}
}'
payload="$(echo -n $payload | plutil -convert xml1 - -o -)"
defaults write com.apple.dock persistent-apps -array-add "$payload"