Scripting Additions Guide

CHAPTER 2
Scripting Addition Commands
Using Read/Write Commands 81
display dialog errString
end try
end WriteNewRecord
The AddRecord handler shown in Listing 2-3 takes five parameters:
recordToAdd
A list of the fields for the record to be added.
whichRecord
An integer that identifies the offset of the record you want
to add.
fieldDelimiter
The delimiter used in the file to separate fields.
recordDelimiter
The delimiter (if any) used to separate records. If the file doesn’t
use a different delimiter to separate records, this parameter
must be set to "".
fileRefNum
A file reference number obtained with the Open for Access
command.
If the new record is to be added at the beginning of the file, AddRecord reads
all the records in the file and stores them in the postBuffer variable, then
resets the file mark to the beginning of the file by writing an empty string to
that location. This is a useful technique whenever you want to set the file mark
without reading or writing any data.
Next, AddRecord uses the WriteNewRecord handler to write the record at
the beginning of the file and writes the contents of the postBuffer variable
after the new record. Note that the Write Command sets the end of file, so this
example doesn’t need to use the Get EOF and Set EOF commands.
If the new record is to be added somewhere other than at the beginning of the
file, AddRecord uses a repeat loop to read through all the records that precede
the new record’s location. If recordDelimiter is set to "", AddRecord reads
the specified number of fields for each record. If recordDelimiter is set to a
delimiter, AddRecord reads all the fields in a record but the last, then reads the
last field up to the record delimiter. The size of each successive record is added
to the accumulatedSize variable, which contains the total size of the
previously read records.