Replace character style

Get help using and writing Nisus Writer Pro macros.
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Replace character style

Post by Tacitus »

I'm trying to do a macro to replace one character style with another. This would operate within a selected text block - essentially the footnote area - but could be applicable anywhere the text is selected. Thus:

Find in selection character style ’Super’

In selection replace all character style ’Super’ with character style ’Super 2’

End macro

I tried doing it with the 'match' command in find, but whilst the style would match the actual text doesn't always, so would need to be repeated for each particular case. Easier with a 'replace style' and the selection limiting it to the area in question.

Be grateful for any help.
History is a nightmare from which I am trying to escape.
adryan
Posts: 598
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Replace character style

Post by adryan »

G'day, Tacitus et al

The easiest way to do this is to use the Find/Replace mechanism and Macroize it.

The resultant expression will look like this if you use PowerFind Pro:–

Find and Replace ".+", @Text<\0>, 'EsauU'

The ".+" (without the quotation marks) is the Find expression and needs to be assigned the Style you wish to be replaced, while the "\0" (again without the quotation marks) is the Replace expression and needs to be assigned the Style you wish to use as the replacement. To assign the first Style to the Find expression, select the expression in either the Find/Replace window or in the Macro and then apply the Style; similarly for the second Style.

"\0" (without quotation marks) represents whatever was found by the Find expression.

The Macro will only work for the two Styles you used in its construction, so it should be given a name that indicates this; eg, "Replace Style 1 with Style 2 in Selection".

A more generic Macro that caters for arbitrary Styles would require the Macro Language (and another contributor to the Forum!).

I hope this helps.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
phspaelti
Posts: 1344
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replace character style

Post by phspaelti »

Personally to write a macro that replaces one style with another, I prefer to write it like this:

Code: Select all

$doc = Document.active
if Find All $doc.styleWithName('Super')
    Character Style:Super2
end
This uses the find by style feature, and it side steps the fickle nature of styled find and replace.

PS: Note that I use the "If" statement to make sure the style application happens only if the style is found.
philip
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

Thank you Phillip and Adrian. I was almost there with something like Phillip's suggestion - a lash up of what Adrian suggested - when Phillip replied.

Phillip's suggestion operates on the whole file, but I only need it to operate on the footnotes. It might be useful to have an alternate version which operates purely on a text selection - it should be simple enough for me to substitute character style names in the macro.

Thanks to you both for the help. One of these days I'll get the hang of macros....
History is a nightmare from which I am trying to escape.
User avatar
phspaelti
Posts: 1344
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replace character style

Post by phspaelti »

By the way; one should also point out that these macro solutions are really only necessary if you want this as part of some more complex macro. If you just want to change the style you can just use the menu command Select All in Style. Explicitly:
  1. Place the cursor in a selection with the relevant style
  2. Use Select All in Style from the relevant style menu (Character/ Paragraph)
  3. With the relevant text selected, apply the desired replacement style
philip
User avatar
phspaelti
Posts: 1344
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replace character style

Post by phspaelti »

Tacitus wrote: 2022-08-07 07:40:01 Phillip's suggestion operates on the whole file, but I only need it to operate on the footnotes.

Code: Select all

$doc = Document.active
if Find All $doc.styleWithName('Super'), '', '-an'
    Character Style:Super2
end
If you prefer something that only works in the selection:

Code: Select all

$doc = Document.active
if Find All in Selection $doc.styleWithName('Super')
    Character Style:Super2
end
philip
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

Thanks very much Phillip. Yes the reason for the operating within the footnotes is so I can tag it on to another macro which performs some formatting of a Scriverner imported doc.

I then thought it might be useful to have one that operated within a selection, which could be the whole or part of a document.

Once again many thanks.
History is a nightmare from which I am trying to escape.
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

Sorry to push this but there is one other thing.

Is it possible to have a macro which sets all footers at (say) 0.5 inch? When importing a Scriv doc, for some reason it sets the footer (but not the header) within each section at different heights. It must be something in Scriv but I can't find it, so simplest might be a macro that ignores the section breaks and sets *all* footers at 0.5"
History is a nightmare from which I am trying to escape.
User avatar
phspaelti
Posts: 1344
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replace character style

Post by phspaelti »

Tacitus wrote: 2022-08-07 09:06:00 Sorry to push this but there is one other thing.

Is it possible to have a macro which sets all footers at (say) 0.5 inch?
See answer here
philip
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

Thaks for the reply Phillip. I thought that might be the case. At present the section breaks look like this:

Image

