SCRIPTONIT documentation   © 2021, Denes Kellner
Capturing shell output

There are two ways to capture console outputs. The first one is for short operations that take practically no time, like a dir command within one folder. For more time-consuming operations, you have to do a little more (but it's still very simple) - start capturing and provide two callbacks: one for following the progress as it goes, and another to be called when the operation has finished. This is an asynchronous way so you can go on doing other jobs & reacting on user interface; you can also stop the running process prematurely.

Let's see them in detail!


    Here's a short example that captures a directory list from the MS-DOS shell:

    let x ="dir d:\\projects\\wendy");
    Now x will look like:
     Volume in drive D is Data Drive
     Volume Serial Number is BE32-C92B
     Directory of d:\projects\wendy
    2018-06-02  01:30    <DIR>          .
    2018-06-02  01:30    <DIR>          ..
    2018-05-31  18:35    <DIR>          BlackPanel
    2018-05-30  19:43    <DIR>          Hollister
    2018-05-30  19:54    <DIR>          Coke
    2021-06-15  11:09    <DIR>          TrainWreck
    2018-04-11  00:03    <DIR>          QuickNote
    2018-05-17  09:45    <DIR>          SilentRunner
    2018-05-19  14:52            44,911 BrowserTester.exe
                   1 File(s)          44,911 bytes
                   8 Dir(s)  766,320,148,096 bytes free

    It's a normal multiline string, it's immediately available as a return value, can't be more convenient. But make sure you don't run into edge cases; some operations can go wild when used without appropriate care. A typical example would be a dir /s which, if launched in your main volume root, lists literally every file on the drive and gives you the impression that your app is frozen (while eating up half of your CPU in the meantime). So, simplicity comes at a low but inevitable price: make sure what you ask for will finish fast. Users can tolerate a little lag but don't test their patience.


    Again, let's start with the example:

    var collectedOutput = "";
        "dir d:\\projects\\wendy /s",
        function(x) { collectedOutput += x; },
        function() { doSomethingWith(collectedOutput); }

    For the sake of brevity, we used very simple functions here, practically the minimum that's still viable. The first argument is a command, obviously; the second is the callback function for following the progress (it gets called many times while the process is running), and the third one is where we already know it's over.

    The follow callback has a string argument input: you can see it's declared as function(x) {...}, where x always gets the latest piece of console output as it happens. If you just stitch them together as a string, you get the full output at the end of the day. Here in the example, our variable collectedOutput gets all the pieces and that's what we use in the finish callback function, giving it to a hypothetical doSomethingWith function.

    You can see a nice example of this mechanism in FolderSizes demo application.