JXA iTerm Automation - Wait for command to finish

285 views Asked by At

I'm trying to use JXA to automated iTerm.

I want to compile some UI code in "watch mode" in the first tab. After the library is compiled the first time (and a dist/ folder is created), I want a second tab to open and run some other code which is dependant on the output of the first. In other words, I need the second tab to wait for the first to finish.

As far as JXA automation of iTerm, I cannot find any way for one tab to wait for another.

Every example I've found involves opening several tabs and launching parallel processes in these tabs.

Here's an example

The best I could think of it using some form of setInterval and checking iTerm's session.isProcessing (which I found in the docs through Script Editor > Window > Library), but even that just gives some weird errors as if isProcessing is not supposed to be read:

Error: Can't convert types

    function timer(repeats, func, delay) {
        const args = Array.prototype.slice.call(arguments, 2, -1);
        args.unshift(this);

        const boundFunc = func.bind.apply(func, args);
        const operation = $.NSBlockOperation.blockOperationWithBlock(boundFunc);
        const timer = $.NSTimer.timerWithTimeIntervalTargetSelectorUserInfoRepeats(
            delay / 1000, operation, 'main', null, repeats
        );
        $.NSRunLoop.currentRunLoop.addTimerForMode(timer, "timer");
        return timer;
    }

    function invalidate(timeoutID) {
        timeoutID.invalidate
    }

    const setTimeout = timer.bind(undefined, false)
    const setInterval = timer.bind(undefined, true)
    const clearTimeout = invalidate
    const clearInterval = invalidate

    const terminal = Application('iTerm')
    terminal.includeStandardAdditions = true

    const window = terminal.currentWindow()
    let tab = window.currentTab()
    let session = tab.currentSession();

    setInterval(function() {
        console.log('isProcessing', session.isProcessing);
    }, 500);

    session.write({ text: 'ng build my-angular-lib --watch' });

    //WAIT HERE

    let tab2 = window.createTabWithDefaultProfile();
    let session2 = tab2.currentSession();
    session2.write({ text: 'npm link my-angular-lib && ng serve' });
1

There are 1 answers

0
Menelaos On

What I would do is to debug the full structure of session and tab. Maybe the information is there under a different field.

I would try to serialize/stringify the whole object to JSON and print in on the console.

Something like:

console.log(JSON.stringify(session) )