simple mac clipboard for node & electron
$ pnpm add simple-mac-clipboardbased on https://developer.apple.com/documentation/appkit/nspasteboard
const clip = require('simple-mac-clipboard')- for read & write, format is required, and format is always the first argument
- for write, the boolean return value is for success or not, map from objc
YESorNO
export function clear(): voidclear the clipboard
// single item
export function readBuffer(format: string): Buffer
export function readText(format: string): string
// items
export function readBuffers(format: string): Buffer[]
export function readTexts(format: string): string[]// typedef
export function writeFormat(format: string, ...datas: Array<Buffer | string | Buffer[] | string[]>): boolean// example
import clip from 'simple-mac-clipboard'
clip.writeFormat(clip.FORMAT_PLAIN_TEXT, 'text1', Buffer.from('text2'), [Buffer.from('text3'), 'text4'])// typedef
export type PasteboardItem = Record<string, Buffer | string>
export function writePasteboardItems(...items: Array<PasteboardItem | PasteboardItem[]>): boolean// example
import clip from 'simple-mac-clipboard'
clip.writePasteboardItems(
{ 'item1-format1': 'text1' },
{ 'item2-format1': Buffer.from('text2'), 'item2-format2': 'text2', [clip.FORMAT_PLAIN_TEXT]: 'text3' },
)
clip.writePasteboardItems([
{ 'item1-format1': 'text1' },
{ 'item2-format1': Buffer.from('text2'), 'item2-format2': 'text2', [clip.FORMAT_PLAIN_TEXT]: 'text3' },
])write single item. this method using
NSPasteboard.declareTypeswhich supports non-UTI format for exampleNTPathFinderFilenamesPboardTypeequalsdyn.ah62d4rv4gu8y6zcuqf4guvxmr3wgn6wgrf0gn5xbrzw1gydcr7u1e3cytf2gnsee https://stackoverflow.com/questions/8003919/are-dynamic-utis-stable & https://alastairs-place.net/blog/2012/06/06/utis-are-better-than-you-think-and-heres-why/
// typedef
export function declareTypeAndSetData(format: string, data: Buffer | string): boolean// example
import { declareTypeAndSetData, readText } from 'simple-mac-clipboard'
declareTypeAndSetData('invalid-uti-without-dot', 'invalid-uti-without-dot-content')
expect(readText('invalid-uti-without-dot')).toEqual('invalid-uti-without-dot-content')FORMAT_PLAIN_TEXT / FORMAT_FILE_URL / FORMAT_SOURCE_APP_BUNDLE_ID
import { FORMAT_FILE_URL, readTexts, writeFormat } from 'simple-mac-clipboard'
import { fileURLToPath, pathToFileURL } from 'node:url'
const filePaths = ['/tmp/a/b.txt', '/tmp/c']
// write
writeFormat(FORMAT_FILE_URL, ...filePaths.map((p) => pathToFileURL(p).href))
// read
console.log(readTexts(FORMAT_FILE_URL).map((u) => fileURLToPath(u)))the MIT License http://magicdawn.mit-license.org