views:

202

answers:

2

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?

A: 

i found a conversation about this with a quick google search

mcgrailm
"However you will not be able to manipulate the RTF as a string within AppleScript.”I saw that in my searches before I posted the question, but I find it unbelievable that there’s no way to get the RTF data out without writing to a file. (I want the RTF on STDOut, so I can make further changes to it.) Does anyone know enough AppleScript to know if this is true?
Carl
what is it that you want t do to the rtf data supposing that there a way to get the data ?
mcgrailm
Convert it to HTML then convert the HTML to Markdown. Obviously, I could just write tempfile somewhere, but I’d really prefer not to for both performance reasons and aesthetic reasons. It seems like AppleScript should have a way to un-hex the data itself. Or that some other tool should exist for getting data off the pasteboard.
Carl
A: 

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.

Antal S-Z