Re: Replace with different text
Re: Replace with different text
- Subject: Re: Replace with different text
- From: Paul Skinner <email@hidden>
- Date: Thu, 16 May 2002 21:58:08 -0400
I prefer to do things once and then make a bed of laurels to rest
on. Here's a handler called ReplaceAndEnumerate that replaces every
occurance of a given string, in a text block, with another string and
enumerates that replacement.* If I make it general enough than maybe
I'll be able to use it again.
The ReplaceAndEnumerate handler needs to break up the inputText at
the occurances of the originalText in the inputText, but that's
something I can probably use again too, so I made it into another
handler; ListFromText*. That's two things I might never have to write
again!
Not really, but you get the idea. If I refined these handlers,
added uber-error trapping and documentation I might never have to
rewrite these functions. I think I wrote the same code over and over in
different ways a hundred times before I realized it, and a hundred more
times before I decided there had to be a better way.
*texts that form lists of less than 4000 text items yadda yadda or
loop yadda concateyadda...
--begin script
set storyText to "Once upon a time there lived in a certain village a
little girl, LIBRARYNAME the prettiest creature that ever was seen;
LIBRARYNAME she was loved and admired by all, not one in LIBRARYNAME the
village could compare with her. Her mother was fond of her to a great
degree, LIBRARYNAME and her grandmother much more so; and well they
might, she was so good-natured, LIBRARYNAME so sweet of temper, and so
ready to oblige; LIBRARYNAME and, besides, never missed her prayers
twice or thrice a day for her mother, and all her relations, and every
body else she could think of. LIBRARYNAME It being cold weather, her
grandmother had a little red riding-hood made for her to keep her warm
and she looked so very pretty LIBRARYNAME and pleasant when she had it
on, that by every body she soon gained LIBRARYNAME the name of Little
Red-Riding-Hood."
ReplaceAndEnumerate(storyText, "LIBRARYNAME", "FG")
on ReplaceAndEnumerate(inputText, originalText, replacementText)
set inputList to ListFromText(inputText, originalText)
set output to {item 1 of inputList}
repeat with iteration from 2 to the length of inputList
set the end of output to (replacementText & iteration - 1 &
item iteration of inputList) as text
end repeat
return output as text
end ReplaceAndEnumerate
on ListFromText(inputText, TID)
set AppleScript's text item delimiters to TID
set inputList to every text item of the inputText
set AppleScript's text item delimiters to ""
return inputList
end ListFromText
--end script
-->"Once upon a time there lived in a certain village a little girl, FG1
the prettiest creature that ever was seen; FG2 she was loved and admired
by all, not one in FG3 the village could compare with her. Her mother
was fond of her to a great degree, FG4 and her grandmother much more so;
and well they might, she was so good-natured, FG5 so sweet of temper,
and so ready to oblige; FG6 and, besides, never missed her prayers twice
or thrice a day for her mother, and all her relations, and every body
else she could think of. FG7 It being cold weather, her grandmother had
a little red riding-hood made for her to keep her warm and she looked so
very pretty FG8 and pleasant when she had it on, that by every body she
soon gained FG9 the name of Little Red-Riding-Hood."
On Thursday, May 16, 2002, at 06:04 PM, Elmer Banate wrote:
Hi to all,
I need to replace the word "LIBRARYNAME" in my quark
document to the name of items in the library I
created.
The script must replace not one replace string.
example "LIBRARYNAME" will be replaced to "FG01" after
replacing the first word ("LIBRARYNAME") in the next
replace is "FG02" and soon, depending on how many
occurrences, if "LIBRARYNAME" occurred 22 time the
last replacement text is "FG22".
I'm trying to use counter but my counter doesn't work.
I suspect the my problem is around here:
set (every text of stories where it is search_string)
to replace_string
--------
tell application "QuarkXPress 4.11"
set FigName to "FG0"
tell document 1
try
set LibName to every text of every story
where it is "LIBRARYNAME" as list
yuck! Is that how you HAVE to talk to QXP?
end try
set ctr to 1
repeat with i from 1 to the number of items in
LibName
set search_string to item i of LibName
set replace_string to FigName & ctr as
string
try
set (every text of stories where it is
search_string) to replace_string
set ctr to ctr + 1
end try
end repeat
end tell
end tell
TIA
Regards
elmer
--
Paul Skinner
_______________________________________________
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.