How to search for text and apply a style to it

Get help using and writing Nisus Writer Pro macros.
Post Reply
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

How to search for text and apply a style to it

Post by vwnisus »

I have saved a selection of emails from Apple Mail to an rtf format file.

In Nisus Writer Pro I wish to apply to different styles to the 'header' of each email which appears in the file.

For example:
From: Kevin Smith <ks@nothing.com>
Subject: Re: Certificate for reactivation of account
Date: 3 March 2022 at 14:07:08 GMT
To: Adam Smith <adamsmith@as.org.uk
The idea is:

1. to search for each "From:" to the end of the line - select the whole line and apply "Heading 1" style
2. to search for each "Subject:" to the end of the line - select the whole line and applying "Heading 2" style

and so on

I know how to construct search to select each to the end of the line in Find and Replace, but have difficulty understanding how to, in a macro, to conduct a find and then apply a style.

Any help would be gratefully received.

macOS 12.2.1 - Nisus Writer Pro, version 3.2.2
User avatar
phspaelti
Posts: 1311
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to search for text and apply a style to it

Post by phspaelti »

For this particular task finding bits and applying styles (that exist in your file) you can do this quite simply.

Code: Select all

if Find All '^From\:.+', 'Ea'
    Heading 1
end
I am using the "if" clause, so that we can skip the formatting command if the Find is unsuccessful. You can just chain a series of such clauses.
philip
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Re: How to search for text and apply a style to it

Post by vwnisus »

Philip,

Thank you for the code.

Running it only deals with the first "From:" not any of the following ones.

Is this intended or does the code need adaptations?
adryan
Posts: 559
Joined: 2014-02-08 12:57:03
Location: Australia

Re: How to search for text and apply a style to it

Post by adryan »

G'day, vwnisus et al

As Philip says, you just copy and paste his code fragment as many times as needed into your Macro document. You then amend each occurrence of it to suit your purpose.

For example, you might replace "From" with "Subject" in the second occurrence, and possibly also replace "Heading 1" there with some other Style suitable for the Subject line, and so on.

I've used just such a Macro for many years, embedding it in an AppleScript script that converts a selected message in Mail into a Nisus Writer document. I don't trust Mail to keep important documents filed correctly, and recovering specific Mail messages from the Library (which you may wish to do) is hampered by Mail's inscrutable filing system. NWP documents, on the other hand, are easily handled in the Finder.

Things get more complicated if you wish to save images and attachments, but I'm assuming it's the text bit that's really of interest here.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Re: How to search for text and apply a style to it

Post by vwnisus »

Adrian,

Thank you for the detailed response.

I understand what I need to do to change the code for different words (eg "From", "Subject", etc).

My purpose is different. In each file I have saved several emails not just one. There will be several "From:"'s and several "Subject:"'s, etc.

So the macro should find each "From:", select the line and apply the Heading 1 style to it, etc.

Further help would be gratefully received.
User avatar
phspaelti
Posts: 1311
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to search for text and apply a style to it

Post by phspaelti »

Thanks Adrian, for elaborating my intentions.
vwnisus wrote: 2022-03-11 00:27:47 My purpose is different. In each file I have saved several emails not just one. There will be several "From:"'s and several "Subject:"'s, etc.
So the macro should find each "From:", select the line and apply the Heading 1 style to it, etc.
It is for that reason that I used Find All in my original example. Does the macro not apply the style to all "From" lines?
My specific formulation limited the finds to "From" at the beginning of the line with a following colon. If this is not appropriate let us know and we can help you adjust it.
philip
adryan
Posts: 559
Joined: 2014-02-08 12:57:03
Location: Australia

Re: How to search for text and apply a style to it

Post by adryan »

G'day, Philip et al

The only reason I can think of that Philip's code might not work for all the occurrences in vwnisus' document is that they fail to meet the stipulations Philip has just mentioned. And the most likely culprit here is the odd gremlin that can secrete itself in text, particularly that originating in email messages. Try ticking the Show Invisibles command in the View menu and see whether some interloper character has been hiding there and sabotaging the Macro. If you find something, use Replace All to clean up your document before performing any further manipulations on it.

Remember that you can use the Macroize feature of the NWP Find/Replace system to generate code fragments such as that Philip used in his suggestion.

As always, it's best to know your document and what it's likely to contain before devising Macros for it. And always test Macros and complicated Find/Replace expressions on a copy of the document.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Re: How to search for text and apply a style to it

Post by vwnisus »

Adrian, Philip,

Thank you for your detailed and helpful replies.

I have found (I hope) the issue: the subsequent email headers had soft returns rather than hard returns, so this is was the reason why the search failed.

However, now the search proceeds it leads to a formatting issue.

I have set up "Heading 1" and "Heading 2" to be in different colours, different sizes and both in bold. Heading 2 is applied to 3 lines in the header of each email (Subject:, Date: and To:).

But the result is only one line of the 3 is bold although the other formatting is correct, see example and how I set up the two Heading styles
result of search.png
result of search.png (58.37 KiB) Viewed 3290 times
Attachments
Heading 1 style.png
Heading 1 style.png (242.16 KiB) Viewed 3290 times
Heading 2 style.png
Heading 2 style.png (206.82 KiB) Viewed 3290 times
adryan
Posts: 559
Joined: 2014-02-08 12:57:03
Location: Australia

Re: How to search for text and apply a style to it

Post by adryan »

G'day, Victor et al

I set up text, Styles and Macro code just as you and Philip have posted, and it all works OK for me.

One thing that can cause things to become unstuck is the operation of a pre-existing defined Character Style that explicitly unbolds the character format. This Character Style will remain respected even though a subsequently applied Paragraph Style calls for unbolded text. Try removing all Character Styles in the four lines (eg, by unticking them in the Character Style list in the Format menu) before applying the Macro.

If a problem remains, you may need to attach your Macro file and a file showing the original email header text you are working with (suitably altered to retain privacy as desired).

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Re: How to search for text and apply a style to it

Post by vwnisus »

Adrian,

Thank you for the review.

I added a step in the macro to remove all Character Styles which fixed the problem.

When saving several emails to RTF form Apple Mail results in a page break between each email. This has the result that first line after a page break not being found by:

Code: Select all

if Find All '^From\:.+', 'Ea'
    Heading 1
end
So I added a further line to the macro to search for page breaks and replace them with a page break and a hard return, which appears to be fix the issue.

Thank you to you and Philip for all the help
Post Reply