Accented text to HTML

I have a few small scripts to paste a hyperlink into rtf documents. They produced garbled text when the link text contained accented letters, which until recently didn’t bother me enough. Then I got bothered enough, and searched MacScripter for a solution. I couldn’t find one, so I’m posting mine.

Here is one of these scripts: it puts a link for a Contacts card onto the clipboard.
The handler to convert accented characters to HTML code is the recent addition.
A table listing those codes can be found here.

(*
	This is a Vcard "URL":
	addressbook://357A63B8-077A-4464-84FE-51182EE1A:ABPerson
	
	In OmniFocus you can create a clickable link by dragging a card into a note field.
	This does not work in TextEdit, but a link made by hand will work.
	Both will open the card when clicked.
*)

tell application "Contacts"
	set thisCard to properties of item 1 of (get selection)
	set {lastName, firstName, cardID} to {last name, first name, id} of thisCard
end tell

-- preprocess the text
set firstName to textToHTMLencode(firstName)
set lastName to textToHTMLencode(lastName)

-- use the retrieved data to create the link:
set txtTitle to firstName & space & lastName
set txtURL to "addressbook://" & cardID
-- and copy to clipboard 
if (txtURL is not "") and (txtTitle is not "") then
	set txtHTML to "<font size=4><a href=\"" & txtURL & "\">" & txtTitle & "</a></font>"
	do shell script "echo " & quoted form of txtHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
end if

on textToHTMLencode(textin)
	set textout to ""
	repeat with i from 1 to count textin
		set c to text i of textin
		set cid to id of c
		if cid < 127 then
			set textout to textout & c
		else
			set textout to textout & "&#" & cid & ";"
		end if
	end repeat
	return textout
end textToHTMLencode

Although that will probably work fine for your case, some characters return a list of values for id, rather than a single value. Here’s an AppleScriptObjC alternative:

use AppleScript version "2.5" -- requires 10.11 or later
use framework "Foundation"
use scripting additions

on textToHTMLencode(textin)
	set theString to current application's NSString's stringWithString:textin
	return (theString's stringByApplyingTransform:"[^\\x20-\\x7e]-Hex/XML10" |reverse|:false) as text
end textToHTMLencode

And FWIW, here’s an ASObjC version of your whole script:

use AppleScript version "2.5" -- requires 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

tell application "Contacts"
	set thisCard to properties of item 1 of (get selection)
	set {lastName, firstName, cardID} to {last name, first name, id} of thisCard
end tell

set linkTextStr to my textToHTMLencode(firstName & space & lastName)
set urlStr to "addressbook://" & cardID

set linkURL to current application's |NSURL|'s URLWithString:urlStr
-- define attributes to apply
set theAtts to current application's NSDictionary's dictionaryWithObjects:{linkURL, current application's NSColor's linkColor(), get current application's NSSingleUnderlineStyle} forKeys:{current application's NSLinkAttributeName, current application's NSForegroundColorAttributeName, current application's NSUnderlineStyleAttributeName}
-- make attributed string
set attString to current application's NSAttributedString's alloc()'s initWithString:linkTextStr attributes:theAtts
-- need it in RTF data form for clipboard
set theDict to current application's NSDictionary's dictionaryWithDictionary:{DocumentType:current application's NSRTFTextDocumentType}
set rtfData to attString's RTFFromRange:{0, attString's |length|()} documentAttributes:{DocumentType:current application's NSRTFTextDocumentType}
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
-- set  clipboard
pb's clearContents()
pb's setData:rtfData forType:(current application's NSPasteboardTypeRTF)

on textToHTMLencode(textin)
	set theString to current application's NSString's stringWithString:textin
	return (theString's stringByApplyingTransform:"[^\\x20-\\x7e]-Hex/XML10" |reverse|:false) as text
end textToHTMLencode