Getting RTF data out of Mac OS X pasteboard (clipboard)

7.4k Views Asked by At

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?

6

There are 6 best solutions below

2
On BEST ANSWER

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.

3
On

i found a conversation about this with a quick google search

0
On

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.

0
On

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

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"

0
On

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