Copy to clipboard in Node.js?

Javascriptnode.jsClipboard

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

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();
//🦄

Solution 3 - Javascript

Here's a module that provide copy and paste functions: https://github.com/xavi-/node-copy-paste

When require("copy-paste").global() is executed, two global functions are added:

> copy("hello") // Asynchronously adds "hello" to clipbroad
> Copy complete
> paste() // Synchronously returns clipboard contents
'hello'

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).

This module was very helpful when I was doing a lot of work in the REPL for a side project. Needless to say, copy-paste is only a command line utility -- it is not meant for server work.

Solution 4 - Javascript

Shortest way in Windows:

const util = require('util');
require('child_process').spawn('clip').stdin.end(util.inspect('content_for_the_clipboard'));

Solution 5 - Javascript

A clipboard is not inherent to an operating system. It's a construct of whatever window system the operating system happens to be running. So if you wanted this to work on X for example, you would need bindings to Xlib and/or XCB. Xlib bindings for node actually exist: https://github.com/mixu/nwm. Although I'm not sure whether it gives you access to the X clipboard, you might end up writing your own. You'll need separate bindings for windows.

edit: If you want to do something hacky, you could also use xclip:

var exec = require('child_process').exec;

var getClipboard = function(func) {
  exec('/usr/bin/xclip -o -selection clipboard', function(err, stdout, stderr) {
    if (err || stderr) return func(err || new Error(stderr));
    func(null, stdout);
  });
};

getClipboard(function(err, text) {
  if (err) throw err;
  console.log(text);
});

Solution 6 - Javascript

I managed to do so by creating a different application which handles this. It's certainly not the best way, but it works.

I'm on Windows and created a VB.NET application:

Module Module1

    Sub Main()
        Dim text = My.Application.CommandLineArgs(0)
        My.Computer.Clipboard.SetText(text)
        Console.Write(text) ' will appear on stdout
    End Sub
End Module

Then in Node.js, I used child_process.exec to run the VB.NET application, with the data to be copied passed as a command line argument:

require('child_process').exec(
    "CopyToClipboard.exe \"test foo bar\"",

    function(err, stdout, stderr) {
        console.log(stdout); // to confirm the application has been run
    }
);

Solution 7 - Javascript

Mac has a native command line pbcopy for this usecase:

require('child_process').exec(
    'echo "test foo bar" | pbcopy',

    function(err, stdout, stderr) {
        console.log(stdout); // to confirm the application has been run
    }
);

Same code for Linux but replace pbcopy with Xclip (apt get install xclip)

Solution 8 - Javascript

check this zeroclipboard

> npm install zeroclipboard

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTowerView Question on Stackoverflow
Solution 1 - JavascriptBenjamin AtkinView Answer on Stackoverflow
Solution 2 - JavascriptSindre SorhusView Answer on Stackoverflow
Solution 3 - JavascriptXaviView Answer on Stackoverflow
Solution 4 - JavascriptErnst RobertView Answer on Stackoverflow
Solution 5 - JavascriptchjjView Answer on Stackoverflow
Solution 6 - JavascriptpimvdbView Answer on Stackoverflow
Solution 7 - JavascriptFGRibreauView Answer on Stackoverflow
Solution 8 - JavascriptDamodaranView Answer on Stackoverflow