Search code examples
node.jsexeciterm2

How to exec script to set iterm2 Badge from nodejs?


I get this bash script from Iterm2 official site.

printf "\e]1337;SetBadgeFormat=%s\a" $(echo "text" | base64)

I tried exec like bellow, there is no error, but failed to set iterm2 Badge

var exec = require('child_process').exec;
exec('printf "\e]1337;SetBadgeFormat=%s\a" $(echo "text" | base64)');

Solution

  • setBadgeFormat.js =>

    #!/usr/bin/env node
    
    var rawBadgeFormat = 'test'
    var base64BadgeFormat = new Buffer(rawBadgeFormat).toString('base64')
    var setBadgeFormatCmd = 'printf "\\e]1337;SetBadgeFormat=' + base64BadgeFormat + '\\a"'
    require('child_process').exec(setBadgeFormatCmd, function(error, stdout, stderr) {
        if (error) console.log(error);
        process.stdout.write(stdout); // this line actually do the trick
        process.stderr.write(stderr);
    });