How can I create a rich text link for pbcopy

I played with a script that displays the selected text in Chrome and views it on Google, offering the four best options, and then pasting the appropriate link. It is pasted in different formats, depending on which page is currently open in Chrome - DokuWiki with the opening of DokuWiki, HTML with regular websites, and I want to have rich text for my WordPress WYSIWYG editor.

I tried using pbpaste -Prefer rtf to see which link on rich text, without any other style, looked like on a file cabinet, but it still displays the text. After saving the file in a text editor and experimenting, I came up with the following

 text = %q|{\rtf1{\field{\*\fldinst{HYPERLINK "URL"}}{\fldrslt TEXT}}}| text.gsub!("URL", url) text.gsub!("TEXT", stext) 

(I had to use gsub because somehow when using %Q and #{} to insert variables, the line did not work)

This works, however, when I insert it, there is an extra line-line before and after the link. What would a string look like to avoid this?

+4
source share
3 answers

From the shell, this is a clean solution:

 URL="http://www.google.com/" NAME="Click here for Google" echo "<a href='$URL'>$NAME</a>" | textutil -stdin -format html -convert rtf -stdout | pbcopy 

So, use the textutil command to convert the correct html .. to rtf ...

ruby option:

 url = 'http://www.google.com' name = 'click here' system("echo '<a href=\"#{url}\">#{name}</a>' | textutil -stdin -format html -convert rtf -stdout | pbcopy") 

so when you run above without the pbcopy part, you will get:

 {\rtf1\ansi\ansicpg1250\cocoartf1038\cocoasubrtf350 {\fonttbl\f0\froman\fcharset0 Times-Roman;} {\colortbl;\red255\green255\blue255;\red0\green0\blue238;} \deftab720 \pard\pardeftab720\ql\qnatural {\field{\*\fldinst{HYPERLINK "http://www.google.com/"}}{\fldrslt \f0\fs24 \cf2 \ul \ulc2 click here}}} 

EDIT: deleted -Support based on mklement comment

+8
source

One way to do this is to use MacRuby, which can directly access cardboard through Cocoa's infrastructure, rather than using a command line tool that gives you more options.

For example, you can use this function to embed HTML code, including hyperlinks that will be correctly inserted in TextEdit or in the WordPress editing field:

 framework 'Cocoa' def pbcopy(string) pasteBoard = NSPasteboard.generalPasteboard pasteBoard.declareTypes([NSHTMLPboardType], owner: nil) pasteBoard.setString(string, forType: NSHTMLPboardType) end 

This works much better than the pbcopy command line, since it completely avoids adding white space and also allows you to send RTF for rich text, where HTML is much easier to create programmatically.

+2
source

pbcopy macOS pbcopy can detect RTF. The following example (using pandoc to convert markdown to RTF) places a piece of expanded text into the paste buffer:

 echo '**foo**' | pandoc -t rtf -s | pbcopy 
0
source

All Articles