The search command for these in Powerfind Pro, I think is: \f If I do "find/replace" those with (say) xx$xx (anything unique) it removes the sections. Do a global set footer height and then do the reverse replacing the xx$xx with section breaks that seems to work without messing up the formatting too badly.

The only thing I did notice is that despite using \f the replacements are page breaks and not "Section Break (Next Page)" as per the original. At this stage of the project it makes little difference, but I can't see a reason why it should be so.
Attachments
Screenshot 2022-08-08 at 09.18.40.png
Screenshot 2022-08-08 at 09.18.40.png (10.23 KiB) Viewed 83191 times
History is a nightmare from which I am trying to escape.
User avatar
xiamenese
Posts: 553
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Replace character style

Post by xiamenese »

Hi @Tacitus,

I wrote a reply earlier in the thread, but it took me so long to write it that it was redundant by the time I'd posted it.

I don't think I've ever encountered your footer problem with Scrivener --> NWP, but I'll have a shufti. Any information you can give me about your Scriv project and Compile set-ups will make it easier to work out what might be going on at the Scrivener end.

:)

Mark
User avatar
xiamenese
Posts: 553
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Replace character style

Post by xiamenese »

To continue… :)

I've been playing around with a project that has Section breaks, using two different Compile formats to RTF, and in neither of them is there any difference in the footers in the different sections. I tried it with both the default 1 inch margins all round, and also with the footer margin changed to 0.5 inch … in neither case did I get a variation in footer height.

If you want to change the margins in Scrivener, you need to edit the Compile format Page settings, though I would presume you've already tried that.
Screenshot 2022-08-08 at 14.21.jpg
Screenshot 2022-08-08 at 14.21.jpg (116.66 KiB) Viewed 83195 times
As I say, info about your project and compile settings would be a great help. You could PM me here or on the Scrivener forum (same handle on both!)

:)

Mark
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

These are the settings I have for my Scriv defaults. The Nisus page is A4 with 1" margins all round. I normally have a gutter of 0" with the header and footers at 0.5". I think it might be the different header/footer on first pages that's making the difference. It's trivial to do the find/replace etc and a global page setting but I can't see what in Scriv is causing the problem.

Image
Attachments
Screenshot 2022-08-08 at 17.30.png
Screenshot 2022-08-08 at 17.30.png (219.56 KiB) Viewed 83164 times
History is a nightmare from which I am trying to escape.
User avatar
phspaelti
Posts: 1344
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Replace character style

Post by phspaelti »

Tacitus wrote: 2022-08-08 00:30:26 The search command for these in Powerfind Pro, I think is: \f If I do "find/replace" those with (say) xx$xx (anything unique) it removes the sections. Do a global set footer height and then do the reverse replacing the xx$xx with section breaks that seems to work without messing up the formatting too badly.

The only thing I did notice is that despite using \f the replacements are page breaks and not "Section Break (Next Page)" as per the original. At this stage of the project it makes little difference, but I can't see a reason why it should be so.
The problem is basically that "\f" (= "\x0C") is the generic character used for all breaks, i,e., all types of section breaks as well as page breaks, column breaks, and probably glossary entry separators as well. Nisus distinguishes them using the formatting.

While one can probably get the desired results using format sensitive replace, I would opt for a macro language solution like this:

Code: Select all

# Replace All Section Breaks
$doc = Document.active
$breakSels = $doc.text.find "\x0C", 'a'
foreach $sel in $breakSels
  $type = $sel.text.breakTypeAtIndex $sel.location
  if $type == 'section'
    $doc.setSelection $sel
    Section Break:Next Page
  end
end
You should be able to use something like this without the need to replace the breaks with markers first.
Note also that you can also regularize the section formatting without the need for any macro. Just select the whole document, and adjust the formatting in the Sections palette. (NB: You will need to type specific numers in the boxes and then press "enter" for this to have any effect.)
philip
Tacitus
Posts: 126
Joined: 2004-02-12 00:11:45
Location: UK

Re: Replace character style

Post by Tacitus »

Hi Mark:

Thanks for the image of your Scriv settings which made me take another look at mine.

As a result I've modified them to uncheck "use project settings" and then reset the margins to match those in Nisus. Now if I do compile the Scriv project, the footers and headers are consistent throughout the sections in the compiled doc when it is opened in Nisus.

Why this should ever have been a problem will remain a mystery - at least to me. The number of years I've used both Nisus and Scriv and I'm still learning.

Thanks to all for the help.
History is a nightmare from which I am trying to escape.
Post Reply