如何在Ruby中的剪贴板中获取临时文件? [英] How to get the temp file in the clipboard in Ruby?

查看:125
本文介绍了如何在Ruby中的剪贴板中获取临时文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取剪贴板中的临时文件的目录。我写了这样的代码

I am trying to get the directory of the temp file that is in the clipboard. I wrote the code like this

require 'clipboard'

puts Clipboard.paste

我注意到,当它是剪贴板中的文本时,脚本会打印出文本,但是在归档输出时是一个空字符串。

I notice that when it is text in the clipboard, the script prints out text, but when it comes to file the output is an empty string.

在Ruby中从剪贴板获取文件的正确方法是什么?

What is the correct way to get the file from clipboard in Ruby?

推荐答案

剪贴板上的宝石使用命令行应用程序 pbcopy 在MacOS上 pbpaste 。两种应用程序都无法按照您描述的方式来处理文件。

The clipboard gem uses the command line applications pbcopy and pbpaste on macOS. Neither application works for files in the way you are describing it.

答案,但简短的版本是,当您在Finder中选择文件并按CMD + C时,macOS会将对所选文件的引用放入剪贴板,然后按CMD + V macOS解释引用的文件,并将文件复制到目标位置。为了促进该操作,已进行了许多幕后工作。这是剪贴板基本功能的扩展。

There is more detail at this answer, but the short version is that when you select a file in the Finder and press CMD+C, macOS is placing a reference to the selected file into the clipboard, and then when you press CMD+V macOS interprets the referenced file and performs a copy operation for the file to the destination location. There is a lot of behind-the-scenes work done to facilitate that operation. It is an extension of the clipboard's base functionality.

pbcopy pbpaste都不

Neither pbcopy nor pbpaste support that functionality; they can only operate on strings. Accordingly, you cannot use the clipboard gem for this purpose.

如果要复制的文件是纯文本,则可以将其读取为字符串,然后将其粘贴到新文件。如果文件不是纯文本文件(例如图像或已编译的二进制文件),则无法执行此操作。

If the file you want to copy is plaintext then you can read it into a string and then paste it into a new file. If the file is not plaintext (e.g., an image or a compiled binary) then you cannot do this.

这篇关于如何在Ruby中的剪贴板中获取临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