Re: File I/O
Re: File I/O
- Subject: Re: File I/O
- From: email@hidden
- Date: Fri, 11 Jan 2002 03:04:24 EST
Bless you for asking. Using Applescript to read and write text files is,
IMHO, dramatically underappreciated. I have created surprisingly powerful
pseudodatabases which blow away anything else I have tried for speed,
stability, flexibility and freedom from corruption.
The following 4 handlers have been battle tested and are believed to be 100%
bug free. They may appear to have excessive error handling, but they are
absolutely mission critical. They were tested and used on MacOS 8.6, 9.04 and
9.1. Akua Sweets recommended.
-- theFile is a full file path, eg. "HD:folder1:folder2:filename.txt"
---------------------------------------------------
on closeAccess(theFile)
try
close access file theFile
on error --
end try
end closeAccess
on readFile(theFile)
my closeAccess(theFile)
set theText to ""
try
set transID to open for access file theFile
set myeof to get eof transID
if myeof is greater than 0 then set theText to read transID
close access transID
on error errmsg
try
close access transID
on error --
end try
end try
return theText
end readFile
on writeFile(theFile, theText, wipeclean)
my closeAccess(theFile)
try
set transID to open for access file theFile with write permission
if wipeclean then set eof transID to 0
write theText to transID starting at 1
close access transID
on error
try
close access transID
on error --
end try
try
set folderPath to my extractFolderPath(theFile)
verify path folderPath -- Akua Sweets 1.4.1; 1.4.3 should work
set transID to open for access file theFile with write permission
if wipeclean then set eof transID to 0
write theText to transID starting at 1
close access transID
on error
try
close access transID
on error --
end try
end try
end try
end writeFile
-- used only by writefile()
on extractFolderPath(theFile)
try
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theFolderPath to ("" & (text items 1 thru -2 of theFile))
set AppleScript's text item delimiters to oldDelims
on error
try
set AppleScript's text item delimiters to oldDelims
on error
set AppleScript's text item delimiters to {""}
end try
set x to -3
if text x thru -2 of theFile contains ":" then
set x to -2
else
set x to x - 7
repeat until text x thru -2 of theFile contains ":"
set x to x - 7
end repeat
set x to x + 7
end if
repeat until character x of theFile is equal to ":"
set x to x - 1
end repeat
set theFolderPath to (text 1 thru x of theFile) as string
end try
return theFolderPath
end extractFolderPath
-----------------------------------------------------
Jeff Baumann
email@hidden
www.linkedresources.com
In a message dated 1/8/02 5:49:15 PM, Stephen wrote:
>
I am relatively new to AppleScript and need some
>
simple ways to just read and write strings to and from
>
text files.