Re: Read / Write with Files
Re: Read / Write with Files
- Subject: Re: Read / Write with Files
- From: Andrew Oliver <email@hidden>
- Date: Wed, 30 Jun 2004 17:51:22 -0700
'write' will absolutely do this - it blindly writes data to the file,
overwriting whatever was there before, and as you've found 'read' has its
own limitations, too.
A better all-round solution is to use 'offset of...' to find the point in
your file where you want to insert the text, then build a new file contents
that you write out to the file. Something like (watch for linebreaks!):
>
set theFile to "Macintosh HD:path:to:file.txt
>
set fileContents to read file theFile
>
set insertPt to offset of "keyword" in fileContents
>
set newContents to ((characters 1 through (insertPt - 1)) of fileContents) &
"new text" & (characters insertPt through end of fileContents) as text
>
set newFile to open for access file "Macintosh HD:path:to:file.txt" with write
permission
>
write newContents to newFile starting at 0
>
close access newFile
Andrew
:)
On 6/30/04 3:10 PM, "Andersen Studley" <email@hidden> wrote:
>
First off, thanks to Tom for his reply that helped me get going.
>
>
Have a couple more specific questions now...
>
>
I'm trying to inject text at a certain place in a text file. I had
>
hoped to be able to scan up to a unique set of characters and use that
>
as my insertion point, but the only command I've found that sounds like
>
it would work is read and it only searches for a single character
>
>
read: Read data from a file that has been opened for access
>
read anything -- the reference number, alias, or file reference of
>
the file to read
>
[until plain text] -- or read up to and including this character
>
>
Is there anyway to find out which characters are permissible? In my
>
test script I tried to use some of the "special" characters so that
>
they would be unique, but they were skipped over.
>
>
What is the newline character? Putting in "\n" simply dropped my code
>
down a line without affecting my position in the file.
>
>
>
More troublesome, however, is that write myContent to myFile
>
stomps over whatever was there. for example, if I have a file
>
contain containing "I ran tonight" and want to insert "fast" into it,
>
I end up with "I ran fastght"
>
>
Am I missing better commands for these two functions? Any advice would
>
be appreciated
>
>
>
Andersen Studley
>
_______________________________________________
>
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.
_______________________________________________
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.