Adding and removing styles from a document

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
phspaelti
Posts: 1345
Joined: 2007-02-07 00:58:12
Location: Japan

Adding and removing styles from a document

Post by phspaelti »

Over in another thread Þorvarður asks whether it's possible to remove or add (from some file) styles via macro. The answer is of course yes. But since this is a technical discussion, I'm moving the answer over here.

Removing all styles from a document can be done like this.

Code: Select all

$doc = Document.active
foreach $style in $doc.allStyles
   $doc.removeStyle $style
end
But of course this is a bit like taking a sledgehammer to your document. If any styles are actually used, that formatting will vanish.

A more clever way is to remove only the styles not actually used:

Code: Select all

$doc = Document.active
foreach $style in $doc.allStyles
   If ! Find($style)
      $doc.removeStyle $style
   end
end
This still has one problem, if some of your used styles are based on others that are themselves not used, you might still lose formatting (and you'll change the styles).

So I attach a more elaborate macro here:
Remove Unused Styles.nwm
(7.41 KiB) Downloaded 1271 times
The basic idea of the macro is to go through the styles 3 times:
  1. On the first pass, find all the used styles (as above), but don't remove them. Instead create a "flag" array (actually a hash) that marks whether a style is used or not
  2. On the second pass, check all styles to see whether they are (a) used, and (b) based on another. If yes, then follow the 'based-on' path to the end and mark all these styles as used
  3. On the third pass, eliminate all styles for which the used flag is set to @false
Now to adding styles from another file, for example, one of the files in the Style Library.

Code: Select all

$doc = Document.active
# Adjust path to point to the desired file
$styleFile = '~/Nisus Documents/Style Library/MyStyles.rtf'
File.requireAccessAtPath $styleFile
$styleDoc = Document.open $styleFile, @false
$doc.addStyles $styleDoc.allStyles, 'replace'
$styleDoc.close
For this your best bet is to hard code the file path into the macro, as I have done here. Alternatively one could use a "Choose" command to let the user select the relevant file. [Thanks to Þorvarður for the correction.]

The macro opens the style file (hidden), copies the styles and replaces the ones in the current document. Finally it closes the style file again. The addStyles command allows you to set the style conflict resolution as you would by hand. Here I have chosen the replace option, but rename or ignore are possible as well.

Finally as a variation, I add a macro that lets you choose one of your Style Library files for style import:
Add All Styles from StyleLib File.nwm
(6.21 KiB) Downloaded 1260 times
Last edited by phspaelti on 2018-02-04 06:28:14, edited 2 times in total.
philip
Þorvarður
Posts: 441
Joined: 2012-12-19 05:02:52

Re: Adding and removing styles from a document

Post by Þorvarður »

This is great. Thank you so much, Philip
I merged the two macros, and I'll be using them a lot. I still have many Nisus Classic documents with old styles that I want to get rid of when I convert them.

Needless to say, these macros would solve the original poster's problem, but I don't think he's listening. :roll:
Þorvarður
Posts: 441
Joined: 2012-12-19 05:02:52

Re: Adding and removing styles from a document

Post by Þorvarður »

Hi Philip,
I think the macro “Remove Unused Styles” also removes footnote styles from documents that have footnotes, so that all superscripted Footnote References suddenly become non-superscripted. Can this be fixed?

Here are the details:
After I run the macro, the Character Style “Footnote Reference” has been removed
1.png
1.png (30.55 KiB) Viewed 9612 times

When I reimport the Note Style “Footnote” (see screenshot 2 below), a Character Style called “Footnote Reference” is imported and everything is fine again.
2.png
2.png (36.24 KiB) Viewed 9612 times

So, the Note Style “Footnote” seems to be the culprit. Importing it again from my Style Library solves the problem.
User avatar
phspaelti
Posts: 1345
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Adding and removing styles from a document

Post by phspaelti »

Hello Þorvarður,

Thank you for your comment. I guess you can see from this problem how rarely I use notes of any kind :wink:

I tried checking this and I see that you are right. The problem is that Notes are the strangest beasts in Nisus. Footnote reference characters are simply not "real" things. Finding them does not seem to work, which is why my macro removes the styles, even though they are being used. I tried exempting "Note" styles from the macro, but this has zero effect on the two character styles used for the note references (which are not Note styles!)

To add to the confusion of how these styles work, you seem to have a character style called "Footnote Reference". But my styles are called "Note Reference" and "Note Reference in Note". I wasn't aware that these "technical" styles could vary in name, but this means that these styles can't be identified by name either.

So I have added a bit of code to check for notes and exempt the note reference styles. Hope that does the trick.
Attachments
Remove Unused Styles.nwm
(8.5 KiB) Downloaded 292 times
philip
Þorvarður
Posts: 441
Joined: 2012-12-19 05:02:52

Re: Adding and removing styles from a document

Post by Þorvarður »

Hello Philip,
phspaelti wrote: 2024-09-05 19:04:41 Notes are the strangest beasts in Nisus
Yep, sometimes a bit confusing and not very intuitive …
So I have added a bit of code to check for notes and exempt the note reference styles. Hope that does the trick.
Yes, this seems to work fine now. Thanks a lot.
Post Reply