如何创建可以在OSX中编辑plist的脚本

A O*_*A O 2 macos scripting

所以我每天都在不断手动修改plist(我在OSX 10.9上),我决定创建一个脚本来自动执行此操作.

经过一些研究,我发现我可以创建一个.command文件来在双击文件上执行命令.

现在,我不确定我是如何检查一个字符串的plist,如果找到它,如何替换它.有什么建议?

提前致谢!

Gor*_*son 5

这里有两个相关的命令:defaults/usr/libexec/PlistBuddy.defaults实际上是为管理首选项设置而设置的,PlistBuddy而是一个更通用的plist工具.例如,要检查并设置停靠方向(它出现在屏幕的哪一侧),您可以使用:

oldOrientation=$(defaults read com.apple.dock orientation)
defaults write com.apple.dock orientation "$newOrientation"
Run Code Online (Sandbox Code Playgroud)

PlistBuddy版本:

oldOrientation=$(/usr/libexec/PlistBuddy -c "print :orientation" ~/Library/Preferences/com.apple.dock.plist)
/usr/libexec/PlistBuddy -c "set :orientation '$newOrientation'" ~/Library/Preferences/com.apple.dock.plist
Run Code Online (Sandbox Code Playgroud)

正如这个例子所示,PlistBuddy语法更笨拙,更冗长......但如果你想做一些非常重要的事情,它的功能也会更强大.

  • 虽然defaults可以给出不在〜/ Library/Preferences文件夹中的plist文件的完整路径,但它总是将".plist"附加到文件名,因此如果您正在处理没有该扩展名的文件,defaults出去了.
  • PlistBuddy可以轻松深入到plist文件中的数组和字典; 例如,要获取Dock中右侧第一个永久条目的URL,您只需参考:persistent-others:0:tile-data:file-data:_CFURLString.(注意,数组是0索引的,:persistent-others:0第一个元素:persistent-others:1也是第二个,等等).像这样的东西:

    fileURL=$(/usr/libexec/PlistBuddy -c "print :persistent-others:0:tile-data:file-data:_CFURLString" ~/Library/Preferences/com.apple.dock.plist)
    
    Run Code Online (Sandbox Code Playgroud)

    ......那并不是真的defaults可以做的事情.