質問

I'm writing a Dashboard widget in Dashcode, and on the back side, I've got a string for credits. 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.

Searches on Apple's developer documentation, Google and various forums have proven fruitless so far. 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.

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

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".

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top