Formating help needed.

Get help using and writing Nisus Writer Pro macros.
Post Reply
HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Formating help needed.

Post by HowardNH » 2009-06-16 09:27:22

I am a returning user to Nisus Writer. The last time I used the program was probably 15 years ago. I thought I remembered that there was a way to record key strokes to create a macro. Maybe that was Word or Excel which I also at the same time.

Based on my ability to manipulate the string, I shall be purchasing Nisus Pro certainly within the next couple of days.

I have spent the morning downloading the demo version and reading these support files and the documentation for Pro application and the Macros. I've seen pieces and parts but not enough to get me going on my own with any useful results.

I have tried using PowerFind but my effort has not gotten too far. To do the formatting it seems as though I need Macros. That is just beyond my understanding at this point. Your assistance and patience are gratefully requested.

I have a stream of text created by FileMaker Pro using a syntax I have specified (see below). I can change the syntax and add tags if necessary.

Here is the description of the steps I want to accomplish. I am assuming the find/replace effort moves sequentially down the string.
Please note that I am using different brackets to enclose related information. If necessary these can be specified as unique identifiers.

Change all double quotes to null.

Change all text to Times 10 point.

Go to the start of each sentence AND
select the next anyword and make it bold AND
find the next punctuation character, select the next anyword and make it blue AND
find the next punctuation character, select the next anyword and highlight it in yellow AND
select the next anyword and make it bold AND
find the next punctuation character, select the next anyword and highlight it in yellow AND
find the next punctuation character, select the next anyword and make it blue
<end>
Here is what the string actually looks like. After the semicolon is a hard return The string ends with a period.

"by [ bii ], { por };
por { por }, [ bay ]."

Here is what the final output should look like:

by< bold> [ bii <blue>], { por <highlight yellow> };
por <bold> { por <highlight yellow> }, [ bay <blue> ].

THANK YOU for your insight and assistance.

Howard

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Formating help needed.

Post by Kino » 2009-06-16 10:18:19

HowardNH wrote:I thought I remembered that there was a way to record key strokes to create a macro.
Unfortunately NW Pro does not have that fantastic feature yet.

If I understand you well, the following macro will do the job. If not, please post some other examples of the data.

Code: Select all

 Replace All '"', ''
Select All
Menu ':Format:Font:Times'
Menu ':Format:Size:10'

$numFound = Find All '^.+?;\n.+?\.(?=\n)', 'E'  # find/select "(paragraph start)blah blah ... ;<LF>blah blah ... .(before LF)"
if ! $numFound
	exit 'Nothing found, exit...'
end

$doc = Document.active
$sels = $doc.textSelections

$numFound = Find All in Selection '^\w+', 'E'
if $numFound  # if something has been found and selected
	Menu ':Format:Bold'
end

$doc.setSelections $sels  # Restore selections
$numFound = Find All in Selection '(?<={\x20).+(?=\x20})', 'E'  # \x20 is a space char and you can use real space chars instead
if $numFound
	Menu ':Format:Highlight:Yellow'
end

$doc.setSelections $sels  # Restore selections
$numFound = Find All in Selection '(?<=\[\x20).+(?=\x20\])', 'E'
if $numFound
	Menu ':Format:Text Color:Blue'
end

# Then, move the insertion point to the document start
Select Page 1
Select Start

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Formating help needed.

Post by martin » 2009-06-16 10:46:47

HowardNH wrote:I am a returning user to Nisus Writer. The last time I used the program was probably 15 years ago. I thought I remembered that there was a way to record key strokes to create a macro. Maybe that was Word or Excel which I also at the same time.
Glad to have you back. Unfortunately there is no feature in Nisus Writer Pro to record macros, though Classic Nisus Writer had this functionality.
Change all double quotes to null.
If you mean to delete all the double quotes, this is the macro code:

Code: Select all

Replace All '[“”"]', '', 'E'
The 'E' option turns on PowerFind Pro, which we use here to match any kind of double-quote, curly or straight. But if you meant that you want to replace all double quotes with a real NULL character, use this instead:

