Change identical style in several files

Get help using and writing Nisus Writer Pro macros.
js
Posts: 259
Joined: 2007-04-12 14:59:36

Change identical style in several files

Post by js »

I have a number of files using certain named styles in common. Opening one file and changing the attributes of a style is easy. But how about changing that same style in all other files?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

The thing to do is open each of the files in turn, process, and then close (if so desired).

The basic outline of your macro will be something like this:

Code: Select all

# Identify Folder to process
$folderPath = Choose Folder
#--------------------------
# Loop through all files
#--------------------------
foreach $fileName in $fileNames
  # Locate File
  $filePath = @S"$folderPath/$fileName"
  if File.existsAtPath $filePath
    $doc = Document.open $filePath

    # Do stuff here

    $doc.close
  end # if
end # foreach
Note 1: You will need to have some way of identifying the files you want to process. In my example I just assumed they were in an array called $fileNames.
Note 2: This will potentially run into sandboxing issues. In which case you will need to add an ".requireAccess" as necessary.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

Note 3: In the above code, it will probably be a good idea to use a more specific name for the $doc variable, unless these are the only documents your macro works with.
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change identical style in several files

Post by adryan »

G'day, Philip et al

With respect to js's statement of his problem, I would have thought that altering a Style in one file and then saving it to a particular set of Styles in the Style Library would make the altered Style automatically available to any other file that used it; ie, that used the Style of the same name that is stored in the same Style set in the Style Library.

But a problem arises if different files use similarly named (but possibly different) Styles that reside in different sets in the Style Library. Presumably the business end of Philip's Macro would need to replace the relevant Styles with the favored one.

What I can't see is how one can tell which set in the Style Library contains a given Style in a given file. For example, I might have a Paragraph Style called "Salutation" in each of two different files. Without scrutinizing the details of each in the relevant Style Sheets, is there any way of determining whether or not they are in fact the same Style?

It must be remembered, too, that the same Style can be saved in multiple sets in the Style Library, but that not all Styles with the same name are necessarily the same Style. One of my "Salutation" Styles might reside in a "Letter" set and another in an "Email" set, for example, with each being defined differently.

It would make things look a bit messier, but it might make the situation clearer, if each Style in the list in the Styles Palette were accompanied by a reference to its containing set. Thus we might have "Salutation < Letter" and "Salutation < Email". The reference could be incorporated as part of the Style importation process. In the case of Styles defined in the present file but not yet saved to the Style Library, they would simply be listed without a reference until they were saved to the Style Library, at which time the reference would be appended to the name in the Styles Palette.

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

Re: Change identical style in several files

Post by phspaelti »

Hello Adrian, and js,

I was taking js at his word that it was easy to do what he wanted, so I only addressed the processing of multiple files. It really depends on how he is going about things.

But in fact changing styles is a bit cumbersome in a macro. In the GUI you can just import styles from the library. But this requires you to go through an interface which is not available in a macro, and for which there are no commands. So if you want the macro to change styles by importing them from a style library, you will have to open the relevant library file and copy the styles and then paste them into the target document. I'm not sure if that is what js is aiming for.
adryan wrote: 2022-07-13 00:31:31 With respect to js's statement of his problem, I would have thought that altering a Style in one file and then saving it to a particular set of Styles in the Style Library would make the altered Style automatically available to any other file that used it; ie, that used the Style of the same name that is stored in the same Style set in the Style Library.
All style libraries are "available" to all files. But once styles are imported into a document, changing the style in the library does not affect the documents that are "using" that style. The library is just a repository. If the style you import has the same name as another, you get a conflict resolution dialog. In a macro you can set options to resolve the conflict to supress the dialog, which will be handy if you are trying to process a large number of files. That's about it.
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Change identical style in several files

Post by adryan »

G'day, Philip et al

So each file accesses its included Styles from its own Style Sheet, without ever referring to the Style Library. And the Style Library is not a dynamic structure, all interaction with it being user-mediated, either by saving to it from a Style Sheet or by importing from it into a given file.

