Skip to content

Instantly share code, notes, and snippets.

@Teggy
Created August 10, 2012 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Teggy/3314596 to your computer and use it in GitHub Desktop.
Save Teggy/3314596 to your computer and use it in GitHub Desktop.
Chocolat mixin: Replace selection with current date (example code that captures the output of a spawned process)
/*!
* Replace selection with current date
* Copyright(c) 2012 Torsten Grust <torsten.grust@gmail.com>
*/
var spawn = require('child_process').spawn;
/**
* Hook up menu items.
*/
Hooks.addMenuItem('Text/Insert/Current Date', 'control-shift-d', function() {
var date = spawn('/bin/date', []),
out = '';
/* 'data' callback sends strings (not buffers) */
date.stdout.setEncoding('utf8');
date.stdout.on('data', function(data) {
out += data;
});
date.on('exit', function(code) {
Recipe.run(function(recipe) {
var sel = recipe.selection;
recipe.replaceTextInRange(sel, out.trimRight());
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment