C H A P T E R 3
Values and Constants
Unicode Text and International Text
5/5/99 ã Apple Computer, Inc.
AppleScript provides coercions among the Unicode Text, International Text, and
String (or Text) classes. For example, if your script gets a value of type Unicode
Text from an application that supports Unicode, you can coerce the value to
String to see it in a readable format. However, because the String, Unicode Text,
and International Text classes store data differently, as shown in Figure 3-1
(page 88), and because there are differences in the text data each can represent,
information may be lost in some coercions. As shown in Figure 3-3 (page 99),
coercions from Unicode Text to International Text or String, and from
International Text to String, may result in lost information. For example, the
String class cannot represent 2-byte Chinese characters (from either
International Text or Unicode Text values) in its 1-byte character format.
For an overview of AppleScript coercion, see “Coercing Values” (page 97).
The following script statements demonstrate how to initialize a string as
Unicode Text and then coerce it to a standard String value:
set myString to "hello" as Unicode text
--result: «data utxt00680065006C006C006F»
myString as string
The following script retrieves text data from an open AppleWorks word
processing document, adds to it, and replaces the existing text with the
combined text. Because the original text is stored as Chinese characters (entered
with the Chinese Language Kit), AppleWorks returns the text as International
Text, so the script doesn’t need to coerce it to International Text. After the script
completes, the document contains the original Chinese characters, followed by
“The End.” in the current language and script (English Roman). If the
appropriate language kit installed, AppleScript can display Chinese characters
in its result window.
tell application "AppleWorks"
-- Get text (Chinese characters) from open document.
set myText to text body of document "Chinese Text"
-- Add some information at the end in English.
set myText to myText & return & "The End."
-- Select all the current text in the document and replace it.
select text body of document "Chinese Text"
set selection of document "Chinese Text" to myText