Re: Very basic "write to file" query
Re: Very basic "write to file" query
- Subject: Re: Very basic "write to file" query
- From: email@hidden
- Date: Thu, 5 Feb 2004 08:03:54 +0000
Richard,
That did the trick.
Strangely, in the whole of my application, this simple routine comes up
in 5 places, and in one instance, I had omitted "file" in "open for
access file file_ with write permission", and as you say, it worked
"some of the time"... but not this time!
I've also taken in the rest of your tips.
Many thanks again.
It works fine in Panther... and someone is getting error "(-48)
duplicate filename" in Jaguar...
try
set file_ to ((path to desktop) & "test file") as Unicode text
open for access file_ write permission 1
The variable 'file_' is a Unicode string. 'open for access' asks for
a file spec (or whatever it's called these days ;). What you have
works for some of the people some of the time, but this is reliable -
from System 7.1 onwards:
open for access file file_ with write permission
copy the result to file_reference
You can also eliminate this line by setting the variable directly in
the previous line:
set file_reference to open for access file file_ with write permission
write "test" starting at 0 to file_reference
'starting at 0' is not required if that's what you want to do, but
either way it could get messy if the file already exists & has more in
it than "test". If you have a file that contains the text "popsicles"
and you write to it using the line above, you will end up with, ah, a
different word.
The safe way to overwrite an existing file is to:
set eof of file_reference to 0
Then:
write "test" to file_reference
If you wish to append the text to any existing contents, use this:
write "test" to file_reference starting at eof
close access file_reference
on error err_msg
close access file file_
display dialog err_msg buttons {"-"} default button 1
FWIW, another way of handling this or any other error is to propagate
it up the chain. Instead of displaying a dialog, just pass the error
on:
on error err_msg number err_num
close access file file_
error err_msg number err_num
end try
--DP
_______________________________________________
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.