[Widget] Getting version number from Info.plist

Discussion in 'Web Design and Development' started by Shunnabunich, Apr 25, 2012.

  1. Shunnabunich macrumors regular

    Shunnabunich

    Joined:
    Oct 30, 2005
    Location:
    Alberta, Canada
    #1
    Hi, folks! I take it this is the right forum for questions related to Dashboard widget development?

    I've been working on my first "real" widget (i.e. the first one that wasn't a prefab countdown widget) the past few days in Dashcode. I don't have a lot of experience with Javascript, or aptitude for coding in general, but I've been managing to scrape by with Google's help. This particular issue, though, is proving difficult to find information about.

    I have a "credits" label on the back of my widget. (I plan to add code to cycle through a series of strings each time it's clicked, "About This Mac"-style, to take advantage of the limited space, but that's unimportant here.) What I'd like to do is have my script pull the widget's version number from Info.plist and append it to the credits string, to save me having to update the version number in multiple places if and when I update the widget.

    I can't seem to find a "canon" way to do this, though — the documentation I've seen doesn't mention anything along the lines of simply calling something like widget.version, or having some sort of already-prepared object with the plist's contents ready-to-use. Does this mean I'll have to have the widget run the entire plist through an XML parser and then pick out the one value I actually want? Is there something I'm missing that provides a simpler way to accomplish this? Am I just plain overcomplicating things here? :)

    Thanks for any and all help you can provide this poor noob!
     
  2. Shunnabunich thread starter macrumors regular

    Shunnabunich

    Joined:
    Oct 30, 2005
    Location:
    Alberta, Canada
    #2
    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:


    Code:
    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 (DC.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".

    Hopefully this helps anyone else who wants to do this sort of thing!
     
  3. Shunnabunich thread starter macrumors regular

    Shunnabunich

    Joined:
    Oct 30, 2005
    Location:
    Alberta, Canada
    #3
    Just wanted to post an update on this, because the above method has recently been giving me errors for reasons I can't fathom. Instead of relying on Dashcode's built-in (and, of course, sparsely-documented) data sources feature, I decided to try doing it the way web pages usually do it: with an XMLHttpRequest (aka AJAX).

    Here's what I came up with:
    Code:
    function getWidgetVersion()
    {
    	xmlhttp = new XMLHttpRequest();
    	xmlhttp.open("GET", "Info.plist", false); // If that last argument is "true", which makes the request asynchronous, you'll probably end up trying to call getElementsByTagName() on a null value instead of XML, resulting in an error
    	xmlhttp.send();
    	infoPlist = xmlhttp.responseXML;
    	var ver = infoPlist.getElementsByTagName("string")[5].childNodes[0].nodeValue; // The sixth <string> element contains the value for CFBundleVersion; you could use the one for CFBundleShortVersionString instead if you wanted
    	return ver;
    }
     

Share This Page