Re: Insert Text Here
Re: Insert Text Here
- Subject: Re: Insert Text Here
- From: Deivy Marck Petrescu <email@hidden>
- Date: Sat, 21 Jan 2012 12:34:05 -0500
On Jan 21, 2012, at 12:04 PM, Marconi wrote:
> I need to open a text file for writing but, instead of appending text, I need to insert text at a given point.
>
> Suppose I have a text file that is like this:
>
> File starts here
> some more stuff
> insert text here.
> some more stuff too
> end of the stuff
>
>
> And suppose I wanted to insert some text ("this has been inserted") after the designator "insert text here" such that the result is:
>
>
> File starts here
> some more stuff
> insert text here.
> this has been inserted
> some more stuff too
> end of the stuff
>
> Is there a way, when writing a text file, to insert text rather than appending or overwriting?
>
>
> "A man who believes in nothing will fall for anything." -- Malcolm X
>
Yes! And No!
You can not insert text, because it would overwrite anything after the insertion point.
What you can do, is to actually change the text of the file.
So, read the file and save its text in a variable.
Using whatever text manipulation you choose, change the original text to the new text.
If, as you indicate in your example, the final text is longer than the original then you just write the new text from the beginning of the file.
A NOTE: You don't need to open for access to read or to rite to a file.
There are advantages and disadvantages to doing this, however in your case advantages beat disadvantages.
Deivy Petrescu
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden