从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据
根据 pbpaste
的 man
页面,
-Prefer {txt | rtf | ps}
tells pbpaste what type of data to look for in the pasteboard
first. As stated above, pbpaste normally looks first for plain
text data; however, by specifying -Prefer ps you can tell
pbpaste to look first for Encapsulated PostScript. If you spec-
ify -Prefer rtf, pbpaste looks first for Rich Text format. In
any case, pbpaste looks for the other formats if the preferred
one is not found. The txt option replaces the deprecated ascii
option, which continues to function as before. Both indicate a
preference for plain text.
但是(至少根据我使用 10.6 Snow Leopard 的经验),pbpaste -Prefer rtf
永远不会给出即使 RTF 数据存在于粘贴板上,也可以将其向上。有没有其他简单的方法来获取准备粘贴的 RTF 文本?
我尝试了 AppleScript,但是 osascript -e 'the Clipboard as «class RTF »'
给出了响应 «data RTF 7B
ton of Hex编码垃圾7D». AppleScript 可以将此十六进制数据转换为我可以使用的文本吗?
According to the man
page for pbpaste
,
-Prefer {txt | rtf | ps}
tells pbpaste what type of data to look for in the pasteboard
first. As stated above, pbpaste normally looks first for plain
text data; however, by specifying -Prefer ps you can tell
pbpaste to look first for Encapsulated PostScript. If you spec-
ify -Prefer rtf, pbpaste looks first for Rich Text format. In
any case, pbpaste looks for the other formats if the preferred
one is not found. The txt option replaces the deprecated ascii
option, which continues to function as before. Both indicate a
preference for plain text.
However (in my experience with 10.6 Snow Leopard at least), pbpaste -Prefer rtf
never, ever gives up the RTF data even when it exists on the pasteboard. Is there any other simple way to get the RTF text of whatever’s ready to be pasted?
I tried AppleScript, but osascript -e 'the clipboard as «class RTF »'
gives the response «data RTF 7B
ton of Hex encoded crap7D»
. Can AppleScript convert this hexdata into text I can play with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我看不出有什么方法可以从 AppleScript 内部做到这一点,但既然你无论如何都在 shell 中工作,我只是对其进行后处理:“十六进制编码的废话”是你想要的 RTF 数据。我能想到的最简单的脚本是
一个解释:
substr($_,11,-3)
剥离«data RTF
和»\n 位(每个 guillemets 是两个字节);
pack("H*", ...)
将十六进制编码的数据打包到字节流中;unpack("C*", ...)
将字节流解包为字符值数组;print chr foreach ...
将数组中的每个整数转换为其对应的字符并打印;-ne
选项评估为每一行给出的脚本,该行隐式存储在$_
中。 (如果您希望该脚本位于其自己的文件中,只需确保 shebang 行为#!/usr/bin/perl -ne
。)然后,运行将为您提供原始 RTF 输出。
I can't see any way to do it from inside AppleScript, but since you're working in the shell anyway, I'd just post-process it: the "hex-encoded crap" is the RTF data you want. The simplest script I can think of is
An explanation:
substr($_,11,-3)
strips off the«data RTF
and»\n
bits (each of the guillemets is two bytes);pack("H*", ...)
packs hex-encoded data into a bytestream;unpack("C*", ...)
unpacks a bytestream into an array of character values;print chr foreach ...
converts each integer in the array to its corresponding character and prints it; and the-ne
options evaluate the script given for each line, with that line implicitly stored in$_
. (If you want that script in its own file, just make sure the shebang line is#!/usr/bin/perl -ne
.) Then, runningwill give you raw RTF output.
我认为,如果您从 Chrome 复制 HTML 内容,至少在 OS X 10.8 上这会起作用:
I think that at least on OS X 10.8 this would work if you copied HTML content from Chrome:
通过 AppleScript(在 10.11 El Capitan 中测试)非常简单:
您可以通过 Automator 创建服务:
脚本:
完成。现在保存新服务并尝试一下:选择一个文本,然后转到应用程序菜单并选择“服务”>“服务”。 “转换为RTF”
It is very easy via AppleScript (tested in 10.11 El Capitan):
You can create a Service via Automator:
The Script:
Done. Now save the new Service and to try it out: Select a text, then go to the Application Menu and choose "Services" > "convert to RTF"
根据我的经验,即使手册页另有说明,也不可能从
pbpaste
中获取 RTF 数据。最简单的解决方案是使用 pbv 来代替,它的开发正是为了解决 <代码>pbpaste。
示例:将以下富文本字符串复制到剪贴板后:
pbv
能够给你返回正确的RTF数据:而
pbpaste
即使指示更喜欢 RTF,也始终输出纯文本:通过此类似问题找到。
In my experience it's impossible to get RTF data out of
pbpaste
, even if the man page says otherwise.The simplest solution is to use pbv instead, which was developed exactly to work around the limitations of
pbpaste
.An example: after copying the following rich text string into your clipboard:
pbv
is able to give you back proper RTF data:Whereas
pbpaste
will always output plain text even when instructed to prefer RTF:Found via this similar question.
我提供的简单 AppleScript 解决方案。它比其他答案中提出的要快:
而且,这是来自用户 @ionah 在 MacScripter 上的有趣的 AsObjC 解决方案。这段代码甚至更快:
Plain AppleScript solution from me. It is faster than proposed in other answers:
And, here is interesting AsObjC solution from user @ionah on MacScripter. This code is even faster:
我通过快速谷歌搜索发现了关于此的对话
i found a conversation about this with a quick google search