Re: as is what?
Re: as is what?
- Subject: Re: as is what?
- From: Nigel Garvey <email@hidden>
- Date: Mon, 25 Aug 2003 14:02:08 +0100
John Delacour wrote on Sun, 24 Aug 2003 14:25:00 +0100:
>
>set fileNum to open for access file "sarigama:as:test" with write
>
permission
>
>try
>
> set fileEnd to get eof fileNum
>
> write (a as text) & return to fileNum starting at (fileEnd + 1)
>
> set fileEnd to get eof fileNum
>
> write (b as text) & return to fileNum starting at (fileEnd + 1)
>
>on error errMsg
>
> close access fileNum
>
> error errMes
>
>end try
>
>close access fileNum
>
It is not necessary to get eof and so long as the file is open, all
>
new stuff will be written starting at eof, so that's not necessary
>
either.
... provided that - as here - the file has just been created by the 'open
for access' command. If you'll want to use the script again when the file
already exists, you'll want an action that either replaces the existing
data (if any) with new data:
set fileNum to open for access file "sarigama:as:test" with write
permission
try
set eof fileNum to 0
write (a as text) & return to fileNum
write (b as text) & return to fileNum
-- etc
... or appends new data to the end of the existing data (if any):
set fileNum to open for access file "sarigama:as:test" with write
permission
try
write (a as text) & return to fileNum starting at eof
write (b as text) & return to fileNum
-- etc
>
If you use a file specification
>
>
set f to "sarigama:as:test" as file specification
>
>
it is unecessary to bother with filenum and the use of filenum is
>
_always_ an unecessary complication with no advantages that I know of.
The advantages are:
1) File accesses are faster. With file specifications, every 'read',
'write', 'eof', and 'close access' command has to search for the
open-access reference (if any) associated with that file. If you provide
the reference number directly, it saves time
2) You can always be sure that any action you take applies to your
particular access to the file. A file can be open for access by several
processes at once (though only one can have write permission). By using
your own access reference, you guarantee that you're using your own
file-position pointer and closing your own access. With file
specifications, the access reference used is the first one found for the
file, which may not be yours. This can screw things up both for your
script and for the process whose access you've hijacked. There are
obviously situations where this is relevant and some where it isn't, but
you can't be sure when you write the script which are going to occur.
'Read', 'write', 'get eof', and 'set eof' (+ file specification) also
work when the file hasn't been opened for access at all. In this case -
when no open-access reference is found - the file is opened, has the
relevant action performed upon it, and is closed again. This is the
fastest way of performing one-off actions on files (or was when I last
checked), but is subject to the vulnerabilities described above.
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.