Re: Saving info to a newly created text file from within a scri
Re: Saving info to a newly created text file from within a scri
- Subject: Re: Saving info to a newly created text file from within a scri
- From: Nigel Garvey <email@hidden>
- Date: Fri, 20 Jul 2001 15:09:36 +0100
kim wrote on Fri, 20 Jul 2001 10:25:50 +1200:
>
when writing to a file you really need to enclose it in a try block so if
>
it errors out, the file closes gracefully.
>
>
set thefile to ((path to desktop folder) & "Test file") as text
>
try
>
open for access file thefile with write permission
>
write "the text" to thefile
>
close access thefile
>
on error errmess
>
close access thefile
>
beep
>
display dialog "The file could not be written because of error: " &
>
errmess
>
end try
Ideally too, if you open a file for access, you should refer to it by the
returned reference:
set thefile to (path to desktop as text) & "Test file"
try
set fileRef to (open for access file thefile with write permission)
write "the text" to fileRef
close access fileRef
on error errmess
close access fileRef
beep
display dialog "The file could not be written because of error: " &
errmess
end try
If the error's caused by not being able to open the file (perhaps it's
already open with write permission), trying to close it again with the
non-existent reference will cause another error. In this case you could
indeed use the file specification:
close access file thefile
... but you'd need to be sure you weren't queering the pitch for whatever
process opened the file in the first place.