1
const proc = require('child_process').spawn('pbcopy');
const iconv = require('iconv-lite');
const name = '吉星高照';

function iconvDecode(str = '') {
  return iconv.decode(Buffer.from(str, 'binary'), 'cp936');
}

function pbcopy(data) {
  proc.stdin.write(iconvDecode(data));
  proc.stdin.end();
  return data;
}

pbcopy(name);

剪贴板的预期输出(在 macOS 上)应该吉星高照 ˱®.

4

1 回答 1

1

原来我需要LC_CTYPE为 pbcopy 设置环境变量:

const proc = require('child_process').spawn('pbcopy', {
  env: {
    LC_CTYPE: 'UTF-8',
  },
});
const name = '吉星高照';

function pbcopy(data) {
  proc.stdin.write(data);
  proc.stdin.end();
  return data;
}

pbcopy(name);
于 2021-08-19T02:33:16.153 回答