Skip to content

Instantly share code, notes, and snippets.

@sfoster
Created June 10, 2011 11:36
Show Gist options
  • Save sfoster/1018660 to your computer and use it in GitHub Desktop.
Save sfoster/1018660 to your computer and use it in GitHub Desktop.
Screenshot sequence w. node.js and screencapture
(function(){
var sys = require('sys');
var filestem = process.ARGV.length > 2 ? process.ARGV.length[2] : "screen";
var spawn = require('child_process').spawn,
timer = null,
startTime, stopTime;
function outfile(d) {
d = d || new Date();
var timestr = (
d.toLocaleTimeString() +"-"+
d.getUTCMilliseconds()
).replace(/\W+/g, '.');
return __dirname + "/" + timestr + "_" + filestem + ".png";
}
function start() {
startTime = new Date();
stopTime = new Date(startTime.getTime() + 5000);
tick();
}
function tick(){
if(timer) {
clearTimeout(timer);
}
timer = setTimeout(function(){
var now = new Date();
var screencap = spawn("screencapture", ["-T", 0, outfile()]);
if(now < stopTime) {
tick();
} else {
console.log("done, ", now, stopTime);
}
}, 250);
}
function onCommand(cmd) {
var parts = cmd.toString().split(/\s+/);
var verb = parts.shift().toLowerCase().replace(/\s+^/gm, '');
switch(verb) {
case "start":
console.log("starting...");
start();
break;
case "startin":
var delay = Number(parts.shift());
if(isNaN(delay)) {
console.log("delay is not a number? " + delay);
} else {
console.log("starting in " + delay + " seconds");
setTimeout(function(){
start();
}, delay * 1000);
}
break;
case "stop":
console.log("stopping...");
timer && clearTimeout(timer);
break;
default:
console.log("huh? what is: " + cmd + " supposed to mean");
}
}
console.log("listening for commands (start|stop|startIn)");
process.stdin.resume();
process.stdin.on('data', onCommand);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment