Scripting Additions Guide

CHAPTER 2
Scripting Addition Commands
82 Using Read/Write Commands
After it has stored, in the accumulatedSize variable, the total size of the
records preceding the point at which the new record is to be added, AddRecord
reads the remainder of the file and stores it in the postBuffer variable. It then
resets the file mark to the byte at which the new record is to be added by writing
an empty string to that location. After using the WriteNewRecord handler to
write the record, AddRecord writes the contents of the postBuffer variable
after the new record.
The WriteNewRecord handler shown in Listing 2-3 takes four parameters:
recordToAdd
A list of the fields for the record to be added.
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 "", WriteNewRecord includes a field delimiter
after each field it writes. If recordDelimiter is set to a delimiter,
WriteNewRecord includes a field delimiter after each field in a record but
the last and includes a record delimiter after the last field.
Listing 2-4 demonstrates one way to take advantage of the fact that the Open
for Access command can create a file with a specified name in a specified
location if the file doesn’t already exist at that location.
Listing 2-4 Opening a file for write access and creating one if the file doesn’t exist
on OpenFileIfItExists(theFile, writePermission)
try
(* if theFile doesn’t exist, Info For returns error -43 *)
set x to info for file theFile
if writePermission is true then
return (open for access file theFile with write permission)
else