Jump to content

Creating Rich text links in Mail app


Recommended Posts

Hi, my first post is a little bit long, but I spent some days in Google before posting here, so I hope to get help.

 

I am trying to create workflow using AppleScript that creates Mail message for me with some links in the footer of the mail body. 
 
The problem is that I can't insert links this way, because it places this html as Plain text instead of Rich text:
set FooterText to "<a href=\"http://example.com\">Example link</a>"
I was trying to find a way to convert html text to Rich text, and found that it can be done with "textutil" from a Terminal app.
Running this command from Terminal app works fine:
echo '<a href="http://example.com">Example link</a>'|textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy

I can then open Mail app and paste "Example link" as a rich text, and this link is working.

 

However I can't do the same using "do shell script" from Applescript (NSAppleScript). 

First I thought it is something to do with escaping characters, so I did a test with <b>bold</b>, but it didn't work, too.
Then instead of using "do shell script" I tried "do script" by telling application "Terminal" same script that was working in Terminal app, but it doesn't work from AppleScript, too:
 
on alfred_script(q)

set myHtmlText to "<b>bold example</b>"
tell application "Terminal"
   do script "echo " & quoted form of myHtmlText & |textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"
end tell

end alfred_script
 
Error msg says: "Expected end of line but found identifier." pointing to "|" before LC_CTYPE
Any ideas why it is not working?

 

Thanks in advance,

Romans

 

Link to comment

You forgot to open the quotes. Instead of

do script "echo " & quoted form of myHtmlText & |textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"

it should read (note the quote mark preceding |textutil)

do script "echo " & quoted form of myHtmlText & "|textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"

You don’t need tell Terminal or alfred_script(q), though. You can replace everything with a Run Script using /usr/bin/osascript as the language (as opposed to a Run NSApplescript) and 

set myHtmlText to "<b>bold example</b>"
do shell script "echo " & quoted form of myHtmlText & "|textutil -inputencoding UTF-8 -format html -convert rtf -stdin -stdout|LC_CTYPE=UTF-8 pbcopy"
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...