K J
K J

Reputation: 4743

Apple Script : How can I copy html content to the clipboard?

I know how to copy plain text to the clipboard:

oascript -e 'set the clipboard to "plain text"'

But the question is how can I copy html contents to the clipboard? For example, how can I copy the following html content to the clipboard:

<b>bold text</b>

so that I get bold text when I paste it in TextEdit?

Thanks for the help in advance!


I found an intermediate solution for this:

echo "<b>bold text</b>" | textutil -stdin -stdout -format html -convert rtf | pbcopy

This works, so far so good, but unfortunately I found that it doesn't work for an image tag:

echo "<img src=\"https://www.google.com/images/srpr/logo3w.png\">" | textutil -stdin -stdout -format html -convert rtf | pbcopy

This does not do the job I want, so anybody knows the reason?
Thanks!


I've found a working solution and posted it below :)

Upvotes: 15

Views: 7137

Answers (6)

Weihang Jian
Weihang Jian

Reputation: 8715

The solution by @k-j would not work if the input HTML is too big, you might encounter some error messages like below:

/usr/bin/osascript: Argument list too long

I made some improvements to @k-j's solution by converting it to an executable and handling data via pipe. I hope it helps as well.

Executable

~/bin/pbcopyhtml:

#!/bin/sh
printf "set the clipboard to «data HTML$(cat $@ | hexdump -ve '1/1 "%.2x"')»" | osascript -

Usage

from pipe

$ printf '# title\n\n- list\n- list' | cmark | ~/bin/pbcopyhtml
$ osascript -e 'the clipboard as record'
«class HTML»:«data HTML3C68313E7469746C653C2F68313E0A3C756C3E0A3C6C693E6C6973743C2F6C693E0A3C6C693E6C6973743C2F6C693E0A3C2F756C3E0A»

from file

$ printf '# title\n\n- list\n- list' | cmark > sample.html
$ ~/bin/pbcopyhtml sample.html
$ osascript -e 'the clipboard as record'
«class HTML»:«data HTML3C68313E7469746C653C2F68313E0A3C756C3E0A3C6C693E6C6973743C2F6C693E0A3C6C693E6C6973743C2F6C693E0A3C2F756C3E0A»

Upvotes: 7

pspi
pspi

Reputation: 11927

(Expanding on the anwser by Ryan Pattersson that works like a charm.)

You can create a subroutine that puts a link to the clipboard and then call it from multiple places in your code

my urlToClipboard("Gmail", "http://gmail.com")

on urlToClipboard(theTitle, theUrl)
    set rawHTML to "<a href=\"" & theUrl & "\">" & theTitle & "</a>"
    set escapedData to do shell script "echo " & (quoted form of rawHTML) as «class HTML»
    set the clipboard to escapedData
end urlToClipboard

Upvotes: 0

Ryan Patterson
Ryan Patterson

Reputation: 627

I wasn't able to use any other solution in this thread on Yosemite. When putting RTF content into the clipboard things worked when pasting into TextEdit, but not when pasting into Chrome (I wanted to script pasting into a Google Sheets spreadsheet). Eventually I was able to get this to work:

set rawHTML to "<a href=\"" & gmailURL & "\">" & myTitle & "</a>"
set escapedData to do shell script "echo " & (quoted form of rawHTML) as «class HTML»
set the clipboard to escapedData

Upvotes: 3

JMichaelTX
JMichaelTX

Reputation: 1807

Sorry, but your code won't even compile in AppleScript Editor in OS X Mavericks

hex=`echo -n "your html code here" | hexdump -ve '1/1 "%.2x"'`
osascript -e "set the clipboard to «data HTML${hex}»"

However, I have found the following code does work well:

set the_HTML to "<font size=4 face=\"verdana\"><a href=\"" & the_url & "\" target=_blank>" & link_text & "</a></font>"
--set the clipboard to the_text

do shell script "echo " & quoted form of the_HTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"

Of course you will need to set the AppleScript variables "the_url" and "link_text" prior to the above statements.

Upvotes: -1

K J
K J

Reputation: 4743

I've found a solution and the idea is to use the HTML class directly instead of the RTF class. (TextEdit or web editors can handle this HTML class as well as the RTF class data)
All you have to do is to convert your html code into raw hexcode.
The complete code looks like:

hex=`echo -n "your html code here" | hexdump -ve '1/1 "%.2x"'`
osascript -e "set the clipboard to «data HTML${hex}»"

You can combine them into one sentence, of course.
Hope this helped anybody interested. :)

Upvotes: 36

Michael Morgan
Michael Morgan

Reputation: 814

I'm not super great at AppleScript, but here's something that works. Unfortunately, since it opens a Safari window, it's not instant. You may need to adjust the delay value to compensate for slower performance, but 0.25 s seemed long enough in my tests.

set theHTML to "<b>bold text</b>"

tell application "Safari"
    open location "data:text/html," & theHTML
    activate
    tell application "System Events"
        keystroke "a" using {command down}
        keystroke "c" using {command down}
    end tell
    delay 0.25
    close the first window
end tell

After that, the rendered text should be on your clipboard, ready to paste into TextEdit.

Upvotes: 1

Related Questions