Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [125]

By Root 1640 0
n to «data utxt03910313030103BB03BA03B703C303C403B903C2»

set n to n as Unicode text

tell application "Finder"

set name of folder "Mannie" to n

end tell

(To enter guillemets on a U.S. keyboard layout, type Option-\ and Option-Shift-\.) The result is shown in Figure 13-1. We've successfully given a folder the name of an Ancient Greek tragedy, creating that name ex nihilo in AppleScript.

Figure 13-1. A Finder item named with Unicode text

Another approach is to write the data out to a file and read it back in, which works because AppleScript gives you more ways to treat file data than it gives you to treat text data. Here's an example (on reading and writing files, see Chapter 21). We start with a decimal representation of the same bytes as in the previous example; we write these bytes to a file:

set L to {913, 787, 769, 955, 954, 951, 963, 964, 953, 962}

set s to (path to desktop as string) & "tempFile"

set f to a reference to file s

open for access f with write permission

repeat with aChar in L

write aChar to f as small integer -- two bytes per character

end repeat

close access f

If we were to open this file as UTF-16 in a word processor, we would see that we've successfully written out the desired string (Figure 13-2).

We can obtain this string by reading the file back into AppleScript as Unicode text:

set s to (path to desktop as string) & "tempFile"

set f to a reference to file s

open for access f

set s to read f as Unicode text

close access f

Figure 13-2. A Unicode text file generated in AppleScript

After that, s is the desired Unicode text. There is also support for exchanging data with a file as UTF-8 ; but there is no internal support for AppleScript text in UTF-8 encoding , so you have to express this as «class utf8», and if you read text as UTF-8, it is converted to UTF-16.

Still another approach is to talk to the shell. The do shell script scripting addition command returns Unicode text by default, so if you can get a Unix scripting language, such as Perl, to construct the string for you, you can obtain it. So:

set p to "print pack(\"U10\", 0x0391, 0x0313, 0x0301, 0x03BB, 0x03BA, " & ¬

"0x03B7, 0x03C3, 0x03C4, 0x03B9, 0x03C2);"

set s to do shell script "perl -e " & quoted form of p

After that, s is that same Unicode text. No doubt there's a better way to do this (there's always a better way to do things in Perl), but you get the idea.

Other Text Classes


Various older text classes, fudged into AppleScript (as I mentioned before) to grapple with the problem of encodings, are still around. These are generally to be avoided nowadays, though they can crop up occasionally.

For example, the international text class was a way of representing text in accordance with a particular language and script (where "script" means a writing system); each language-script combination had its own rules (an encoding) for how particular sequences of bytes were mapped to characters (glyphs). The mess created by this multiplicity of encodings is the reason why Unicode is a Good Thing.

The styled text class is another case in point. A style is an attribute of text, such as its font and size, whether it's underlined, that sort of thing. AppleScript defines a styled text class, but you can't manipulate it in any interesting way; in fact, you can barely even detect that it exists, because if you happen to encounter one and ask for its class, you're told it's a string. Nor is it used very much for representing style information; most applications that provide scriptable text styling use a more sophisticated class that lets you access and manipulate styles. Nevertheless, you might encounter styled text from time to time, such as when retrieving text data from the clipboard. You can detect that this has happened by coercing the text to a record:

tell application "Finder"

activate

set x to (the clipboard)

end tell

x as record

-- {«class ktxt»:"test",¬«class ksty»:«data styl000100000000000D000A00100000000C000000000000»}

As you can see, the string is actually made up of text information and

Return Main Page Previous Page Next Page

®Online Book Reader