A command line utility that allows read/write (i.e copy/paste) access to the system clipboard. It does this by wrapping pbcopy/pbpaste (for OSX), xclip (for Linux, FreeBSD, and OpenBSD), and clip (for Windows). Currently works with node.js v0.8+.
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 withtext. 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.copy.json(obj[, callback]): asynchronously replaces the current contents of the clip board with the JSON string ofobj. Returns the same value passed in. Optional callback will fire when the copy operation is complete.paste([callback]): if no callback is provided,pastesynchronously 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. The first one being a potential error.require("copy-paste").global(): addscopyandpasteto the global namespace. Returns an object withcopyandpasteas properties.
Example usage:
const { copy, paste } = require("copy-paste");
copy("some text", (err, text) => {
// "some text" is in your clipboard
});
paste((err, text) => {
// complete...
});
const text = paste(); // Synchronous paste
copy({ hello: "world" }) // Asynchronous copyFor modern JavaScript applications, you can use the promise-based interface by requiring the promises submodule:
const clipboard = require('copy-paste/promises');The promise-based API provides the following methods:
copy(text): Returns a promise that resolves with the copied text when the operation is completecopy.json(obj): Returns a promise that resolves with the copied JSON string when the operation is completepaste(): Returns a promise that resolves with the clipboard contents when the operation is complete
Example usage with async/await:
const { copy, paste } = require('copy-paste/promises');
// Copy text
await copy('Hello World');
// Copy JSON
await copy.json({ hello: 'world' });
// Paste text
const text = await paste();The easiest way to get node-copy-paste is with npm:
npm install copy-paste
Alternatively you can clone this git repository:
git clone git://github.com/xavi-/node-copy-paste.git
To use node-copy-paste in a TypeScript project, you can install the community-maintained type definitions from DefinitelyTyped:
Install types via npm:
npm install --save-dev @types/copy-pasteThese definitions were written by Tobias Kahlert.
Type definitions available on npm: @types/copy-paste
Once installed, you can import the package in your TypeScript code as usual:
import { copy, paste } from 'copy-paste';
copy('Hello TypeScript');
const text = paste();Or, if you're using the promise-based API:
import { copy, paste } from 'copy-paste/promises';
await copy('Hello TypeScript');
const text = await paste();I'm hoping to add various fallbacks for instances when xclip or clip is not avaiable (see experimental-fallbacks branch). Also this library needs to be more thoroughly tested on windows.
- Xavi Ramirez
This project is released under The MIT License.