【问题标题】:Dashboard widget: getting version number from Info.plist仪表板小部件:从 Info.plist 获取版本号
【发布时间】:2012-05-11 09:20:57
【问题描述】:
我正在用 Dashcode 编写一个仪表板小部件,在背面,我有一个积分字符串。我想在该字符串中包含小部件的版本号,但如果可能的话,我想以编程方式从 Info.plist 中的 CFBundleVersion 或 CFBundleShortVersionString 键中获取它,以避免在当我更新小部件。
到目前为止,在 Apple 的 developer documentation、Google 和各种论坛上的搜索都没有结果。我想知道的是是否有一种内置的方法可以做到这一点,Apple 包括但忘了提及(如var version = widget.version(); 或其他东西),或者我的脚本是否必须在采摘之前拉入并解析整个 plist出我真正想要的一个值。
感谢您提供的任何帮助!
【问题讨论】:
标签:
javascript
macos
widget
dashboard
dashcode
【解决方案1】:
我似乎找到了答案:使用 Dashcode 的“数据源”工具来读取 Info.plist 作为 XML 数据源。从那里,this blog post 向我展示了如何遍历 plist 的结构并获取正确的字符串(在这种情况下,文件中的第五个 <string> 元素,对应于 CFBundleShortVersionString。
我最终得到的功能:
function getWidgetVersion() {
var dataSource = dashcode.getDataSource("infoPlist");
var version = dataSource.selection().valueForKey("dict").valueForKey("string")[4]; // This line and the previous could probably be combined for the sake of brevity
if (typeof(version) == 'string') {
document.getElementById("creditsLabel").innerHTML += version; //I'll change this to just pass the number on
}
}
由于creditsLabel div 的文本已经以本地化字符串开头,我得到一个漂亮的小标签,上面写着“1.0 版”。