Skip to content

Instantly share code, notes, and snippets.

@hacker-bastl
Created February 5, 2018 20:10
Show Gist options
  • Save hacker-bastl/c5cfe46c8223b6009425bf610d9f2e9f to your computer and use it in GitHub Desktop.
Save hacker-bastl/c5cfe46c8223b6009425bf610d9f2e9f to your computer and use it in GitHub Desktop.
battery level audio output on Mac
#!/usr/bin/env node
// https://nodejs.org/api/child_process.html
const child_process = require('child_process');
child_process.spawn('/usr/bin/pmset', ['-g', 'pslog'])
.stdout.on('data', function(chunk) {
// parse battery info from pmset output stream
var verbose_output = [/(\d+%)/, /(\d+:\d+)\ remaining/]
.filter(function(expression) {
// output contains battery info
return expression.test(chunk);
}).map(function(expression) {
// parse battery info
return expression.exec(chunk).shift().trim();
}).join(' - ');
// chunk is linebreak or similar
if (!verbose_output) return;
// log timestamp to console
var time_stamp = new Date().toJSON().replace(/[TZ]/g, ' ');
console.log(`${time_stamp}${verbose_output}`);
// audio output of bettery information
var audio_output = verbose_output
.split(/\ +/g).concat(['-v', 'Samantha']);
child_process.spawn('/usr/bin/say', audio_output);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment