advertisement

Article:
  Build a Dashboard Widget
Subject:   The Working JavaScript Function and Explanation of Error in Article
Date:   2005-06-30 00:59:51
From:   maymay
Response to: It doesn't work

The reason the above code doesn't work (at least not on 10.4.1) is because the Widget will freeze, waiting for output, due to the call from widget.system. By specifying a handler, you're changing from synchronous operation to asynchronous operation.


These are the offending lines that need to change in the article:

var output = widget.system(commandLine, null);
document.getElementById('outputArea').value = output.outputString;


What's happening here is that the call to widget.system() is performed synchronously and thus makes the Widget wait for the output from the call to complete before continuing its own execution. Unfortunately, when this method is called synchronously (that is, when its second argument is null), one needs to specify a property of the method to capture, as explained in the Dashboard Programming Guide in the section on Command-Line Access, under Synchronous Operation.


As cited in the Guide: Running widget.system(commandLine, null) as shown above executes the comman, but any output is lost since you don’t specify that you want that information. To get its output, specify the outputString property and save it in a variable:


var output = widget.system(“/usr/bin/id -un”, null).outputString;


So learning from this example, the getManPage() JavaScript function's lines mentioned above should be changed to this and it will work:


var output = widget.system(commandLine, null).outputString;
document.getElementById('outputArea').value = output;


Additionally, it should be noted that according to Apple's introduction to developing a Dashboard widget, the Info.plist file is required to have a CFBundleDisplayName key as well as a CFBundleVersion key, but is not required to have a Width or a Height.


Best regards, and best of luck,
-Meitar Moscovitz
http://www.maymay.net/ (Personal site)