copy = function (text, callback) { var child = spawn(config.copy.command, config.copy.args); var done = (callback ? function() { callback.apply(this, arguments); done = noop; } : noop); var err = []; child.stdin.on("error", function (err) { done(err); }); child .on("exit", function() { done(null, text); }) .on("error", function(err) { done(err); }) .stderr .on("data", function(chunk) { err.push(chunk); }) .on("end", function() { if(err.length === 0) { return; } done(new Error(config.decode(err))); }) ; if(text.pipe) { text.pipe(child.stdin); } else { var output, type = Object.prototype.toString.call(text); if(type === "[object String]") { output = text; } else if(type === "[object Object]") { output = util.inspect(text, { depth: null }); } else if(type === "[object Array]") { output = util.inspect(text, { depth: null }); } else { output = text.toString(); } child.stdin.end(config.encode(output)); } return text; }
...
- `require("copy-paste").global()`: adds `copy` and `paste` to the global namespace. Returns an object with `copy` and
`paste` as properties.
## Example
```js
var ncp = require("copy-paste");
ncp.copy('some text', function () {
// complete...
})
```
## Getting node-copy-paste
The easiest way to get node-copy-paste is with [npm](http://npmjs.org/):
...
global = function () { GLOBAL.copy = exports.copy; GLOBAL.paste = exports.paste; return exports; }
...
When `require("copy-paste")` is executed, an object with the following properties is returned:
- `copy(text[, callback])`: asynchronously replaces the current contents of the clip board with `text`. Takes either a string,
array, object, or readable stream. Returns the same value passed in. Optional callback will fire when the copy operation is complete
.
- `paste([callback])`: if no callback is provided, `paste` synchronously returns the current contents of the system clip board.
Otherwise, the contents of the system clip board are passed to the callback as the second parameter.
**Note**: The synchronous version of `paste` is not always availabled. Unfortunately, I'm having a hard time finding a synchronous
version of `child_process.exec` that consistently works on all platforms, especially windows. An error message is shown if the
synchronous version of `paste` is used on an unsupported platform. That said, the asynchronous version of `paste` is always available
.
- `require("copy-paste").global()`: adds `copy` and `paste` to the global
namespace. Returns an object with `copy` and `paste` as properties.
## Example
```js
var ncp = require("copy-paste");
ncp.copy('some text', function () {
...
noConflict = function () { throw new Error("DEPRECATED: copy-paste no longer adds global variables by default."); }
n/a
paste = function (callback) { if(execSync && !callback) { return config.decode(execSync(pasteCommand)); } else if(callback) { var child = spawn(config.paste.command, config.paste.args); var done = callback && function() { callback.apply(this, arguments); done = noop; }; var data = [], err = []; child.on("error", function(err) { done(err); }); child.stdout .on("data", function(chunk) { data.push(chunk); }) .on("end", function() { done(null, config.decode(data)); }) ; child.stderr .on("data", function(chunk) { err.push(chunk); }) .on("end", function() { if(err.length === 0) { return; } done(new Error(config.decode(err))); }) ; } else { throw new Error("A synchronous version of paste is not supported on this platform."); } }
n/a
silent = function () { throw new Error("DEPRECATED: copy-paste is now always silent."); }
n/a
decode = function (chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }
...
child
.on("exit", function() { done(null, text); })
.on("error", function(err) { done(err); })
.stderr
.on("data", function(chunk) { err.push(chunk); })
.on("end", function() {
if(err.length === 0) { return; }
done(new Error(config.decode(err)));
})
;
if(text.pipe) { text.pipe(child.stdin); }
else {
var output, type = Object.prototype.toString.call(text);
...
encode = function (str) { return new Buffer(str, "utf8"); }
...
var output, type = Object.prototype.toString.call(text);
if(type === "[object String]") { output = text; }
else if(type === "[object Object]") { output = util.inspect(text, { depth: null }); }
else if(type === "[object Array]") { output = util.inspect(text, { depth: null }); }
else { output = text.toString(); }
child.stdin.end(config.encode(output));
}
return text;
};
var pasteCommand = [ config.paste.command ].concat(config.paste.args).join(" ");
exports.paste = function(callback) {
...
decode = function (chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }
...
child
.on("exit", function() { done(null, text); })
.on("error", function(err) { done(err); })
.stderr
.on("data", function(chunk) { err.push(chunk); })
.on("end", function() {
if(err.length === 0) { return; }
done(new Error(config.decode(err)));
})
;
if(text.pipe) { text.pipe(child.stdin); }
else {
var output, type = Object.prototype.toString.call(text);
...
encode = function (str) { return new Buffer(str, "utf8"); }
...
var output, type = Object.prototype.toString.call(text);
if(type === "[object String]") { output = text; }
else if(type === "[object Object]") { output = util.inspect(text, { depth: null }); }
else if(type === "[object Array]") { output = util.inspect(text, { depth: null }); }
else { output = text.toString(); }
child.stdin.end(config.encode(output));
}
return text;
};
var pasteCommand = [ config.paste.command ].concat(config.paste.args).join(" ");
exports.paste = function(callback) {
...
decode = function (chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } var b64 = iconv.decode(Buffer.concat(chunks), "cp437"); b64 = b64.substr(0, b64.length - 2); // Chops off extra "\r\n" // remove bom and decode var result = new Buffer(b64, "base64").slice(3).toString("utf-8"); return result; }
...
child
.on("exit", function() { done(null, text); })
.on("error", function(err) { done(err); })
.stderr
.on("data", function(chunk) { err.push(chunk); })
.on("end", function() {
if(err.length === 0) { return; }
done(new Error(config.decode(err)));
})
;
if(text.pipe) { text.pipe(child.stdin); }
else {
var output, type = Object.prototype.toString.call(text);
...
encode = function (str) { return iconv.encode(str, "utf16le"); }
...
var output, type = Object.prototype.toString.call(text);
if(type === "[object String]") { output = text; }
else if(type === "[object Object]") { output = util.inspect(text, { depth: null }); }
else if(type === "[object Array]") { output = util.inspect(text, { depth: null }); }
else { output = text.toString(); }
child.stdin.end(config.encode(output));
}
return text;
};
var pasteCommand = [ config.paste.command ].concat(config.paste.args).join(" ");
exports.paste = function(callback) {
...