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.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
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:
Shortest way in Windows:
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:
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:For OS X:
write()
can take a buffer or a string. The default encoding for a string will be utf-8.Check out
clipboardy
. It lets you copy/paste cross-platform. It is more actively maintained than thecopy-paste
module mentioned in another answer and it fixes many of that module's issues.Here's a module that provide
copy
andpaste
functions: https://github.com/xavi-/node-copy-pasteWhen
require("copy-paste").global()
is executed, two global functions are added: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 topbcopy/pbpaste
(for OSX),xclip
(for linux), andclip
(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.