Scripting Additions Guide
Table Of Contents
CHAPTER 2
Scripting Addition Commands
72 Using Read/Write Commands
You can use the Read/Write commands with either text-based data or binary
data. Most databases can export data as text, with fields and records separated
by delimiters, and some store their data as text files. The examples in this
section demonstrate how to use the Read/Write commands with text-based
data. These examples assume that you have basic information about the way
the data is stored in a text file, such as the delimiters used to separate fields and
records. You can use similar techniques to read and write binary data if you
know how the data is organized within a file. For example, if you know the
header format for a file of type 'PICT', you can write scripts that read and
write to 'PICT' files.
Both the Read command and the Write command make use of the file mark, a
marker used by the File Manager that indicates the byte at which the Read and
Write commands begin operating. By default, the file mark is the first byte of
the file. After the Read command reads a range of bytes or the Write command
writes over a range of bytes, the file mark is set to byte just after the end of that
range. The next Read or Write command begins operating at the new file mark.
For example, suppose you want to extract a particular record from a text-based
database of names and addresses. To do so, you need to know the number of
fields in each record, the position of the desired record in the database, and the
delimiters used to separate the records in the database. You can then use the
Open for Access command to get a file reference number for the file that
contains the desired record and a Read command within a repeat loop to read
each successive record. After reading each record, the Read command sets the
file mark to the beginning of the next record. When the repeat loop determines
that the desired record has been reached, it returns the data for that record.
Listing 2-1 shows one way to do this.
Listing 2-1 Reading a specific record from a text-based database file
--first choose data file to work with
set pathToUse to choose file
try
set x to open for access pathToUse
set z to ReadRecord(10, 1, tab, return, x)
close access x
z --display requested record
on error errString number errNum