简体   繁体   English

仪表板小部件:从Info.plist获取版本号

[英]Dashboard widget: getting version number from Info.plist

I'm writing a Dashboard widget in Dashcode, and on the back side, I've got a string for credits. 我正在用Dashcode编写一个Dashboard小部件,在背面,我有一个积分字符串。 I want to include the widget's version number in that string, but if possible, I want to programmatically grab it from the CFBundleVersion or CFBundleShortVersionString key in Info.plist to avoid having to change the number in multiple places if and when I update the widget. 我想在该字符串中包含小部件的版本号,但如果可能的话,我想以编程方式从Info.plist中的CFBundleVersionCFBundleShortVersionString键中获取它,以避免在更新小部件时必须在多个位置更改编号。

Searches on Apple's developer documentation , Google and various forums have proven fruitless so far. 到目前为止,在Apple 开发人员文档 ,Google和各种论坛上的搜索都被证明是徒劳的。 What I'd like to know is whether there's a built-in way to do this that Apple included but forgot to mention (like var version = widget.version(); or something), or whether my script will have to pull in and parse the entire plist before plucking out the one value I actually want. 我想知道的是,Apple是否提供了一种内置的方法来执行此操作,但忘了提及(例如var version = widget.version();或其他内容),或者我的脚本是否必须插入并在提取我真正想要的一个值之前,分析整个plist。

Thanks for any help you can provide! 感谢您的任何帮助,您可以提供!

I seem to have found the answer: use Dashcode's "data source" facility to read in Info.plist as an XML data source. 我似乎已经找到了答案:使用Dashcode的“数据源”功能将Info.plist读取为XML数据源。 From there, this blog post showed me how to traverse the plist's structure and get the correct string (in this case, the fifth <string> element in the file, corresponding to CFBundleShortVersionString . 从那里开始, 这篇博客文章向我展示了如何遍历plist的结构并获取正确的字符串(在这种情况下,该文件中的第五个<string>元素对应于CFBundleShortVersionString

The function I ended up with: 我最终得到的功能是:

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
    }
}

Since the text of the creditsLabel div has already been started off with a localized string, I get a nice little label saying "Version 1.0". 由于creditsLabel div的文本已经以本地化字符串开始,因此我得到了一个很好的小标签,表示“ 1.0版”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM