|
试了下 pbpaste命令,貌似只能打印出剪切板里的文本
用 Swift 写个命令行工具,然后 Node.js 调用吧。
如下是使用 js 和 vue 从剪切板获取图片对象,实测可用.转成 base64 需要自己处理getImage() { const clipboardObj = navigator.clipboard; if (clipboardObj !== undefined) { setTimeout(async () => { try { const clipboardItems = await navigator.clipboard.read(); for (const clipboardItem of clipboardItems) { // console.log(clipboardItem); for (const type of clipboardItem.types) { const blob = await clipboardItem.getType(type); console.log(blob); if(blob.type.includes("image")){ this.imageUrl = URL.createObjectURL(blob); return }else{ console.log("not image"); } } } this.$message({ message: '剪切板没有图片', type: 'warning', duration: 2000 }); } catch (err) { console.error(err.name, err.message); } }, 100); } },
google 关键字 pngpaste
pbpaste 已经接近可用的思路了,可以参照下面的方案写个函数存在 .*shrc 里:openssl base64 < path/to/file.png | tr -d '\n' | pbcopycat path/to/file.png | openssl base64 | tr -d '\n' | pbcopy
https://github.com/magicdawn/simple-mac-clipboard和 Electron clipboard 差不多的 api, 之前做 electron, 碰到读图片导致 electron crash. 写了个 addon, 可以在 node or electron 中使用const clip = require('simple-mac-clipboard')const buf = clip.readBuffer('public/png') // png bufferconst base64 = buf.toString('base64') // png buffer -> string (base64 encoding)
readBuffer 第一个参数可以从剪贴板数据中获取参见- https://superuser.com/questions/541335/clipboard-viewer-for-os-x-that-allows-extracting-the-data-in-any-available-forma- https://github.com/sindresorhus/Pasteboard-Viewer- https://langui.net/clipboard-viewer/
http://www.markdouma.com/developer/ClipboardViewer.zip
|
|