You can add color to your display dialogs! Yes, Really

Here’s one I did.

I wanted to add color to a title, so I tried HTML and CSS. It didn’t work so then I tried emoji and that was the way ahead.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set debug to false
set Keycap00 to «data utxt0030FE0F20E3» as Unicode text
set Keycap01 to «data utxt0031FE0F20E3» as Unicode text
set Keycap02 to «data utxt0032FE0F20E3» as Unicode text
set Keycap03 to «data utxt0033FE0F20E3» as Unicode text
set Keycap04 to «data utxt0034FE0F20E3» as Unicode text
set Keycap05 to «data utxt0035FE0F20E3» as Unicode text
set Keycap06 to «data utxt0036FE0F20E3» as Unicode text
set Keycap07 to «data utxt0037FE0F20E3» as Unicode text
set Keycap08 to «data utxt0038FE0F20E3» as Unicode text
set Keycap09 to «data utxt0039FE0F20E3» as Unicode text
set Keycap10 to «data utxtD83DDD1F» as Unicode text
if debug then
set Globe to SurrogatePairCalculation("U+1f30e") --> «data utxtD83CDF0E» as Unicode text
else
set Globe to «data utxtD83CDF0E» as Unicode text --> See just above
end if
set Button1 to «data utxtD83DDED1» as Unicode text -->Stop sign
set Button2 to «data utxtD83CDDFAD83CDDE6» as Unicode text -->Ukraine
set Button3 to «data utxtD83CDD97» as Unicode text -->OK sign   
set Face to «data utxtD83DDE03» as Unicode text --> Smiling Face

display dialog Keycap00 & Keycap01 & Keycap02 & Keycap03 & Keycap04 & Keycap05 & ¬
Keycap06 & keycap07 & Keycap08 & keycap09 & Keycap10 & Globe ¬
buttons {Button1, Button2, Button3} ¬
cancel button 1 default button 3 with icon note with title Face

if button returned of result is "🆗" then
set tabsx2 to tab & tab
set tabsx4 to tabsx2 & tabsx2
display dialog tabsx2 & («data utxt0032FE0F20E3D83DDC1D222826D40032FE0F20E3D83DDC1D21942753»     ¬
	as Unicode text) & return & tabsx4 & («data utxt2014D83DDCB5D83EDD1D2197» ¬
	as Unicode text) buttons {Button3} default button 1 ¬
	with icon stop with title («data utxtD83CDCCF» as Unicode text)
end if

Surrogate pairs are a necessary tool for handling emoji. The pairs deconstruct 5+ character hexidecimal into a high 4 character hex number and an low 4 character hex number, which are concatenated together (H&L) as a replacement (hence surrogate) set. I stole the algorithm from Russell Cottrell’s website.. My code faithfully returns the surrogates, but returns them as a string, not as utxt data. If anyone know how to coerce the string to data, please advise. This is what I get when using the calculator (set debug to true).


Note the included string vice the utxt data. This is the calculator’s code:

on SurrogatePairCalculation(S)
--The Surrogate Pair Calculator
--http://russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm
set C1 to 65536 -->Hex2Dec("10000")
set C2 to 1024 -->Hex2Dec("400")
set C3 to 55296 -->Hex2Dec("D800")
set C4 to 56320 -->Hex2Dec("DC00")
set S to hex2dec(S)
set H to dec2hex(((S - C1) div C2) + C3, 0, 2, 1) -- Integer Division
set L to dec2hex(((S - C1) mod C2) + C4, 0, 2, 1) -- Modular Division
set ucode to "«data utxt" & H & L & "» as Unicode text"
log class of ucode
return ucode
end SurrogatePairCalculation

For completeness’s sake here are the hex2dec and dec2hex code.

on hex2dec(nHex)
set dquo to character id 34
set str1 to "abcdefABCDEF0123456789"
set str2 to "perl -e 'printf(hex(" & dquo
set str3 to dquo & "))'"
set nHex to the items of nHex

repeat while item 1 of nHex is not in str1
	set nHex to the rest of nHex --strips others
end repeat
set nHex to nHex as text
set nDec to (do shell script str2 & nHex & str3) as number
return nDec
end hex2dec

on dec2hex(nDec, flag, pad, caps)
set dquo to character id 34
set Flags to {"U+", "0x", "0X", "&H", "&#x", "u", "U"}
if flag = 0 then
	set str1 to ""
else if flag > 0 and flag ≤ (count of Flags) then
	set str1 to item flag of Flags
else
	set str1 to "?"
end if

set str2 to "perl -e 'printf(" & dquo & "%"

if pad = 0 then
	set str3 to ""
else
	set str3 to "0" & pad
end if

if caps = 1 then
	set str4 to "X" & dquo & ","
else
	set str4 to "x" & dquo & ","
end if

set str5 to ")'"

set nHex to str1 & (do shell script str2 & str3 & str4 & nDec & str5)
return nHex
end dec2hex

Can someone please tell us how to replace a string facsimile with real utxt data?

I’ll write it in one line. We can input emoji from macOS emoji palette.

display dialog "0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣🌏" with title "😃" buttons {"🛑", "🇺🇦", "🆗"}

Wow, I didn’t know that. Thanks. Much easier.

A working version of SurrogatePairCaculation is below:

on SurrogatePairCalculation(S)
--The Surrogate Pair Calculator
--http://russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm
--http://books.gigatux.nl/mirror/applescriptdefinitiveguide/applescpttdg2-CHP-13-SECT-8.html
--https://macscripter.net/viewtopic.php?id=41341
set C1 to 65536 -->Hex2Dec("10000")
set C2 to 1024 -->Hex2Dec("400")
set C3 to 55296 -->Hex2Dec("D800")
set C4 to 56320 -->Hex2Dec("DC00")
set S to hex2dec(S)
set H to dec2hex(((S - C1) div C2) + C3, 0, 2, 1) -- Integer Division
set L to dec2hex(((S - C1) mod C2) + C4, 0, 2, 1) -- Modular Division
set ucode to "«data utxt" & H & L & "» as Unicode text"
set hexdata to run script ucode
log hexdata
return hexdata
end SurrogatePairCalculation

The fix was to run the assembled string as a script. So close. Worth remembering.