如何为pbcopy生成富文本链接

Sti*_*lev 5 ruby macos clipboard rtf

我一直在播放一个脚本,该脚本在Chrome中选择文本并在Google中查找,提供四个首选,然后粘贴相关链接.它以不同的格式粘贴,具体取决于当前在Chrome中打开的页面 - DokuWiki格式,DokuWiki打开,HTML与普通网站,我想要WordPress WYSIWYG编辑器的富文本.

我试图用来pbpaste -Prefer rtf查看粘贴板上没有其他样式的富文本链接,但它仍然输出纯文本.在文本编辑中保存文件并进行实验后,我想出了以下内容

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

(我不得不使用gsub,因为不知何故在使用%Q#{}插入变量时,字符串不起作用)

但是,当我粘贴它时,链接之前和之后还有一个额外的换档符号.字符串会是什么样的,以避免这种情况?

jm6*_*666 8

从shell开始,干净的解决方案就是:

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

所以,使用textutil命令将正确的html ..转换为rtf ...

红宝石变种:

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

所以,当你在没有pbcopy部分的情况下运行上面的内容时,你会得到:

{\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}}}
Run Code Online (Sandbox Code Playgroud)

编辑:删除-Prefer基于mklement注释