4

How can I send html to the clipboard as rich text from a script? My end goal is create a script so I can paste content from a source file into an email, but I want a general answer for pasting into any program that accepts rich text.

Example usage for pasting into email:

  1. Open the source file in vim
  2. Use :TOhtml command to create an html file with vim's syntax highlighting
  3. Use an answer from here to copy the html as rich text
  4. Paste into an email (this one wouldn't be scripted)

Related: Pasting diff output into Microsoft Outlook with syntax highlighting

5

Linux

via this answer

cat text.html | xclip -t text/html

Mac

via this answer

cat text.html | textutil -stdin -format html -convert rtf -stdout | pbcopy

Windows

In older Windows, you can natively only copy plaintext (via this answer).

type text.html | clip

In PowerShell you can copy rich text:

type text.html | Set-Clipboard -AsHtml

If you create a C:\sandbox\pbcopy.ps1:

type $args[0] | Set-Clipboard -AsHtml

Then you can enable scripts and then run it from anywhere (cmd.exe, .bat files, etc):

powershell C:\sandbox\pbcopy.ps1 text.html

There are a few different Cygwin commands to copy to Windows clipboard and it looks like cygwin provides xclip, so you could probably use the Linux solution on Windows if you have cygwin.

  • 1
    With Windows you can put rich text to clipboard using cli. Just use following PowerShell command: Echo "<p>foo <strong>bar</strong></p>" | Set-Clipboard -AsHtml – Marek Lewandowski Apr 21 '17 at 23:31
  • -AsHtml doesn't work for pasting to google docs (plain text appears), and there's no option -AsRtf. xclip requires X windows to be installed :-( – Vladimir Alexiev Mar 15 at 13:39
0

Use e-mail CSS! Use UTF8 encoding html file!

Run http (HTML-eMail.html) e-mail from comman line:

powershell .\mail-http.ps1

mail-http.ps1:

$time = get-date 

$from    = 'So.From@gmail.com'
$to      = 'So.To@gmail.com'
$subject = 'eMail-HTML ' + $time

$server=smtp.gmail.com;$port=587

$encoding = [System.Text.Encoding]::UTF8

$email=new-object Net.Mail.MailMessage($from, $to, $subject, $body)
$email.DeliveryNotificationOptions=[System.Net.Mail.DeliveryNotificationOptions]::Delay
$email.IsBodyHtml = $true
$email.Priority = [System.Net.Mail.MailPriority]::High

$email.BodyEncoding=$encoding

$email.Body = gc '.\HTML-eMail.html' -encoding UTF8

$smtp=new-object Net.Mail.SmtpClient($server,$port)
$smtp.EnableSSL = $true
$smtp.Timeout = 30000  #ms
$smtp.Credentials=New-Object System.Net.NetworkCredential($from, 'derParol'); 

$smtp.Send($email)

HTML-eMail.html:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">

...

</html>
  • That only works if I want to compose an email in html. I may be replying to an existing email, I may want to paste into a word processor, etc. I've edited the question to clarify that email was an example, and not the real end goal. – idbrii May 11 '15 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.