RE: Quark Find and Replace with variables (new question)
RE: Quark Find and Replace with variables (new question)
- Subject: RE: Quark Find and Replace with variables (new question)
- From: Hans Haesler <email@hidden>
- Date: Mon, 29 Jan 2001 22:44:27 +0100
On Mon, 29 Jan 2001, Aaron Miller wrote:
>
(...) Two questions, if you don't mind, about approach 1:
>
>
1. What formatting is changed in case 1?
The whole story 1 will be formatted like the _first character_
of the original story 1. Try it: set a word to bold or apply
a color. After running the script the bold or the color will be
gone. Or set the color of the first character to "Magenta".
After running the script all text will be colored "Magenta".
Please tell me if this behavior would be a problem. Then I'll
send you a vanilla solution which conserves local formatting.
>
2. Could you explain to me what is going on in the handler,
>
particularly how the delimiters are working?
Let's say your text looks like this: "xxxaxxxxxaxxxx".
The default of the text item delimiters is {""}, an emtpy string.
If you ask for every text item of your string, then you'll get this:
{"x", "x", "x", "a", "x", "x", "x", "x", "x", "a", "x", "x", "x", "x"}
By setting the text item delimiters to {"a"} the result is:
{"xxx", "xxxxx", "xxxx"}, i.e. in this case, a list of three separate
strings. Every character of the original string is there, except the
"a", which is the delimiter.
Now, you set the text item delimiters to {"b"}. And this time you set
a new variable to the list, but as string. The new delimiter will be
inserted between the items of the list:
"xxxbxxxxxbxxxx"
As a repetition, the code of the explanation with the result after
each command line:
---
set oD to AppleScript's text item delimiters
-->{""}
set tStr to "xxxaxxxxxaxxxx"
-->"xxxaxxxxxaxxxx"
set AppleScript's text item delimiters to {"a"}
-->{"a"}
set aList to (every text item of tStr)
-->{"xxx", "xxxxx", "xxxx"}
set AppleScript's text item delimiters to {"b"}
-->{"b"}
set tStr to aList as string
-->"xxxbxxxxxbxxxx"
set AppleScript's text item delimiters to oD
-->{""}
---
As shown above, it is good practice to save and to restore the text
item delimiters. Even in your droplet, which is a world of it's own.
Therefore, here is a new version of the snippet, as I should have
presented in my first reply:
---
property list1 : {"one", "One"}
property list2 : {"uno", "Uno"}
on open y
set oD to AppleScript's text item delimiters
tell application "QuarkXPress 4.11"
activate
open y
tell document 1
set newStory to story 1 of text box 1
repeat with i from 1 to count of list1
set list1item to item i of list1
set list2item to item i of list2
set newStory to my searchReplace(list1item, list2item, newStory)
end repeat
set story 1 of text box 1 to newStory
end tell
end tell
set AppleScript's text item delimiters to oD
end open
on searchReplace(sStr, rStr, tStr)
set AppleScript's text item delimiters to sStr
set aList to (every text item of tStr)
set AppleScript's text item delimiters to rStr
set tStr to aList as string
end searchReplace
---
The repeat loop makes two iterations, since there are two items in list 1.
Given this string: "this one is one a one test", the results in the handler
look like this:
---
on searchReplace(sStr, rStr, tStr)
set AppleScript's text item delimiters to sStr
-->{"one"}
set aList to (every text item of tStr)
-->{"this ", " is ", " a ", " test"}
set AppleScript's text item delimiters to rStr
-->{"uno"}
set tStr to aList as string
-->"this uno is uno a uno test"
end searchReplace
If you want to avoid that portions of a word are changed ("alone" -->"aluno")
then you can put spaces before and after the words: " one ". This works,
unless the words are followed by punctuation characters or the like. Then
you'll be better off with saving the text as XPress Tags and do the S&R using
RegEx Commands which give you better options.
But, please, be aware that you can't be sure to match all occurrences. If
someone has, e.g. fiddled with kerning values, you may find a <k$> inside of
the word and it will not be matched by the search pattern.
So much for today,
Hans
---
Hans Haesler | email@hidden