• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Insert Text Here
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Insert Text Here (From: Marconi <email@hidden>)

  • Prev by Date: Insert Text Here
  • Next by Date: Re: Insert Text Here
  • Previous by thread: Insert Text Here
  • Next by thread: Re: Insert Text Here
  • Index(es):
    • Date
    • Thread