Page 1 of 1

Question about Type Text

Posted: 2010-05-21 06:00:33
by goldste4
I'm new to NWP and the its macro language (it's been a long time since I've done any programming either), and I thought I could simply modify the following macro which Martin provided in NWP forum.

Code: Select all

While Select Next Note
   If Find '(.*)\s', 'Es$'
      If '.' != $1
         Select Start
         Type Text ā€˜.ā€™
The macro puts a '.' after an footnote number (I'm converting files from Wordperfect and the footnote formats don't always convert just right). In addition, I wanted a tab after the period, so modified to the Type Text command
to read:

Code: Select all

Type Text '.\t'
Thinking that, since the text is interpolated first, that the '\t' would be converted to a tab character. However, when the macro is run, the '\t' appears in its un-interpolated form (e.g., 1.\tSome footnote text here.)

What am I missing here?

Josh Goldstein

Re: Question about Type Text

Posted: 2010-05-21 06:40:23
by martin
Hi Josh- you just missed a small detail: use double quotes if you want string interpolation to convert escape sequences into special characters. Single quotes don't do any interpolation.

Re: Question about Type Text

Posted: 2010-05-21 08:53:24
by goldste4
Hello Martin,

Thanks very much, that did the trick. I went back and look through the manual and did now catch the line that " " are for interpolation and ' ' are for literals. Although it still seems a bit confusing (at least to me) that the Find expressions appear to work the opposite (from the Macro Help Manual):
It is important to remember, especially for PowerFind Pro expressions, that string literals undergo interpolation (see [Referenced content is missing.]) before being used by the find commands. For example, the following commands are not the same:
Find '\s+', 'E'
Find "\s+", 'E'
The first finds one or more whitespace characters, as you might expect. The second however searches for one or more of the letter ā€˜sā€™ because the backslash is first interpreted using the rules for string literals. If double-quotes are to be used the backslash must be escaped like so:
From the use of " ", I would have guessed that the first Find expression would find the text "\s" and the second Find expression would find the whitespace, but apparently not! Maybe there is something I'm just not grasping.

Thanks again. As a long time Wordperfect user, I'm quite enjoying NWP.


Re: Question about Type Text

Posted: 2010-05-22 11:00:26
by martin
I'm glad to hear you're enjoying NWP :)

Let's see if I can explain string interpolation with respect to Find commands. First, let's make distinct the string itself, and the string's representation in the macro. Back to your tab, the typed representation in the macro is "\t", but the actual string is just a single tab character. The Find command sees the actual string, never the typed representation. So, if the Find command is given the representation "\s+", interpolation makes the actual string "s+", eg: the backslash has been removed before the Find command sees it. Only then does the Find command start looking for its own kind of special sequences like "\s" or ".+"

It might be more clear if one separates out the Find command into two steps:

Code: Select all

$pattern = "\s+" 
Find $pattern, 'E'
If you ever get confused as to what final string the Find command sees, you could use the Prompt command to display $pattern.

Re: Question about Type Text

Posted: 2010-05-25 14:59:25
by goldste4
Hello Martin, thanks for the in depth reply, appreciate it. Josh