Code: Select all

Replace All '[“”"]', "\u0000", 'E'
Instead of replacing the quotes with the empty string we use a special kind of escape that stands for U+0000 (unicode code point 0000), the NULL character.
Change all text to Times 10 point.
To do this, we use commands just as they appear in the Nisus Writer Menus, eg:

Code: Select all

Select All
Times
10
It would probably be more clear if we made the last menu command more explicit:

Code: Select all

Select All
Times
:Format:Size:10

Code: Select all

Go to the start of each sentence AND
  select the next anyword and make it bold AND
  find the next punctuation character, select the next anyword and make it blue AND
  find the next punctuation character, select the next anyword and highlight it in yellow AND
  select the next anyword and make it bold AND
  find the next punctuation character, select the next anyword and highlight it in yellow AND
  find the next punctuation character, select the next anyword and make it blue
Finding the start/end of a sentence can be tricky, as there's no definitive character that always means you've reached the end of a sentence. For now to keep it simple I'll just assume your sentences always end with a period followed by some whitespace. We could come up with a more complex rule if needed.

Also you say "next punctuation character", but appear to mean just open brackets/braces. I'll assume you just want to find these braces.

So, that in mind, your code to do the formatting would look something like this:

Code: Select all

# define some expressions we'll be using several times
$anyWord = '\w+'			# "\w" stands for "any word character" and "+" means "one or more"
$endOfSentence = '\.\s'	# period followed by "\s" which stands for "any whitespace"
$punct = '[\\[{]'			# a bit tricky, but means either an open bracket or open brace character
$options = 'E-W'			# "E" for PowerFind Pro and "-W" to turn off "wrap around"

# select the next anyword and make it bold
While Find Next $anyWord, $options
	Bold

	# find the next punctuation character, select the next anyword and make it blue
	Find $punct, $options
	Find $anyWord, $options
	:Format:Text Color:Blue
	
	# find the next punctuation character, select the next anyword and highlight it in yellow
	Find $punct, $options
	Find $anyWord, $options
	:Format:Highlight:Yellow
	
	# select the next anyword and make it bold
	Find $anyWord, $options
	Bold
	
	# find the next punctuation character, select the next anyword and highlight it in yellow
	Find $punct, $options
	Find $anyWord, $options
	:Format:Highlight:Yellow
	
	# find the next punctuation character, select the next anyword and make it blue
	Find $punct, $options
	Find $anyWord, $options
	:Format:Text Color:Blue

	# move to the end of the sentence
	Find $endOfSentence, $options
End

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-17 12:25:51

Martin and Kino,

THANK YOUR SO MUCH for your prompt response.

I will be working with this tonight.

I really appreciate your effort and that you presented it in a tutorial manner. At least, can learn more about this incredible feature set.

Thanks again!

I will reply again with my results.

- Howard :D

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-17 15:19:56

Hello All.

Thanks again for your help.

After testing the macro I realized that I often have multiple words before and within brackets. With the macro as provided, the first word gets formated but subsequent words do not. This causes other attributes to be formatted incorrectly as the formatting becomes out of sync.

I tried defining open brace/close brace with a find and yellow highlight in between. I did not get my desired on/off effect like say an an html tag.

I have come to realize that a more generalized approach might provide a better solution. Once provided I think I can adapt it to the several circumstances I have.

Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.

For example: [ word1 word2 word3 ]

The number of the words varies from 1 to 4. I tried using a repeat range but was unsuccessful. Everything within the bracket would be yellow. The brackets themselves would not be.

Thank you again for your assistance.

- Howard

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-18 01:55:20

Hi,

Forgive me, but I seem to be stuck on what should be a simple replace step.

Based on the examples above, I am trying to change "xxx" to a hard carriage return.

I have tried these several approaches however the xxx's do not get recognized.

Replace All '[xxx]', "\u00b6", 'E'

Replace All '[xxx]', "\n", 'E'

Find and Replace 'xxx', '\n', 'E'

The replace works manually in Find, but not in the macro.

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Formating help needed.

