苹果脚本:如何将html内容复制到剪贴板?

时间:2021-11-17 21:12:00

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:

但问题是如何将html内容复制到剪贴板?例如,如何将下列html内容复制到剪贴板中:

<b>bold text</b>

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

所以我在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 :)

我找到了一个可行的解决方案,并把它贴在下面:

5 个解决方案

#1


9  

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:

我找到了一个解决方案,它的想法是直接使用HTML类而不是RTF类。(TextEdit或web编辑器可以处理这个HTML类以及RTF类数据)您所要做的就是将HTML代码转换为原始的hexcode。完整的代码如下:

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. :)

当然,你可以把它们合并成一句话。希望这能帮助任何感兴趣的人。:)

#2


1  

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.

我在AppleScript上不是很好,但这里有一些有用的东西。不幸的是,由于它打开了Safari窗口,所以它不是即时的。您可能需要调整延迟值以补偿较慢的性能,但在我的测试中,0.25秒似乎足够长了。

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.

之后,呈现的文本应该在剪贴板上,准备粘贴到TextEdit中。

#3


1  

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:

我不能在约塞米蒂的线程中使用任何其他的解决方案。当将RTF内容放入剪贴板时,当将内容粘贴到TextEdit,而不是将内容粘贴到Chrome时(我希望将脚本粘贴到谷歌表)。最终,我成功地把这个工作做到了:

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

#4


0  

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

对不起,但是您的代码甚至不能在OS X Mavericks的AppleScript编辑器中编译。

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.

当然,您需要在上述语句之前设置AppleScript变量“the_url”和“link_text”。

#5


0  

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

(瑞恩·帕特松(Ryan Pattersson)在《anwser》(the anwser)一书中进行了扩展,这部作品就像一种魅力。)

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

#1


9  

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:

我找到了一个解决方案,它的想法是直接使用HTML类而不是RTF类。(TextEdit或web编辑器可以处理这个HTML类以及RTF类数据)您所要做的就是将HTML代码转换为原始的hexcode。完整的代码如下:

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. :)

当然,你可以把它们合并成一句话。希望这能帮助任何感兴趣的人。:)

#2


1  

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.

我在AppleScript上不是很好,但这里有一些有用的东西。不幸的是,由于它打开了Safari窗口,所以它不是即时的。您可能需要调整延迟值以补偿较慢的性能,但在我的测试中,0.25秒似乎足够长了。

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.

之后,呈现的文本应该在剪贴板上,准备粘贴到TextEdit中。

#3


1  

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:

我不能在约塞米蒂的线程中使用任何其他的解决方案。当将RTF内容放入剪贴板时,当将内容粘贴到TextEdit,而不是将内容粘贴到Chrome时(我希望将脚本粘贴到谷歌表)。最终,我成功地把这个工作做到了:

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

#4


0  

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

对不起,但是您的代码甚至不能在OS X Mavericks的AppleScript编辑器中编译。

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.

当然,您需要在上述语句之前设置AppleScript变量“the_url”和“link_text”。

#5


0  

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

(瑞恩·帕特松(Ryan Pattersson)在《anwser》(the anwser)一书中进行了扩展,这部作品就像一种魅力。)

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