Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy to clipboard in Node.js?

Is there a way you can copy to clipboard in Node.js? Any modules or ideas what so ever? I'm using Node.js on a desktop application. Hopefully that clears up why I want it to be able to achieve this.

like image 544
Tower Avatar asked Oct 15 '22 13:10

Tower


People also ask

How do you copy text in node?

Like many of the other answer mentioned, to copy and paste in node you need to call out to an external program. In the case of node-copy-paste , it calls out to pbcopy/pbpaste (for OSX), xclip (for linux), and clip (for windows).

Can you copy paste in node?

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 and OpenBSD), and clip (for Windows). Currently works with node.

How do I copy text to clipboard?

Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

What is copy to clipboard means?

Updated: 05/03/2022 by Computer Hope. The clipboard, also known as pasteboard, is a particular location on a computer, phone, or tablet memory that temporarily stores cut or copied text or other data. Once something is stored in the clipboard, it can be pasted to a new location.


2 Answers

For OS X:

function pbcopy(data) {
    var proc = require('child_process').spawn('pbcopy'); 
    proc.stdin.write(data); proc.stdin.end();
}

write() can take a buffer or a string. The default encoding for a string will be utf-8.

like image 148
Benjamin Atkin Avatar answered Oct 18 '22 01:10

Benjamin Atkin


Check out clipboardy. It lets you copy/paste cross-platform. It is more actively maintained than the copy-paste module mentioned in another answer and it fixes many of that module's issues.

const clipboardy = require('clipboardy');

// Copy
clipboardy.writeSync('🦄');

// Paste
clipboardy.readSync();
//🦄
like image 114
Sindre Sorhus Avatar answered Oct 18 '22 02:10

Sindre Sorhus