Re: Beginner here
Re: Beginner here
- Subject: Re: Beginner here
- From: has <email@hidden>
- Date: Thu, 23 May 2002 16:58:15 +0100
Mike Galke wrote:
>
2) How would I even BEGIN to go about creating a script that would find and
>
replace various unwanted items, such as double-spaces, those little (>>)
>
characters, usually found in emails, and/or create a (-) instead of (--).
Here is the canonical Find-and-Replace routine of the AppleScript world:
======================================================================
on textReplace(theText, srchStrng, replStrng)
try
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to srchStrng
set tempList to theText's text items
set AppleScript's text item delimiters to replStrng
set theText to tempList as string
set AppleScript's text item delimiters to oldTID
return theText
on error errMsg number errNbr
set AppleScript's text item delimiters to oldTID
error errMsg number errNbr
end try
end textReplace
======================================================================
It uses AppleScript's text item delimiters (wonderful things) to break the
text up into a list of strings, then reconstitutes it using a different
delimiter string. (If you want to get a better notion of how it works, take
a look at the contents of the tempList variable halfway through.)
GOOD: simple, fast
BAD: can't do case-insensitive matches, breaks on strings containing > 4000
matches due to a "stack overflow" problem that occurs when converting the
string to a list [more advanced versions can work around these problems]
--
Using the above, you could remove double spaces from text by calling it
from a repeat loop that keeps going until the text that comes back is the
same as the text that goes in:
======================================================================
on removeDoubleSpaces(theText)
set oldText to missing value
repeat until theText is equal to oldText
set oldText to theText
set theText to textReplace(theText, " ", " ")
end repeat
return theText
end removeDoubleSpaces
======================================================================
There's plenty other options too: you could use a scripting addition which
provides a find-and-replace facility, e.g. Satimage osax, or a scriptable
application such as BBEdit. There's a library on my site [address below],
stringLib, that contains three vanilla Find & Replace routines, one of
which provides case-insensitive searches (though currently only for
MacRoman character sets):
======================================================================
-- load stringLib into a global variable for easy access
global stringLib
set stringLib to load script (file "[path to stringLib here]")
--TEST CODE
set theText to "foo Foo FOO"
set srchStrng to "foo"
set replString to "bar"
tell stringLib to findAndReplace(theText, srchStrng, replString, {iCase:true})
--> "bar bar bar"
======================================================================
(Note: stringLib is also immune to stack overflow problems; a wise
precaution if you're working with strings over a few KB.)
If you need to perform complex matching operations (e.g. match entire words
only, match any number of repeating characters) then you'll probably need
to use Regular Expressions, a powerful text matching system. (e.g. The
Satimage osax and BBEdit both support regex as well as plain
find-and-replace.)
For example, you could replace multiple spaces by matching the pattern "
+" (two spaces and a plus sign) and replacing with a single space. They can
take a little getting used to, but they're widely used in general-purpose
text editors and other scripting/programming languages, and you can find
plenty of information online that will get you started [there tends to be
some minor differences between various implementations, but the basics are
pretty much universal].
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.