Re: search and replace
Re: search and replace
- Subject: Re: search and replace
- From: has <email@hidden>
- Date: Fri, 22 Mar 2002 22:03:10 +0000
Brian Mather wrote:
>
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?
To amend your current read/write-based approach:
1. Read from the old file up to the point where the text to change starts.
Write that content into a new file.
2. Write your replacement text to the end of the new file.
3. Read your old file from the point where the text to change ends. Write
that text to the end of the new file.
4. Replace the old file with your new file.
--
A simple find-and-replace approach might use a TID-based find-and-replace
routine to make the change, then write the new text to your existing file
(remembering to set the file's EOF to 0 first).
======================================================================
on tidFindAndReplace(theString, findString, replaceString)
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to findString
set tempList to theString's text items
set AppleScript's text item delimiters to replaceString
set theString to tempList as string
set AppleScript's text item delimiters to oldTID
theString
end tidFindAndReplace
======================================================================
Like the 'offset' command, this routine is case-sensitive. Fast, efficient,
case-insensitive find-and-replace routines are a bit harder to do, though
I've got one kicking around here if you want it (only problem with it is
that it's designed for the MacRoman character set, so won't work correctly
without modification if you're using some other character set on your Mac).
Then there's osaxen, scriptable text editors, etc... but no doubt you know
all that already.:)
HTH
has
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.