Created
June 10, 2011 11:36
-
-
Save sfoster/1018660 to your computer and use it in GitHub Desktop.
Screenshot sequence w. node.js and screencapture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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