Skip to content

Instantly share code, notes, and snippets.

@psenger
Last active March 26, 2024 02:11
Show Gist options
  • Save psenger/cef189aa928e55c2d5953fe8dfe3f19a to your computer and use it in GitHub Desktop.
Save psenger/cef189aa928e55c2d5953fe8dfe3f19a to your computer and use it in GitHub Desktop.
[How to programmatically get data in JavaScript directly into your buffer] #MacOS #JavaScript #Node
/**
* Sends a String of data to the Mac OSX Buffer, this is very helpful if
* you are doing something in scratch pad and want to copy it somewhere.
*
* this beats Pipe-ing the output to pbcopy directly.
*
* @param {string} data - Required UTF-8 String to be copied to the buffer.
*
* @return {void}
*
* @example
* pbcopy(JSON.stringify(data, null, 4))
*/
function pbcopy(data) {
const proc = require('child_process').spawn('pbcopy', { env: { LC_CTYPE: 'UTF-8' } })
proc.stdin.write(data)
proc.stdin.end()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment