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

后端 未结 4 1595
温柔的废话
温柔的废话 2020-12-09 03:46

According to the man page for pbpaste,

   -Prefer {txt | rtf | ps}
          tells pbpaste what type of data to look for  in  the          


        
相关标签:
4条回答
  • 2020-12-09 04:12

    i found a conversation about this with a quick google search

    0 讨论(0)
  • 2020-12-09 04:19

    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 讨论(0)
  • 2020-12-09 04:22

    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 讨论(0)
  • 2020-12-09 04:34

    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.

    0 讨论(0)
提交回复
热议问题