从 Mac OS X 粘贴板(剪贴板)中获取 RTF 数据

发布于 2024-08-27 02:18:50 字数 1071 浏览 4 评论 0原文

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

我尝试了 AppleScript,但是 osascript -e 'the Clipboard as «class RTF »' 给出了响应 «data RTF 7Bton 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 7Bton of Hex encoded crap7D». Can AppleScript convert this hexdata into text I can play with?

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

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

发布评论

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

评论(6

聆听风音 2024-09-03 02:18:50

我看不出有什么方法可以从 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 输出。

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)))'

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)))'

will give you raw RTF output.

我也只是我 2024-09-03 02:18:50

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

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

I think that at least on OS X 10.8 this would work if you copied HTML content from Chrome:

osascript -e 'the clipboard as "HTML"'|perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))'
第七度阳光i 2024-09-03 02:18:50

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

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

您可以通过 Automator 创建服务:

  1. 打开 Automator
  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”

It is very easy via AppleScript (tested in 10.11 El Capitan):

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

You can create a Service via Automator:

  1. open Automator
  2. make new service ("Dienst" in German)
  3. add "execute a AppleScript"
  4. input: nothing; output; replaces Selection

The Script:

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

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"

守望孤独 2024-09-03 02:18:50

根据我的经验,即使手册页另有说明,也不可能从 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"

通过此类似问题找到。

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:

"Hi, I'm rich text"

pbv is able to give you back proper RTF data:

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

Whereas pbpaste will always output plain text even when instructed to prefer RTF:

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

Found via this similar question.

浅暮の光 2024-09-03 02:18:50

我提供的简单 AppleScript 解决方案。它比其他答案中提出的要快:

-- script: Get raw RTF content from the clipboard
-- written: by me, right now

clipboard_as_raw_RTF()

on clipboard_as_raw_RTF()
    -- catch the hext text from the error message
    try
        (the clipboard as «class RTF ») as text
    on error errorMessage
        set hexRTFData to errorMessage
    end try
    -- clear the garbage text 
    set ATID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"«data RTF ", "»"}
    set hexRTFData to text item 2 of hexRTFData
    set AppleScript's text item delimiters to ATID
    -- convert hexadecimal text to ASCII text
    do shell script "/bin/echo -n " & hexRTFData & " | xxd -r -p" without altering line endings
end clipboard_as_raw_RTF

而且,这是来自用户 @ionah 在 MacScripter 上的有趣的 AsObjC 解决方案。这段代码甚至更快:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

on clipboard_as_raw_RTF()
    set thePasteboard to current application's NSPasteboard's generalPasteboard()
    set theData to (thePasteboard's dataForType:(current application's NSPasteboardTypeRTF))
    if (theData is missing value) then return theData -- If no RTF data.
    return (current application's NSString's alloc()'s initWithData:(theData) encoding:(current application's NSUTF8StringEncoding)) as text
end clipboard_as_raw_RTF

clipboard_as_raw_RTF()

Plain AppleScript solution from me. It is faster than proposed in other answers:

-- script: Get raw RTF content from the clipboard
-- written: by me, right now

clipboard_as_raw_RTF()

on clipboard_as_raw_RTF()
    -- catch the hext text from the error message
    try
        (the clipboard as «class RTF ») as text
    on error errorMessage
        set hexRTFData to errorMessage
    end try
    -- clear the garbage text 
    set ATID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"«data RTF ", "»"}
    set hexRTFData to text item 2 of hexRTFData
    set AppleScript's text item delimiters to ATID
    -- convert hexadecimal text to ASCII text
    do shell script "/bin/echo -n " & hexRTFData & " | xxd -r -p" without altering line endings
end clipboard_as_raw_RTF

And, here is interesting AsObjC solution from user @ionah on MacScripter. This code is even faster:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

on clipboard_as_raw_RTF()
    set thePasteboard to current application's NSPasteboard's generalPasteboard()
    set theData to (thePasteboard's dataForType:(current application's NSPasteboardTypeRTF))
    if (theData is missing value) then return theData -- If no RTF data.
    return (current application's NSString's alloc()'s initWithData:(theData) encoding:(current application's NSUTF8StringEncoding)) as text
end clipboard_as_raw_RTF

clipboard_as_raw_RTF()
满身野味 2024-09-03 02:18:50

我通过快速谷歌搜索发现了关于此的对话

i found a conversation about this with a quick google search

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文