Saturday, October 4, 2003

Unicode AppleScript String Literals

This bit of Python translates Unicode strings into AppleScript literals. It’s also useful for escaping any special characters.

def unicodeToAppleScriptLiteral(s):
    from binascii import hexlify
    data = hexlify(s.encode('utf-16-be'))
    left = u"\N{LEFT-POINTING DOUBLE ANGLE QUOTATION MARK}"
    right = u"\N{RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK}"
    return u"(%sdata utxt%s%s as Unicode text)" % (left, data, right)

>>> unicodeToAppleScriptLiteral(u"hello")
(«data utxt00680065006c006c006f» as Unicode text)

2 Comments RSS · Twitter


For Intel compatibility, you can replace "utxt" and "utf-16-be" with "utf8".


In TextMate, there is a command ("Toggle String / «data utxt»") which does this when the insertion point is in a string (just press ⌃⇧'). I suppose we should fix it to use utf8 now too.

Leave a Comment