Re: Faster way to replace text in AppleWorks?
Re: Faster way to replace text in AppleWorks?
- Subject: Re: Faster way to replace text in AppleWorks?
- From: Kai Edwards <email@hidden>
- Date: Tue, 01 Oct 2002 12:50:00 +0000
on Sun, 29 Sep 2002 21:40:52 +1000, Dale Gillard <email@hidden>
wrote:
>
I have written a script that loops thru an AppleWorks document looking
>
for specific characters and replacing them eg ensuring a period/full
>
stop has a single space after it. As you can imagine, it takes quite a
>
while to do this even in a document of only a few hundred words.
>
>
Does anyone have any advice on how I could speed this up this process?
>
eg a 'you beaut' algorithm that's really fast at doing this rather than
>
my 'naive' algorithm.
>
>
Here's a snippet of the script so you can get an idea of what I'm doing:
snippet snipped - apart from:
>
set space to " "
Just a small point before we get onto the more interesting stuff. ;-)
It's not necessary to set the value of 'space', since it's already one of
AppleScript's string constants (like 'tab' and 'return').
Anyway, in spite of some excellent advice from Michelle and Jay on this
subject, I thought I'd add my own 2 cents (FWIW).
Firstly, I had a little difficulty getting a 'global' approach (converting
an entire document in one hit) to work faultlessly - although this might be
down to my cranky system! (In any case, it seemed to choke on larger
documents.)
Since the original approach required AppleWorks to inspect every character
in turn, it was bound to be a pretty slow method. So I tried a sort of
'halfway house' instead - which pulls text in to AS, paragraph by paragraph,
and then returns it to AW for replacement as necessary.
While clearly larger and slower than the one-hit method, this is
considerably faster than a char-by-char routine - and seems to work
reasonably well for me:
=============================================================
tell application "ClarisWorks"'s front document
repeat with i from 1 to count paragraphs
if paragraph i return then
set cleanPara to my cleanUp(paragraph i)
if paragraph i cleanPara then set paragraph i to cleanPara
end if
end repeat
end tell
to cleanUp(thePara)
set thePara to cutEndSpaces from thePara
repeat with punctMark in {".", "!", "?", ",", ":", ";"}
set punctMark to punctMark as string
set doubleSpace to punctMark & space & space is in thePara
if doubleSpace or punctMark is in thePara then [NO BREAK]
set thePara to checkSpaces of thePara by [NO BREAK]
punctMark given fix:doubleSpace
end repeat
thePara
end cleanUp
to checkSpaces of thePara by punctMark given fix:doubleSpace
set {tid, text item delimiters, adjust} to [NO BREAK]
{text item delimiters, punctMark, 0}
set {punctCount, text item delimiters} to [NO BREAK]
{(thePara's text items)'s length, punctMark & space}
set {spaceCount, text item delimiters} to [NO BREAK]
{(thePara's text items)'s length, tid}
if thePara ends with punctMark then set adjust to 1
if punctCount = spaceCount + adjust and [NO BREAK]
not doubleSpace then return thePara
addSpaces to thePara by punctMark
end checkSpaces
to addSpaces to thePara by punctMark
set {tid, text item delimiters} to [NO BREAK]
{text item delimiters, punctMark}
set itemList to thePara's text items
repeat with i from 2 to itemList's length
set theItem to itemList's item i
if theItem "" then
if theItem starts with space & space then
set itemList's item i to cutStartSpaces from theItem
else if theItem does not start with space and [NO BREAK]
theItem's character 1 is not in "0123456789" then
set itemList's item i to space & theItem
end if
end if
end repeat
set {thePara, text item delimiters} to {itemList as string, tid}
thePara
end addSpaces
to cutStartSpaces from theItem
repeat while theItem starts with space & space
set theItem to theItem's text 2 thru -1
end repeat
theItem
end cutStartSpaces
to cutEndSpaces from thePara
repeat while thePara ends with space
set thePara to thePara's text 1 thru -2
end repeat
thePara
end cutEndSpaces
=============================================================
Initially, I didn't look at the issue of styled text either - mainly because
it didn't form a part of the original brief. However, like Jay, I've not yet
noticed any glaring aberrations in my script's treatment of AW's styled
text. :-)
HTH.
Oh yeah - while we're on the subject...
Having spent a number of years in marketing, communications and publishing,
I actually *do* give a s**t about styled text and general presentation
(where it's appropriate and possible) - as I'm sure do many others in "this
crowd".
In addition, since I don't really care enough to berate anyone for drawing
unwanted conclusions, nor am I upset in the least by swearing (whether
appropriate or gratuitous), I'm not sure in which particular pigeon-hole I
might now fit. (Prejudgement, anyone?)
Perhaps we could form a revolutionary new crowd: One that believes it unfair
and unnecessary to castigate someone for trying to help another... ;-)
--Kai the hermit
--
email@hidden
email@hidden
_______________________________________________
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.