Archive for October 4, 2003

Saturday, October 4, 2003 [Tweets] [Favorites]

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)