Scripting Additions Guide
Table Of Contents
CHAPTER 2
Scripting Addition Commands
74 Using Read/Write Commands
The ReadRecord handler shown in Listing 2-1 takes five parameters:
numberOfFields
The number of fields in the record.
whichRecord
An integer that identifies the position of the desired record.
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 recordDelimiter is set to "", the ReadRecord handler reads the specified
number of fields for each record. If recordDelimiter is set to a delimiter,
ReadRecord reads all the fields in a record but the last, then reads the last field
up to the record delimiter. This is necessary to ensure that the last field of one
record is not combined with the first field of the next.
The ReadRecord handler reads each new record into the variable
recordData. If the record is the one requested, ReadRecord returns
that record. If the record is not the requested record, ReadRecord sets
recordData to an empty list and reads the next record.
You can use similar techniques to locate the exact position of a record you want
to delete from a text file. In addition to locating the record to be deleted, you
need to store all the records after that record in a variable and write the
contents of the variable starting at the beginning of the record to be deleted.
You can then use the Get EOF and Set EOF commands to get the initial size of
the file and reset its size after deleting the record. Listing 2-2 demonstrates how
to do this.
Listing 2-2 Deleting a record from a text-based database file
--choose data file to use
set pathToUse to choose file