Example Handlers using External Files
-- Suppose we have a list of names in the form first-name last-name, each
-- on a separate line. The following handler will create two external
-- data files, one containing a list of first names, the other a list of
-- last names.
on saveNames
put "Macintosh HD:AE Modules:first names" into firstFile
put "Macintosh HD:AE Modules:last names" into lastFile
open file firstFile
open file lastFile
-- if either file has a problem exit the program
if the result is not empty then
answer the result with "okay"
else
repeat with ct = 1 to number of lines in card field names
write word 1 of line ct of card field names & return to file firstFile
write word 2 of line ct of card field names & return to file lastFile
end repeat
close file firstFile
close file lastFile
end if
end saveNames
-- Suppose I need a handler that copies data line by line from one file
-- to another. The following handler does that.
on copyFile
put "Macintosh HD:AE Modules:text.dat" into sourceFile
put "Macintosh HD:AE Modules:text.copy" into targetFile
open file sourceFile
open file targetFile
-- if either file has a problem exit the program
if the result is not empty then
answer the result with "okay"
else
repeat until it is empty
-- read a line of data at a time
read from file sourceFile until return
-- if there is data to be read it is put in the variable 'it'
if it is not empty then
write it to file targetFile
end if
end repeat
close file sourceFile
close file targetFile
end if
end copyFile
-- As external conditions change, there may be a need to change
-- the names of external files. Here's one way to do this without
-- re-scripting the handler:
on versatileFile
-- The user is allowed to specify a file to write to, although a
-- default filename is provided. The following command also
-- allows the user to cancel the operation.
ask "Enter filename to write to:" with "Macintosh HD:AE Modules:versatile.dat"
if it is empty then
answer "No file specified -- operation terminating" with "okay"
else
put it into extfile
open file extFile
if the result is not empty then
answer the result with "okay"
else
write card field names to file extfile
close extfile
end if
end if
end versatileFile