MACSCRPT Archives

October 2006

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Eric Schult <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 20 Oct 2006 16:20:40 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (47 lines)
I've been using ACME Script Widgets' "ACME Replace" command for so long I
barely remember how to search and replace strings within strings in vanilla
AS. However, I find myself needing to do that, because my ACME Replace
commands are hanging in some of my newer scripts in OS 10.3.8. (They never
error out; the script just never finishes running, and then script editor
crashes.)

I've just written a vanilla AS variation that works, but I'm worried I'll
bump into errors when running this script on a very long string. Maybe
that's not an issue anymore, but I recall in earlier versions of the Mac OS
and AS having to bite off the task in blocks. Is that still true, and what
is the limitation?

If anybody has a variation on the following  script that'll do the same
thing and handle large blocks of text in a more efficient manner, I'd really
appreciate seeing an example of it.

Thanx!

WES


set myTextBlock to "This string contains more than one instance of the word
'foo', and 'foo' is the string I want to substitute with some other word
other than 'foo'."
set subString to "not_foo"

set AppleScript's text item delimiters to "foo"
set stringList to text items in myTextBlock
set AppleScript's text item delimiters to ""

set newString to ""
set stringCount to count of items in stringList
if stringCount is greater than 1 then
 repeat with i from 1 to count of items in stringList
  set thisString to item i in stringList
   if i is not stringCount then
    set newString to newString &  ¬
     (thisString & subString) as string
   else
    set newString to newString & thisString
   end if
 end repeat
end if

return newString

ATOM RSS1 RSS2