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
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
So I attach a more elaborate macro here: The basic idea of the macro is to go through the styles 3 times:
- 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
- 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
- On the third pass, eliminate all styles for which the used flag is set to @false
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
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: