有没有办法可以在 Node.js 中复制到剪贴板?有什么模块或想法吗?我在桌面应用程序上使用 Node.js。希望这能澄清为什么我希望它能够实现这一目标。
对于 OS X:
function pbcopy(data) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(data); proc.stdin.end();
}
write()
可以采用缓冲区或字符串。字符串的默认编码为 utf-8。
clipboardy
。它允许您跨平台复制/粘贴。它比另一个答案中提到的copy-paste
模块得到了更积极的维护,并且解决了该模块的许多问题。
import clipboardy from 'clipboardy';
// Copy
clipboardy.writeSync('🦄');
// Paste
clipboardy.readSync();
//🦄
const util = require('util');
require('child_process').spawn('clip').stdin.end(util.inspect('content_for_the_clipboard'));
copy
和
paste
功能的模块:https://github.com/xavi-/node-copy-paste执行
require("copy-paste").global()
时,添加了两个全局函数:
> copy("hello") // Asynchronously adds "hello" to clipbroad
> Copy complete
> paste() // Synchronously returns clipboard contents
'hello'
与提到的许多其他答案一样,要复制并粘贴到节点中,您需要调用外部程序。 对于
node-copy-paste
,它会调用
pbcopy/pbpaste
(对于 OSX)、
xclip
(对于 Linux)和
clip
(对于 Windows)。当我在 REPL 中为一个业余项目做大量工作时,这个模块非常有帮助。 不用说,
只是一个命令行实用程序——它不是用于服务器工作。
。虽然我不确定它是否允许您访问 X 剪贴板,但您最终可能会编写自己的剪贴板。您将需要单独的窗口绑定。 编辑:如果你想做一些 hacky 的事情,你也可以使用 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);
});
我在 Windows 上创建了一个 VB.NET 应用程序:
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
然后在 Node.js 中,我使用
child_process.exec
运行 VB.NET 应用程序,并将要复制的数据作为命令行参数传递:
require('child_process').exec(
"CopyToClipboard.exe \"test foo bar\"",
function(err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);
pbcopy
:
require('child_process').exec(
'echo "test foo bar" | pbcopy',
function(err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);
Linux 的代码相同,但将
pbcopy
替换为
Xclip(
apt get install xclip
),它使用 Windows 本机命令行 clip
,并避免任何潜在的问题命令行解决方法的混乱使用,例如上面建议的
echo foo | clip
。 var cp = require("child_process");
var child = cp.spawn('clip');
child.stdin.write(result.join("\n"));
child.stdin.end();
希望这对某人有帮助!
交互。 (这意味着您还可以读取/写入不同的剪贴板格式)
var {
winapi,
user32,
kernel32,
constants
} = require('@kreijstal/winffiapi')
const GMEM_MOVEABLE=0x2;
var stringbuffer=Buffer.from("hello world"+'\0');//You need a null terminating string because C api.
var hmem=kernel32.GlobalAlloc(GMEM_MOVEABLE,stringbuffer.length);
var lptstr = kernel32.GlobalLock(hmem);
stringbuffer.copy(winapi.ref.reinterpret(lptstr, stringbuffer.length));
kernel32.GlobalUnlock(hmem);
if (!user32.OpenClipboard(0)){
kernel32.GlobalLock(hmem);
kernel32.GlobalFree(hmem);
kernel32.GlobalUnlock(hmem);
throw new Error("couldn't open clipboard");
}
user32.EmptyClipboard();
user32.SetClipboardData(constants.clipboardFormats.CF_TEXT, hmem);
user32.CloseClipboard();
对于 macOS:
let filePath; // absolute path of the file you'd like to copy
require('child_process').exec(
`osascript -e 'set the clipboard to POSIX file "${filePath}"'`,
function (err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);
对于 Windows:
const filePath; // absolute path of the file you'd like to copy
// Make a temporary folder
const tmpFolder = path.join(__dirname, `tmp-${Date.now()}`);
fs.mkdirSync(tmpFolder, { recursive: true });
// Copy the file into the temporary folder
const tmpFilePath = path.join(tmpFolder, path.basename(filePath));
fs.copyFileSync(filePath, tmpFilePath);
// To copy the contents of the folder in PowerShell,
// you need to add a wildcard to the end of the path
const powershellFolder = path.join(tmpFolder, '*');
require('child_process').exec(
`Set-Clipboard -PATH "${powershellFolder}"`, { 'shell': 'powershell.exe' },
function (err, stdout, stderr) {
console.log(stdout); // to confirm the application has been run
}
);