【问题标题】:Getting RTF data out of Mac OS X pasteboard (clipboard)从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据
【发布时间】:2011-02-02 11:16:22
【问题描述】:

根据man页面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.

但是(至少根据我使用 10.6 Snow Leopard 的经验),pbpaste -Prefer rtf 永远不会放弃 RTF 数据,即使它存在于粘贴板上。有没有其他简单的方法来获取准备粘贴的任何内容的 RTF 文本?

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

【问题讨论】:

  • 十年后,我选择了这个swift solution。至少对 html 非常有效。

标签: macos applescript rtf pasteboard


【解决方案1】:

我在 AppleScript 中看不到任何方法,但是由于您无论如何都在 shell 中工作,所以我只是对其进行后处理:“十六进制编码的废话”是您想要的 RTF 数据.我能想到的最简单的脚本是

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。)然后,运行

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

会给你原始的 RTF 输出。

【讨论】:

  • 聪明的方法。轻微的简化是将«class RTF » 替换为纯字符串"RTF "(请注意在任何一种情况下结束分隔符之前所需的尾随空格):osascript -e 'the clipboard as "RTF "' | perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' - 适用于(至少)10.8+。另请注意 - 根据设计 - 如果剪贴板上没有 RTF 数据,AppleScript 命令将报告错误。
  • 我已将这个非常方便的别名添加到我的.profilealias pbpaste-rtf="osascript -e 'the clipboard as \"RTF \"'| perl -ne 'print chr foreach unpack(\"C*\", pack(\"H*\", substr(\$_,11,-3)))'"
【解决方案2】:

我认为至少在 OS X 10.8 上,如果您从 Chrome 复制 HTML 内容,这将起作用:

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

【讨论】:

    【解决方案3】:

    根据我的经验,即使手册页另有说明,也不可能从 pbpaste 中获取 RTF 数据。

    最简单的解决方案是改用pbv,它的开发正是为了解决pbpaste 的限制。

    示例:将以下富文本字符串复制到剪贴板后:

    “嗨,我是富有 文字

    pbv 能够返回正确的 RTF 数据:

    $ pbv public.rtf | textutil -stdin -info
    File:  stdin
      Type:  rich text format (RTF)
      Length:  19 characters
      Contents:  "Hi, I'm rich text"
    

    pbpaste 将始终输出纯文本,即使被指示首选 RTF:

    $ pbpaste -Prefer rtf | textutil -stdin -info
    File:  stdin
      Type:  plain text
      Length:  19 characters
      Contents:  "Hi, I'm rich text"
    
    

    通过similar question找到。

    【讨论】:

      【解决方案4】:

      我通过快速的谷歌搜索找到了一个conversation

      【讨论】:

      • “但是,您将无法在 AppleScript 中将 RTF 作为字符串进行操作。”在发布问题之前,我在搜索中看到了这一点,但我发现不写入文件就无法获取 RTF 数据,这令人难以置信。(我想要 STDOut 上的 RTF,所以我可以对其进行进一步的更改。 ) 有没有人知道足够的 AppleScript 来判断这是否属实?
      • 假设有办法获取数据,你想对 rtf 数据做什么?
      • 将其转换为 HTML,然后将 HTML 转换为 Markdown。显然,我可以在某个地方编写 tempfile,但出于性能和美观的原因,我真的不希望这样做。似乎 AppleScript 应该有办法对数据本身进行反十六进制。或者应该存在一些其他工具来从粘贴板上获取数据。
      【解决方案5】:

      通过 AppleScript 非常简单(在 10.11 El Capitan 中测试):

      set the clipboard to (the clipboard as «class RTF »)
      

      您可以通过 Automator 创建服务:

      1. 打开自动机
      2. 创建新服务(德语中的“Dienst”)
      3. 添加“执行 AppleScript”
      4. 输入:无;输出;替换选择

      脚本:

      -- name: convert to RTF
      on run {input, parameters}
          set the clipboard to (the clipboard as «class RTF »)
          return the clipboard
      end run
      

      完成。现在保存新服务并尝试一下:选择一个文本,然后转到应用程序菜单并选择“服务”>“转换为 RTF”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-21
        • 2016-01-30
        • 2017-12-23
        • 2015-07-29
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多