Scripting Additions Guide

CHAPTER 2
Scripting Addition Commands
Using Read/Write Commands 77
The DeleteRecord handler shown in Listing 2-2 takes five parameters:
numberOfFields
The number of fields in each record.
whichRecord
An integer that identifies the position of the record you want
to delete.
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.
Like the ReadRecord handler in Listing 2-1, the DeleteRecord handler reads
the specified number of fields for each record if recordDelimiter is set to
"". If recordDelimiter is set to a delimiter, DeleteRecord 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.
When it reaches the record to be deleted, DeleteRecord stores the contents of
accumulatedSize in the preRecordSize variable, reads through the record
to set the file mark, reads from the file mark to the end of the file, and stores
that portion of the file in the fileBuffer variable. Finally, DeleteRecord
writes the contents of fileBuffer starting at the beginning of the record to
be deleted.
Listing 2-3 demonstrates how you can use similar techniques to insert a record
into a text-based database file.
Listing 2-3 Inserting a record in a database file
--choose file to work with
set pathToUse to choose file