Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Build a Dashboard Widget
Subject:   It doesn't work
Date:   2005-06-14 11:33:30
From:   TimGrant
Response to: It doesn't work

What you're describing happened to me too. I made some changes to the JavaScript and now it works a little bit better:


function getManPage()
{
if (document.getElementById('programName').value != null)
{
var commandLine = 'groff -mandoc -Tascii -P-b -P-c `/usr/bin/man -w "' + document.getElementById('programName').value+'"`' + ' | col -b '
document.getElementById('outputArea').value = "Processing..."
var output = widget.system(commandLine, myEventHandler);
}
}


function myEventHandler(cmd)
{
document.getElementById('outputArea').value=cmd.outputString;
cmd.close
}


It still only works on some terms, though, e.g., "chown" but not "chmod".

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • It doesn't work
    2007-03-23 20:01:10  jeffclough [View]

    Some trial and error shows that the output control can only hold up to 366 lines of text. This seems like an odd number, so maybe it's different from system to system (or version to version or whatever). I just used the head command to truncate the output:


    if (document.getElementById('programName').value != null) {
    var commandLine =
    'groff -mandoc -Tascii -P-b -P-c `/usr/bin/man -w "'
    + document.getElementById('programName').value+'"`'
    + ' | head -366 | /bin/cat';
    var output = widget.system(commandLine, null);
    document.getElementById('outputArea').value = output.outputString;
    }
  • The Working JavaScript Function and Explanation of Error in Article
    2005-06-30 00:59:51  maymay [View]

    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)