从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据 [英] Getting RTF data out of Mac OS X pasteboard (clipboard)

查看:38
本文介绍了从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据pbpasteman页面,

   -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 文本?

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?

我尝试了 AppleScript,但是 osascript -e 'the clipboard as «class RTF »' 给出了响应 «data RTF 7B 大量的十六进制编码废话7D».AppleScript 可以将此十六进制数据转换为我可以播放的文本吗?

I tried AppleScript, but osascript -e 'the clipboard as «class RTF »' gives the response «data RTF 7Bton of Hex encoded crap7D». Can AppleScript convert this hexdata into text I can play with?

推荐答案

我在 AppleScript 内部看不到任何方法,但由于您无论如何都在 shell 中工作,我只是对其进行后期处理:十六进制编码废话"是您想要的 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

perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'

解释:substr($_,11,-3) 去掉 «data RTF»\n 位(每个guillemets 是两个字节);pack("H*", ...) 将十六进制编码的数据打包成字节流;unpack("C*", ...) 将字节流解包为字符值数组;print chr foreach ... 将数组中的每个整数转换为其对应的字符并打印出来;并且 -ne 选项评估为每一行给出的脚本,该行隐式存储在 $_ 中.(如果您希望该脚本在其自己的文件中,只需确保 shebang 行是 #!/usr/bin/perl -ne.)然后,运行

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, running

osascript -e 'the clipboard as «class RTF »' | \
  perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'

会给你原始的 RTF 输出.

will give you raw RTF output.

这篇关于从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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