将剪贴板设置为图像 - pbcopy

发布于 2024-11-27 11:31:43 字数 93 浏览 1 评论 0原文

如何使用 pbcopy 将图像设置为剪贴板?

这不起作用:

cat image.png | pbcopy

How do you set an image as the clipboard with pbcopy?

This doesn't work:

cat image.png | pbcopy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

背叛残局 2024-12-04 11:31:43

更新答案

实际上,您可以在命令行中使用 Applescript 将 JPEG 图像放入剪贴板,如下所示:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.jpg") as JPEG picture)'

然后您可以使用以下命令检查剪贴板上的内容:

osascript -e 'clipboard info'

JPEG picture, 175960, «class 8BPS», 641904, GIF picture, 124637, «class jp2 », 102086, TIFF picture, 1481282, «class PNGf», 412940, «class BMP », 1477734, «class TPIC», 609835

并且还将图像粘贴到文档中,使用通常的 -V。

原始答案

可以做到这一点,不需要编译任何额外的软件,只需使用OS X中提供的工具。基本上,剪贴板无法存储二进制文件,所以你需要将您的二进制图像 uuencode 转换为简单的 ASCII 数据,如下所示:

# Copy image to clipboard
uuencode SomeFile.jpg - | pbcopy

以及 uudecode 当以其他方式返回时

# Paste from clipboard to image file
pbpaste | uudecode -o AnotherFile.jpg

Updated Answer

You can actually put a JPEG image in the clipboard using Applescript like this at the command-line:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.jpg") as JPEG picture)'

You can then check what is on the clipboard with:

osascript -e 'clipboard info'

JPEG picture, 175960, «class 8BPS», 641904, GIF picture, 124637, «class jp2 », 102086, TIFF picture, 1481282, «class PNGf», 412940, «class BMP », 1477734, «class TPIC», 609835

And also paste the image into a document, with the usual -V.

Original Answer

You can do this without needing to compile any additional software and just use the tools provided in OS X. Basically, the clipboard is unable to store binary, so you need to uuencode your binary image into simple ASCII data like this:

# Copy image to clipboard
uuencode SomeFile.jpg - | pbcopy

and uudecode when going back the other way

# Paste from clipboard to image file
pbpaste | uudecode -o AnotherFile.jpg
笑叹一世浮沉 2024-12-04 11:31:43

如前所述,这不适用于 pbcopy,但您可以编写一些 Objective-C 程序来执行此操作:http://www.alecjacobson.com/weblog/?p=3816 。然后你可以发出:

cat image.png | impbcopy -

As stated, this won't work with pbcopy, but you can write a little objective-c program to do this: http://www.alecjacobson.com/weblog/?p=3816 . Then you can issue:

cat image.png | impbcopy -
你的笑 2024-12-04 11:31:43

从文档中:

输入将作为 ASCII 数据放置在粘贴板中,除非它以封装 PostScript (EPS) 文件头或富文本格式 (RTF) 文件头开头,在这种情况下,它会作为这些数据类型之一放置在粘贴板中。

听起来不支持图像数据,所以它不起作用。

From the documentation:

The input is placed in the pasteboard as ASCII data unless it begins with the Encapsulated PostScript (EPS) file header or the Rich Text Format (RTF) file header, in which case it is placed in the pasteboard as one of those data types.

It doesn't sound like image data is supported, so it won't work.

窗影残 2024-12-04 11:31:43

如何将 PNG 图像复制到 Macos 剪贴板的实际(或最终)答案隐藏在 Mark 答案的注释中。由于从马克的回答中来看并不那么明显,而且我花了一段时间才找到它,所以我将其作为答案留在这里:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.png") as  {«class PNGf»})'

The actual (or final) answer to how to copy a PNG image into the macos' clipboard was hidden in the comments to the Mark's answer. As it is not (that) obvious from Mark's answer and it took me a while to find it I will leave it as an answer here:

osascript -e 'set the clipboard to (read (POSIX file "/Users/mark/Desktop/a.png") as  {«class PNGf»})'
谢绝鈎搭 2024-12-04 11:31:43

一种同时适用于图像和 gif 动画的替代方法是:

osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file "<PATH_TO_IMAGE>" )'

one alternative that works for both images and animated gif is:

osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file "<PATH_TO_IMAGE>" )'
我是男神闪亮亮 2024-12-04 11:31:43

要使 osascript 正常工作(对于任何文件类型),请勿使用读取并使用绝对路径:

osascript -e 'set the clipboard to (POSIX file "/Full/Path/To/image.png")'

因此,对于当前目录中的文件(注意外部双引号):

osascript -e "set the clipboard to (POSIX file \"$PWD/image.png\")"

To make osascript work properly (with any file type), don't use read and use absolute path:

osascript -e 'set the clipboard to (POSIX file "/Full/Path/To/image.png")'

So for a file in current directory (note outer double quotes):

osascript -e "set the clipboard to (POSIX file \"$PWD/image.png\")"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文