Convert Unicode Text to Plain Text (or Boolean)

I haven’t seen this in Code Exchange, so here it is:

To convert Unicode text to plain text (and absolutely nothing else), use something like this:

set test to "Hello, World!" as Unicode text
return «class ktxt» of ((test as string) as record)

Once you have plain text, you can coerce to a boolean:

set test to "false" as Unicode text
return «class ktxt» of ((test as string) as record) as boolean

However, the “«»” characters (chevrons?) caused compiling problems in one of my AS Studio projects. I was able to use this instead:

strings of (myUnicodeText as string as record)

Edit: Changed “coerce” to “convert.”

Hi, Bruce.

There’s a nice variation on that, whose discovery is attributed to Arthur Knapp:

set test to "Hello, World!" as Unicode text
set {text:plainText} to (test as string)
return plainText

It’s not only very fast, but it can also be used where the input is already plain text, which saves having to use ‘try’ blocks. I’m not sure how or why it works.

Technically not coercion, but anyway…

One thing to note about this approach is that this won’t return accurate results if the Unicode text value contains any characters outside your system’s old-style primary text encoding (e.g. on English system that’d be MacRoman), which is what unstyled string values rely on. Where that’s an issue you should use something like the Satimage osax’s ‘extract string’ command, which’ll replace such characters with a “?” instead of mistranslating them. There’s also options like the TEC osax and TextCommands, although they require you to specify the target encoding yourself which can be an issue for portability.

[OT]Check your source file’s text encoding - it’s probably set to something like UTF8 (which’d explain why ASCII-only code still works ok) but AppleScript’s crusty old compiler isn’t Unicode-aware and requires source code given in your primary encoding.