modifying a Plist from command line on Mac using Defaults

送分小仙女□ 提交于 2019-12-20 08:40:48

问题


Does any one know how to modify a Plist file from command line using defaults? Currently there are two Dictionaries under the URL types array; I need to add another.

Every command i've tried have either replaced the entire dictionary, or created a new array called URL types instead of editing it. Any ideas of how this can be done in defaults (the console Mac app) and not PlistBuddy?


回答1:


Open the Info.plist in a text editor to see the actual identifiers.

defaults write Absolute/Path/to/Info.plist CFBundleURLTypes -array-add '<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLName</key>
<string>Mac App Store URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>macappstore</string>
</array>
</dict>'

pbpaste | pl converts the XML to the old-style format.

defaults write Info.plist CFBundleURLTypes -array-add '{CFBundleTypeRole=Viewer; FBundleURLName="Mac App Store URL";CFBundleURLSchemes=(macappstore);}'




回答2:


XML property lists can be viewed in a text editor directly as Lauri's answer above suggests.

Binary property lists (found in many of Apple's own shipping applications) need to be converted to an XML property list format first.

plutil may be used to do this, in either direction. Take care though as the property list is modified in place, so you make wish to make a copy of the property list first.

plutil -convert xml1 binary-property-list-to-convert.plist

And to convert it back to binary:

plutil -convert binary1 XML-property-list-to-convert.plist



回答3:


OSX has PlistBuddy, which makes this a lot simpler.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html

See also: https://github.com/kevinSuttle/OSXDefaults/blob/master/REFERENCE.md




回答4:


Use the -array-add value type:

defaults write /path/to/plist/file "URL Types" -array-add '{"URL Identifier" = "com.myapp.2"; "URL Schemes" = { "two"; }; }'


来源:https://stackoverflow.com/questions/13740337/modifying-a-plist-from-command-line-on-mac-using-defaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!