Thanks for clarifying that, Philip.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Change identical style in several files

Post by martin »

phspaelti wrote: 2022-07-12 23:21:32 Note 1: You will need to have some way of identifying the files you want to process. In my example I just assumed they were in an array called $fileNames.
Note 2: This will potentially run into sandboxing issues. In which case you will need to add an ".requireAccess" as necessary.
I recommend using the "Choose Files" command like so:

Code: Select all

$filePaths = Choose Files @undefined, "Process Files"
ForEach $filePath in $filePaths
	$doc = Document.open $filePath
	# do something with the open document
	$doc.close
End
This will let the user choose exactly which files to process and also deals with sandboxing to establish proper access.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Change identical style in several files

Post by martin »

adryan wrote: 2022-07-13 03:15:48 So each file accesses its included Styles from its own Style Sheet, without ever referring to the Style Library. And the Style Library is not a dynamic structure, all interaction with it being user-mediated, either by saving to it from a Style Sheet or by importing from it into a given file.
That's exactly right. Each document gets a separate copy of any styles you import from the Style Library. Conceptually you should think about Style Library import/export actions like copy-paste. There are no automatic updates nor references back to the origin.
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Change identical style in several files

Post by js »

Great thanks to everybody helping to solve the problem.

With “that’s easy” I meant to say: easy to do manually.
As to the “# do stuff here” part using the loop macros suggested by Philip or Martin, I think it could be done like this:

Open one of the files to be treated by the macro
Manually attribute-copy a little chunk of text of each style to be reformatted
Paste this text into the macro and outquote it. Now the macro file has these styles.
At “# do stuff here”, for each style enter:
Find “.+”, 'Eu’ , applying the style to “.+”
Do things like "Set Paragraph Spacing Before xx", whatever you want to apply to the reformatted style, followed by:
Format:Paragraph Style:Redefine Style from Selection

Basically this seems to work. I found two difficulties though:
— I still don’t understand something about indents. Manually we have three choices: First line, Hanging, and Right. But in the macro language I find “Head indent”. What should I do to simple change the indent of the styled text, no first, no hanging, no right.
— I am not sure about attributed text (like colored text) inside a para style. I want to keep it. Is it sufficient to not define a color in the modified style sheet?
— And a final question: If I apply a style in the macros “Find “.+”, 'Eu’” expression, will it search that style, or will it only search text that has the same attributes as the style has? I guess in most cases the would not make a difference, but theoretically it could.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

Adding all styles from a Style Library file that is currently open to the current file is actually very simple. It needs just one line of code:

Code: Select all

$doc.addStyles $styleDoc.allStyles, "replace"
The quick way to get such a Style Library file is to hard code the path into your macro. If you want to have your macro give you a choice at run time the macro will look like this:

Code: Select all

$doc = Document.active

# Get a style file from the Style Library
$dm = DocumentManager.instance
$styleLib = $dm.groupWithName 'Style Library'
$styleFiles = Hash.new
foreach $filePath in $styleLib.allFilePaths
    $styleFiles{$filePath.lastFilePathComponent} = $filePath
end
$styleFile = prompt options 'Choose a style file', '', 'OK', $styleFiles.keys
$styleFilePath = $styleFiles{$styleFile}
File.requireAccessAtPath $styleFilePath
$styleDoc = Document.open $styleFilePath, @false

# Add the styles from the Style Library to the current document
$doc.addStyles $styleDoc.allStyles, "replace"

# Close the Style Library
$styleDoc.close
Yeah. Working with the DM in a macro is a bit baroque :P
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

Before I address these questions, I just want to clear up one thing; the difference between styles and attributes.
  • Attributes are the things that actually format the document and determine the appearance (color, font, size, paragraph spacing, …)
  • Styles are named collections of attributes, However a style doesn't need to contain any attributes at all !
