DekGenius.com
Team LiB   Previous Section   Next Section
data

Allowed coercion

list with one item, as in {<<data utxt00650061007200740068>>}

Syntax

Set theVar to "earth" as Unicode text (* returns <<data 
utxt00650061007200740068>> *)

Description

data is a value type that can be used to store data that cannot be stored using any of the other AppleScript value types.

The Script Editor Result window displays raw data surrounded by double-arrow or guillemet characters ("<< >>"). You can produce these symbols on the Macintosh keyboard by typing option-backslash ("<<") and option-shift-backslash (">>").

For example, in OS 9 Unicode text is an AppleScript value type that is displayed as raw data in the Script Editor Result window (even though it is still stored as type Unicode text). However, AppleScript 1.6 with Mac OS 9.1 and OS X can display Unicode text as regular strings (as in "Hello"). The following example displays a lowercase "u" character as Unicode text:

set ucode to "u" as Unicode text

The Script Editor Result window will return the value as "<<data utxt0075>>."

Unicode text uses two bytes (16 bits) per character to store strings. (See later in this section for more details on the Unicode text class.)

Within the guillemets, the word "data" is followed by a space then a four-character code representing the Unicode text class ("utxt"). The actual data representing the lowercase "u" precedes the closing guillemet character ("0075>>"). The lowercase "u" is represented in the ASCII table as the number 75 in hexadecimal form (117 in decimal). It only takes one byte to store a "u"; the one byte is represented by the "75" portion of the data return value ("<<data utxt0075>>"). The rest of this data ("00"), representing the unused extra byte, is two zeros.

Raw Syntax

You can also use raw syntax to represent data and commands in your own scripts. For example, AppleScript recognizes the term <<class cfol>> as the equivalent of the word folder, which is the Apple event object that represents a folder on your computer. The class part of this data structure stands for the data type, and cfol represents the four-character code for folder (remember our Chapter 1 discussion of four-character codes for class types?).

It is normally preferable to use natural language terms ("folder") for these objects, except for when a built-in AppleScript term or a term from an application's dictionary does not exist for a command you want to execute. While these situations are rare or nonexistent, one of them occurs when you want to send an Apple event to a program, and you do not know the human-language command to use for the event (or there isn't an AppleScript term for the command). For example, you might have developed an application that handles certain Apple events, but the program doesn't have a dictionary yet. However, you still want to test how it deals with Apple events sent by AppleScript. You could then use the following raw syntax to send a get Apple event to the program "myapp" requesting its version property:

tell application "myapp" to<<event coregetd>> version

This expression encloses the word "event" in guillemets ("<<>>") followed by a space and the get command's event class ("core") and event id ("getd") pushed together. Recall from Chapter 1 that every Apple event is distinguished by its event class (the suite or category of events that it is part of) and its individual event id. In other words, the previous example uses raw syntax instead of the conventional coding style:

tell application "myapp" to get version

In another example, you could substitute the raw syntax:

<<class cdis>> "scratch"

for the AppleScript code reference:

disk "scratch"

representing a mounted volume on the desktop with the name "scratch." The four characters "cdis" represent the Apple event object code for the disk class.

Where do you find out about all of these event and class codes? The AppleScript software development kit (SDK) includes a FileMaker Pro file that contains event and class codes for all events and objects associated with Mac OS 8.5 (this SDK had not been updated as of January 2001). Go to http://developer.apple.com/sdk for more SDK info.

Examples

You can use raw syntax to represent data, as in the following examples (the question is, do you really want to?):

<<class file>> "bigfile.txt" (* a form of file "bigfile.txt" that refers to a 
file called "bigfile.txt" *)
<<data utxt0025>> as string -- returns the percent sign ("%")
<<event aevtodoc>> file "bigfile.txt" (* In AppleScriptese, open file 
"bigfile.txt"
 *)
    Team LiB   Previous Section   Next Section