Post by Kino » 2009-06-18 02:39:17

Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.
As [ and ] are special characters, they are not interpreted literally unless you put \ before them. You can do something like this.

Code: Select all

Find All '(?<=\[ )[^\]]+(?= \])', 'E'
	# (?<=abc) look behind (preceded by "abc")
	# (?=abc) look ahead (followed by "abc")
	# [abc] character set (a or b or c)
	# ^ stands for NOT as the first character in a character set
	# so [^\]] means any non-LF character which is not "]"
	# + one or more times
Find All in Selection '\S+', 'E'
	# \S matches all visible characters
Menu ':Format:Highlight:Yellow'
The first command finds characters between "[ " and " ]". The second command finds all visible characters (i.e. not space or tab) in selections made by the first command. And the third command applies yellow highlight on those characters. You can omit the second command if you don't mind space characters between words being highlighted. In stead of the first command, you can use also:

Code: Select all

Find All '(?<=\[ ).+?(?= \])', 'E'
	# . any non-LF character
	# +? shortest one or more times
I am trying to change "xxx" to a hard carriage return.
If "xxx" is really "xxx", your

Code: Select all

Find and Replace 'xxx', '\n', 'E'
should work. If you want to replace all occurrences of "xxx" with LF, use

Code: Select all

Find and Replace 'xxx', '\n', 'Ea'
or

Code: Select all

Replace All 'xxx', '\n', 'E'

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-18 06:24:19

THANKS AGAIN! Kino.

I did try using

Replace All 'xxx', '\n', 'E'

but it did not work. Not sure why.

I will copy your example and work with it this afternoon.

I shall report later.

THANKS AGAIN!

Howard

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Formating help needed.

Post by martin » 2009-06-18 09:37:25

HowardNH wrote:After testing the macro I realized that I often have multiple words before and within brackets. With the macro as provided, the first word gets formated but subsequent words do not. This causes other attributes to be formatted incorrectly as the formatting becomes out of sync.
...
Can you please show me how to identify a starting character, highlight everything it encounters with yellow highlight until the specified ending character.
This is no problem. So to highlight a single word, we did this:

Code: Select all

$options = 'E-W'

# find the next punctuation character, select the next anyword and highlight it in yellow
Find '[\\[{]', $options
Find '\w+', $options
:Format:Highlight:Yellow
Now instead of searching for one or more word characters "\w", let's search for all characters that are NOT the close bracket/brace. The code is then:

Code: Select all

$options = 'E-W'

# find the next punctuation character, select the next anyword and highlight it in yellow
Find '[\\[{]', $options
Select End # don't include the open bracket/brace we just found
Find '[^\]}]+', $options
:Format:Highlight:Yellow
The second expression is a little tricky. Kino has explained some of this already, but here's what it means: the "[xyz]" construct means match any single character listed inside the brackets, while "[^xyz]" means match any character NOT listed in the brackets. We want to match anything but the closing bracket or closing brace. Because the closing bracket is a special character here (it terminates the list) we have to escape it using a backslash. I hope that makes sense.

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-18 18:30:38

Thanks Guys!

I have been able to get the formatting to accomplish what I want. I used the information you provided along with some subtle changes to my data source to make the formatting a bit easier.

It looks like I will be able to use Nisus Writer for my book project.

For some reason, during my tests, the application would hang. Mostly when I would try to select all and then hit the delete key.

I think I might have corrupted the file by repeatedly cutting and pasting data into the file and then deleting it and replacing it.

I have a file I can send if you are interested.

THANKS AGAIN,

Howard

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Formating help needed.

Post by martin » 2009-06-18 22:09:24

I'm glad you've got everything working Howard. If you could send me the file that would be great. Please use the menu Help > Send Feedback, and be sure to indicate which part should be deleted to trigger the hang (if you do not in fact "select all" beforehand). Thanks!

HowardNH
Posts: 7
Joined: 2009-06-16 08:55:55

Re: Formating help needed.

Post by HowardNH » 2009-06-19 16:15:04

Done! A file has been sent.

Post Reply