js wrote: 2022-07-13 10:21:03 — I am not sure about attributed text (like colored text) inside a para style. I want to keep it. Is it sufficient to not define a color in the modified style sheet?
As mentioned a style doesn't need to have any attributes at all. This includes color. But the text in your document will need a color.
If your style includes a color, you can still override it manually, by applying the color by hand. If you apply a style with a specified color attribute to text with manually applied color, the manual color will be removed (fingers crossed). This applies for (some?/all?) other attributes as well. So if I start with a document that has many applied attributes, but no styles, I first find the bits with specific attributes and create named character styles for them (maybe without any attributes). Then I create paragraph styles (without attributes as well) and apply them. Then finally I specify the attributes I want for each of these styles and apply them, clearing out the hand applied attributes. This transfers the formatting information from the attributes to the styles.
js wrote: 2022-07-13 10:21:03 — I still don’t understand something about indents. Manually we have three choices: First line, Hanging, and Right. But in the macro language I find “Head indent”. What should I do to simple change the indent of the styled text, no first, no hanging, no right.
I guess I don't understand "indent, no first, no hanging, no right". What do you want indented?
You are correct that NWP uses two different phrases: left/hanging<->right in the menus, head<->tail in the macro language. But the intended meaning is the same (pace right-to-left language issues). Anyhow the meaning of these is:
  • first: indent the first line of the paragraph
  • left/hanging/head: indent the second, third, etc. lines of the paragraph
  • right/tail: indent (i.e., narrow the margin of) all the lines on the right side
A "hanging" indent is the usual name for a paragraph where the first line is not indented, but all following lines are. The first line is "outdented" relative to the paragraph. This is typically used in things like bibliographies and lists.
Anhow what people usually mean by "indent" is first line indent.
js wrote: 2022-07-13 10:21:03 — And a final question: If I apply a style in the macros “Find “.+”, 'Eu’” expression, will it search that style, or will it only search text that has the same attributes as the style has? I guess in most cases the would not make a difference, but theoretically it could.
I think you will have to test this yourself. My quick test shows that it finds both text with the attribute applied manually, and text with the attribute applied via style. I personally avoid this construct in macros when possible.
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Change identical style in several files

Post by js »

Well thank you Philip. What I sure learned from all this is that to solve my problem I should use the style library. Now what I did is this:

-- I created a new file, entered some text and gave it some paragraph styles.
-- This I saved to the style library in a new document I called test
-- I opened that document
-- I created a new file, gave it a name and applied your macro as indicated above

$doc = Document.active
$styleDoc = "/Applications/nisus writer settings/Style Library/test.rtf"
$doc.addStyles $styleDoc.allStyles, "replace"

This produced an error message: The Text object does not have a "allStyles" property.
Can you comment, please.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

Hi js,
Yes. The line:

Code: Select all

$styleDoc = "/Applications/nisus writer settings/Style Library/test.rtf"
doesn't open your style file. Rather it creates a text object (which contains the path to your style file).

So change your code as follows:

Code: Select all

$doc = Document.active
$styleFilePath = "/Applications/nisus writer settings/Style Library/test.rtf"
$styleDoc = Document.open $styleFilePath, @false
$doc.addStyles $styleDoc.allStyles, "replace"
$styleDoc.close
And as noted before this code might run into sandboxing issues. So you should add a ".requireAccess" line before you open the $styleDoc.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Change identical style in several files

Post by phspaelti »

Oh, and the alternative, if you are going to open the style document by hand is to use this:

Code: Select all

$styleDoc = Document.withDisplayName "test.rtf"
So in that case your macro would be simply:

Code: Select all

$doc = Document.active
$styleDoc = Document.withDisplayName "test.rtf"
$doc.addStyles $styleDoc.allStyles, "replace"
philip
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Change identical style in several files

Post by xiamenese »

I have been using a macro to apply a style collection to an existing document for many, many years now. The code was almost entirely written for me by Martin. I recently created a simplified version of it for another NWP user. It starts by asking you to select the style collection you wish to import. So here is that version of the macro (and an accompanying styles collection), which you might like to look at to see about modifying it for your needs.

HTH 😄

Mark
set-styles.zip
(7.47 KiB) Downloaded 222 times
Post Reply