package/package.json000644 000765 000024 0000001240 12727053200013010 0ustar00000000 000000 { "name": "copy-paste" , "version": "1.3.0" , "description": "A command line utility that allows read/write (i.e copy/paste) access to the system clipboard." , "keywords": [ "copy", "paste", "copy and paste", "clipboard" ] , "maintainers": [ { "name": "Xavi" , "email": "xavi.rmz@gmail.com" , "web": "http://xavi.co" } ] , "main": "./index.js" , "repository": { "type": "git" , "url": "https://github.com/xavi-/node-copy-paste" } , "dependencies": { "iconv-lite": "^0.4.8" } , "optionalDependencies": { "sync-exec": "~0.6.x" } , "devDependencies": { "mocha": "*", "should": ">=8.2.1" } , "scripts": { "test": "node_modules/.bin/mocha -w" } } package/.npmignore000644 000765 000024 0000000026 12302661246012526 0ustar00000000 000000 .DS_Store node_modulespackage/README.md000644 000765 000024 0000004661 12710306454012017 0ustar00000000 000000 # node-copy-paste A command line utility that allows read/write (i.e copy/paste) access to the system clipboard. It does this by wrapping [`pbcopy/pbpaste`](https://developer.apple.com/library/mac/#documentation/Darwin/Reference/Manpages/man1/pbcopy.1.html) (for OSX), [`xclip`](http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/) (for Linux and OpenBSD), and [`clip`](http://www.labnol.org/software/tutorials/copy-dos-command-line-output-clipboard-clip-exe/2506/) (for Windows). Currently works with node.js v0.8+. ## The API 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 () { // complete... }) ``` ## Getting node-copy-paste The easiest way to get node-copy-paste is with [npm](http://npmjs.org/): npm install -g copy-paste Alternatively you can clone this git repository: git clone git://github.com/xavi-/node-copy-paste.git ## Future plans I'm hoping to add various fallbacks for instances when `xclip` or `clip` is not avaiable (see [experimental-fallbacks](https://github.com/xavi-/node-copy-paste/tree/experimental-fallbacks/platform) branch). Also this library needs to be more thoroughly tested on windows. ## Developed by * Xavi Ramirez ## License This project is released under [The MIT License](http://www.opensource.org/licenses/mit-license.php). package/index.js000644 000765 000024 0000006151 12727053100012174 0ustar00000000 000000 var child_process = require("child_process"); var spawn = child_process.spawn; var util = require("util"); var execSync = (function() { if(child_process.execSync) { // Use native execSync if avaiable return function(cmd) { return child_process.execSync(cmd); }; } else { try { // Try using fallback package if available var execSync = require("sync-exec"); return function(cmd) { return execSync(cmd).stdout; }; } catch(e) {} } return null; })(); var config; switch(process.platform) { case "darwin": config = require("./platform/darwin"); break; case "win32": config = require("./platform/win32"); break; case "linux": config = require("./platform/linux"); break; case "freebsd": config = require("./platform/linux"); break; case "openbsd": config = require("./platform/openbsd"); break; default: throw new Error("Unknown platform: '" + process.platform + "'. Send this error to xavi.rmz@gmail.com."); } var noop = function() {}; exports.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; }; var pasteCommand = [ config.paste.command ].concat(config.paste.args).join(" "); exports.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."); } }; exports.silent = function() { throw new Error("DEPRECATED: copy-paste is now always silent."); }; exports.noConflict = function() { throw new Error("DEPRECATED: copy-paste no longer adds global variables by default."); }; exports.global = function() { GLOBAL.copy = exports.copy; GLOBAL.paste = exports.paste; return exports; }; package/platform/darwin.js000644 000765 000024 0000000547 12553111172014201 0ustar00000000 000000 exports.copy = { command: "pbcopy", args: [] }; exports.paste = { command: "pbpaste", args: [] }; exports.paste.full_command = exports.paste.command; exports.encode = function(str) { return new Buffer(str, "utf8"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }; package/platform/linux.js000644 000765 000024 0000000711 12553111155014046 0ustar00000000 000000 exports.copy = { command: "xclip", args: [ "-selection", "clipboard" ] }; exports.paste = { command: "xclip", args: [ "-selection", "clipboard", "-o" ] }; exports.paste.full_command = [ exports.paste.command ].concat(exports.paste.args).join(" "); exports.encode = function(str) { return new Buffer(str, "utf8"); }; exports.decode = function(chunks) { if(!Array.isArray(chunks)) { chunks = [ chunks ]; } return Buffer.concat(chunks).toString("utf8"); }; package/platform/win32.js000644 000765 000024 0000001366 12710306454013663 0ustar00000000 000000 var iconv = require("iconv-lite"); var path = require("path"); var vbsPath = path.join(__dirname, ".\\fallbacks\\paste.vbs"); var paste = { command: "cscript", args: [ "/Nologo", vbsPath ] }; paste.full_command = [ paste.command, paste.args[0], '"'+vbsPath+'"' ].join(" "); exports.copy = { command: "clip", args: [] }; exports.paste = paste; exports.encode = function(str) { return iconv.encode(str, "utf16le"); }; exports.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; }; package/platform/fallbacks/paste.vbs000644 000765 000024 0000002557 12710306454016140 0ustar00000000 000000 Function Base64Encode(sText) Dim oXML, oNode Set oXML = CreateObject("Msxml2.DOMDocument.3.0") Set oNode = oXML.CreateElement("base64") oNode.dataType = "bin.base64" oNode.nodeTypedValue =Stream_StringToBinary(sText) Base64Encode = oNode.text Set oNode = Nothing Set oXML = Nothing End Function 'Stream_StringToBinary Function '2003 Antonin Foller, http://www.motobit.com 'Text - string parameter To convert To binary data Function Stream_StringToBinary(Text) Const adTypeText = 2 Const adTypeBinary = 1 'Create Stream object Dim BinaryStream 'As New Stream Set BinaryStream = CreateObject("ADODB.Stream") 'Specify stream type - we want To save text/string data. BinaryStream.Type = adTypeText 'Specify charset For the source text (unicode) data. BinaryStream.CharSet = "utf-8" 'Open the stream And write text/string data To the object BinaryStream.Open BinaryStream.WriteText Text 'Change stream type To binary BinaryStream.Position = 0 BinaryStream.Type = adTypeBinary 'Ignore first two bytes - sign of BinaryStream.Position = 0 'Open the stream And get binary data from the object Stream_StringToBinary = BinaryStream.Read Set BinaryStream = Nothing End Function Dim objHTML Set objHTML = CreateObject("htmlfile") text = objHTML.ParentWindow.ClipboardData.GetData("Text") Wscript.Echo Base64Encode(text)package/test/copypaste.js000644 000765 000024 0000002123 12710306454014053 0ustar00000000 000000 'use strict'; var should = require('should'); var clipboard = require("../index.js"); function copy_and_paste(content, done) { clipboard.copy(content, function(error_when_copy) { should.not.exist(error_when_copy); clipboard.paste(function (error_when_paste, p) { should.not.exist(error_when_paste); should.exist(p); p.should.equal(content); done(); }); }); } describe('copy and paste', function () { it('should work correctly with ascii chars (<128)', function (done) { copy_and_paste("123456789abcdefghijklmnopqrstuvwxyz+-=&_[]<^=>=/{:})-{(`)}", done); }); it('should work correctly with cp437 chars (<256)', function (done) { copy_and_paste("ÉæÆôöòûùÿÖÜ¢£¥₧ƒ", done); }); it('should work correctly with unicode chars (<2^16)', function (done) { copy_and_paste("ĀāĂ㥹ĆćĈĉĊċČčĎ ፰፱፲፳፴፵፶፷፸፹፺፻፼", done); }); it('should work correctly for "±"', function (done) { copy_and_paste("±", done); }); });package/test/mocha.opts000644 000765 000024 0000000065 12710306454013507 0ustar00000000 000000 --require should --reporter spec --ui bdd --recursive