Replace character style
Replace character style
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.
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.
Re: Replace character style
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
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
macOS Ventura
Nisus Writer user since 1996
Re: Replace character style
Personally to write a macro that replaces one style with another, I prefer to write it like this:
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.
Code: Select all
$doc = Document.active
if Find All $doc.styleWithName('Super')
Character Style:Super2
end
PS: Note that I use the "If" statement to make sure the style application happens only if the style is found.
philip
Re: Replace character style
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....
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.
Re: Replace character style
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:
- Place the cursor in a selection with the relevant style
- Use Select All in Style from the relevant style menu (Character/ Paragraph)
- With the relevant text selected, apply the desired replacement style
philip
Re: Replace character style
Code: Select all
$doc = Document.active
if Find All $doc.styleWithName('Super'), '', '-an'
Character Style:Super2
end
Code: Select all
$doc = Document.active
if Find All in Selection $doc.styleWithName('Super')
Character Style:Super2
end
philip
Re: Replace character style
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.
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.
Re: Replace character style
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"
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.
Re: Replace character style
Thaks for the reply Phillip. I thought that might be the case. At present the section breaks look like this:
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 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 (10.23 KiB) Viewed 83191 times
History is a nightmare from which I am trying to escape.
Re: Replace character style
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
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
Re: Replace character style
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.
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
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.
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
Re: Replace character style
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.
- Attachments
-
- 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.
Re: Replace character style
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.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.
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
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
Re: Replace character style
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.
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.