最近,我热衷于在 OSX 默认 系统,因为它允许我将各种脚本保存在 git 中并将它们推送到 github,而不必担心将密码/设置/等硬编码到脚本中。
使用简单的 bash 命令编写 shell 脚本时,很容易使用反引号调用默认二进制文件来读取首选项,如果读取首选项时出错,脚本将停止执行,您可以看到错误并修复它。当我尝试在 Python 或 Ruby 中做类似的事情时,它往往会有点烦人,因为您必须做额外的工作来检查默认值的返回代码以查看是否存在错误。
我一直在尝试通过 google 断断续续地搜索一个使用 OSX 默认系统的库,当“defaults”是查询字符串的一部分时,结果会变得有些困难。
我想过尝试直接读取 plist 文件,但似乎我找到的 plist 库(例如内置的 python 库)只能读取 XML 库(而不是二进制库),如果我曾经这样做过,这就是一个问题使用默认程序设置任何内容,因为它会将其转换回二进制 plist。
最近,在尝试另一次搜索 Python 库时,我将搜索术语更改为使用 NSUserDefaults 之类的内容(我现在忘记了确切的术语),我发现了一个名为 userdefaults 但它是为旧版本的 OSX (10.2) 和旧版本的 Python (2.3) 开发的,我没有太多运气让它在 OSX 10.6 上编译, Python 2.6
理想情况下,我希望找到一个库,可以轻松地从 OSX 默认系统读取(并作为额外的写入),其方式类似于以下 python 伪代码。
from some.library.defaults import defaults
settings = defaults('com.example.app')
print settings['setting_key']
由于我也开始更多地使用 Ruby,因此我也想找到一个具有类似功能的 Ruby 库。
也许我最终不得不“放弃”并围绕默认二进制文件编写自己的简单库,但我认为尝试查询其他人以查看是否存在现有解决方案不会有什么坏处。
Recently I have become a fan of storing various settings used for my testing scripts in the OSX defaults system as it allows me to keep various scripts in git and push them to github without worrying about leaving passwords/settings/etc hardcoded into the script.
When writing a shell script using simple bash commands, it is easy enough to use backticks to call the defaults binary to read the preferences and if there is an error reading the preference, the script stops execution and you can see the error and fix it. When I try to do a similar thing in Python or Ruby it tends to be a little more annoying since you have to do additional work to check the return code of defaults to see if there is an error.
I have been attempting to search via google off and on for a library to use the OSX defaults system which ends up being somewhat difficult when "defaults" is part of your query string.
I thought of trying to read the plist files directly but it seems like the plist libraries I have found (such as the built in python one) are only able to read the XML ones (not the binary ones) which is a problem if I ever set anything with the defaults program since it will convert it back to a binary plist.
Recently while trying another search for a Python library I changed the search terms to something using something like NSUserDefaults (I have now forgotten the exact term) I found a Python library called userdefaults but it was developed for an older version of OSX (10.2) with an older version of Python (2.3) and I have not had much luck in getting it to compile on OSX 10.6 and Python 2.6
Ideally I would like to find a library that would make it easy to read from (and as a bonus write to) the OSX defaults system in a way similar to the following python psudo code.
from some.library.defaults import defaults
settings = defaults('com.example.app')
print settings['setting_key']
Since I am also starting to use Ruby more, I would also like to find a Ruby library with similar functionality.
It may be that I have to eventually just 'give up' and write my own simple library around the defaults binary but I thought it wouldn't hurt to try to query others to see if there was an existing solution.
发布评论
评论(1)
您需要使用 PyObjC:看看 这个mactech.com 上的文章(具体来说:向下滚动到“通过 Python 访问 plist”)。以及 PyObjC 上的 这篇来自 oreilly 的文章。
运行这个,例如:
You´ll want to use PyObjC: have a look at this article at mactech.com (in specific: scroll down to "Accessing plists Via Python"). And this article from oreilly on PyObjC.
Run this, for example: