• 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: search and replace
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: search and replace


  • Subject: Re: search and replace
  • From: Arthur J Knapp <email@hidden>
  • Date: Fri, 22 Mar 2002 14:07:28 -0500

> From: Brian Mather <email@hidden>
> Subject: search and replace
> Date: Fri, 22 Mar 2002 10:40:14 -0500

> tell application "Finder"
> set the_file to choose file -- for development
> set foo to read the_file
> set got_it to the offset of "sometext" in foo
> if got_it is not 0 then
> beep 3 -- development again
> set file_mark to got_it
> set munger to open for access file (the_file) with write
> permission
> write "some_other_text" to munger starting at file_mark
> close access munger

> problem is that the write command overwrites things instead of inserting
> them. I'm sure some reg ex stuff would be more elegant but I don't know the
> syntax and hadn't the time to learn it last night.
>
> Any thoughts on how I could isolate the retrieved text and replace it with
> the new text correctly?

If you have the memory, the simplest way to do this in AppleScript is
simply to read the file in, process it's contents, set eof to 0, and
then write the whole file out from scratch:

-- Warning: no error-checking...

on ProcessTextFile( file_spec )

set open_file to open for access file_spec with write permission

set file_contents to read open_file

set file_contents to ProcessString( file_contents ) --> modify

set eof open_file to 0 --> delete file's contents

write file_contents to open_file

close access open_file


If you don't have the memory, then things get more complicated. In
AppleScript, it's best to just to have lots of memory available. ;-)



{ Arthur J. Knapp, of <http://www.STELLARViSIONs.com>
<mailto:email@hidden>
try
<http://members.bellatlantic.net/~bswaldie/>
on error number -128
end try
}
_______________________________________________
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.

  • Prev by Date: Was: Is the picture_folder of the screensaver in OS X scriptable?
  • Next by Date: Re: Problems with files on OS X
  • Previous by thread: search and replace
  • Next by thread: Re: search and replace
  • Index(es):
    • Date
    • Thread