• 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: Long text file manipulation...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Long text file manipulation...


  • Subject: Re: Long text file manipulation...
  • From: Timothy Bates <email@hidden>
  • Date: Fri, 06 Apr 2001 14:58:03 +1000

> 'write "stuff" to file "blah" starting at eof'),

You need eof plus 1 here, or else you overwrite the last character!!!

i.e.,
set fpath to choose file name
set f to open for access (fpath) with write permission
write "FIRST" to f starting at (get eof f) + 1
write "second" to f starting at (get eof f)
write "THIRD" to f starting at (get eof f) + 1
close access f
set bingle to read fpath
bingle

> deleting something from
> the beginning or middle is an inherently expensive operation that
> requires someone to read everything in the file after the deletion point
> and write it back out again. Given the tools currently available in
> AppleScript, "someone" means your script. You don't have to read the
> entire file at once, so you can avoid a 2.5 MB memory hit, but you're
> pretty much stuck for it as far as time goes.
>
> Something like this will work fairly efficiently using only standard
> additions:
>
> set source to open for access file "foo"
> set destination to open for access file "bar" with write permission
> set eof destination to 0 -- empty out the destination file.
> read source until return -- discard first line
> try
> repeat
> read source for 16384 -- read 16K worth of what remains.
> write the result to destination
> end
> end
> close access source
> write "trailer" to destination
> close access destination
>
> You can twiddle the constant 16384 to alter the time vs. maximum memory
> profile. (Higher numbers should be faster, but will consume more
> memory.) If you want to mangle the file in place instead of generating
> an altered copy, the logic is significantly more difficult.
>
>
> --Chris Nebel
> AppleScript Engineering
> _______________________________________________
> applescript-users mailing list
> email@hidden
> http://www.lists.apple.com/mailman/listinfo/applescript-users

Dr Timothy Bates <mailto:email@hidden>
Macquarie Centre for Cognitive Science (MACCS)
Macquarie University
Ph 61 (2) 9850 8623
Fx 61 (2) 9850 6059


  • Follow-Ups:
    • Re: Long text file manipulation...
      • From: Chris Nebel <email@hidden>
References: 
 >Re: Long text file manipulation... (From: Christopher Nebel <email@hidden>)

  • Prev by Date: Re: Replacing whole lines in BBEdit
  • Next by Date: Confounding Issues with AppleScript
  • Previous by thread: Re: Long text file manipulation--BIGGER QUESTION
  • Next by thread: Re: Long text file manipulation...
  • Index(es):
    • Date
    • Thread