Macro issues …

Get help using and writing Nisus Writer Pro macros.
Post Reply
xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Macro issues …

Post by xiamenese »

I know I have the reputation of being negative about macros :lol: , but …
I have been looking into my macros, as the one I have always used is highly complex and is beginning to prove slightly ineffective — it was created for Scrivener v. 2, which didn't have styles. What I want macros to do is work on an RTF compiled from Scrivener:
  1. import a style sheet to replace the existing styles in the document — although NWP picks up the styles from Scrivener, they are not cascading, which makes it more difficult to make global changes;
  2. language mark all text in Chinese as "Chinese" rather than "English"
  3. regularise Footnote and Endnote styles to conform with the styles in the NWP style sheet;
To this end:
For (1) I have tried the snippet of code

Code: Select all

$editDoc = Document.active
$filePath = "~/Dropbox/nisus/Nisus Writer Settings/Style Library/Case report.rtf"
$styleLibDoc = Document.open $filePath
$editDoc.addStyles $styleLibDoc.allStyles, "replace"
$styleLibDoc.close
from the https://nisus.com/forum/viewtopic.php?f=17&t=7255 thread. I modified the path to my environment and that works well.
For (2) I have the macro that Martin crafted for me years ago, and that works well.
With (3) I have 3 problems:
  • The macro I have to regularise footnotes—by Philip, no less, modified by him in 2016!—
    Footnote default text.nwm
    (18.9 KiB) Downloaded 88 times
    returns an error in line 26: "The given character index (1) is out of bounds for the Text object (length 0)".
  • The macro I have to regularise endnotes—again courtesy of Philip in 2016—
    Endnote default text.nwm
    (18.79 KiB) Downloaded 88 times
    runs but makes 0 changes in a document with 50+ endnotes, even though they do not match the imported NWP style definition.
  • The macro to regularise the note numbers in the text, whatever I had no longer works for some reason. When I run the macro to import styles, although the footnote marker in the text is defined as '9pt Adobe Garamond Pro raised 2pts', it applies the raised baseline but leaves the font size as 11pt. I've tried using a Search and Replace which I macroized to replace the '11pt raised 2pts' that I get to the '9pt raised 2pts' that I want. What I have is:

    Code: Select all

    Find and Replace @Text<\p{Lower}\d>, @Text<\0>, 'EauU'
    Where the <\d> is styled '11pt Adobe Garamond Pro raised 2pts' and the <\0> is styled '9pt Adobe Garamond Pro raised 2pts'. It only finds one occurrence, where the footnote number is preceded by a lower case 'L'! This is the case whether it runs as a macro or from the "Search and Replace" dialog.
    If I don't run the set styles macro first, the compiled RTF has in-text footnote markers as 11pt superscript, which is too small in my view, hence wanting to change it to 9pt raised 2pts — my default text size is 11pts. So a macro, which would be font and regular, italic or bold variant agnostic to solve that would be equally as useful.
So I'd love some help with the macros for point [3} I'm in the process of creating a sample file which I'll make available, with the NWP stylesheet.
:)
Mark
Edit to attach compiled RTF file and NWP stylesheet.
Standard.rtf
(7.67 KiB) Downloaded 86 times
Seems 3 is the limit on attachments; RTF text in a new post.

xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro issues …

Post by xiamenese »

RTF file compiled from Scrivener:
test_macros.rtf
(31.63 KiB) Downloaded 145 times
Thanks
Mark

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro issues …

Post by phspaelti »

Hello Mark,
Sorry for the trouble with my macros.

It might help to be clear on what you are trying to achieve. You call these macros, macros to "regularise Footnote and Endnote styles". That is not actually what these macros do, or indeed are intended to do. These macros were intended to solve the issue of "default text". The Nisus note styles allow for "default text" which is really just a bit of plain text that gets inserted with each note. The "problem" is that this text is really just a bit of plain text. Once inserted Nisus has no way of identifying it. If you change the note style to insert a different bit of text—or to insert no text—future notes will get this new text, but the already inserted ones are left unchanged. If you change your note style, but leave the default text unchanged, these macros would/should do nothing.

One point that I had insufficiently considered at the time, was that the user might have no default text at all. In that case these macros fail (on line 26) because the length of the default text is then 0. In such cases these macros should exit more gracefully. The truth is that if you were trying to remove existing default text—without inserting new text—you would probably be best served trying to do that with Find/Replace and the insight into the problem that only humans can bring, since Nisus has no sure-fire way of identifying the existing default text.

But if what you are trying to do is change the actual style (font, point size, etc.), can't you just use the style import feature of Nisus? Just import the styles from your library and ask Nisus to replace the existing ones. Doesn't that work? I actually have very little practical experience with note styles as I don't use notes much in my own life.
philip

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro issues …

Post by phspaelti »

And my lack of experience with footnotes shows.
I just tried a simple test and see that the current version of default text does update existing footnotes. In which case these two macros can be assigned to the dustbin of history.
philip

xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro issues …

Post by xiamenese »

Thanks Philip,

I had come to the conclusion that there must be some sort of default text or code in the notes and markers that was not being found. I can sort the notes out—it is the ruler in the notes that is not being respected—by selecting a note and choosing "All in this style" from the status bar in the window-foot and changing the ruler which is not too onerous, but it would be nice to automate it so they follow the style sheet.

The note markers in the text seem more of a problem with Find and Replace.

As the footnote, endnote and marker styles don’t appear in the styles palette, I can find no way to change them all to the style through the UI, only to reproduce the appearance.

Mark

xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro issues …

Post by xiamenese »

Worse and worse …

I'm running NWP on my last model 17" MBP—i7, 16GB RAM, 2TB SSID, MacOS 10.13.6, the last compatible OS version—and I'm beginning to wonder if that is not fully compatible with NWP 3.1.

I have just discovered that PowerFind and PowerFind Pro, when set to find "Any digit", will only find digits in regular text: it won't find superscripted footnote markers—PowerFind set to find superscripted digits—nor even numbers entered randomly "by hand" and superscripted.

I'm running tests on my 5K iMac (built 2016) at the moment, but I'll try it again on that, when it's through.

:(

Mark

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro issues …

Post by phspaelti »

Hello Mark,
I'm getting a little confused about what the issue is and what you are trying to do.
The note reference (in the document) is not really a number. It's some kind of special character. But it does seem to interrupt Find/Replace and can't seem to be found. (I thought this used to be different, but I'm not sure.)

But can't you select the note references using the style? When I insert notes I get a "Note reference" style. If I select all in that style I get all the note references in the document text.

If for some reason you can't do that you can select them using macro code like this:

Code: Select all

$doc = Document.active
$sels = Array.new
foreach $note in $doc.allnotes
    $sels.push TextSelection.new $note.documentText, $note.documentTextRange
end
$doc.setSelection $sels
If there is some kind of editing/formatting workflow for which you can describe the steps, we can probably work out some macro that works for your case.
philip

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro issues …

Post by phspaelti »

xiamenese wrote:
2020-08-28 01:22:32
I can sort the notes out—it is the ruler in the notes that is not being respected—by selecting a note and choosing "All in this style" from the status bar in the window-foot and changing the ruler which is not too onerous, but it would be nice to automate it so they follow the style sheet.
Why do some notes not respect the "ruler"? (We don't really have rulers anymore, since we left Nisus Classic. So do you mean the paragraph formatting?)

Why do you have to use select "All in this style" to select the notes. The notes reside in the special "fulltext". You can just click in a note and do "Select all" twice to get all the notes. If you wanted to automate this, for footnotes for example, you could use code like this:

Code: Select all

$doc = Document.active
if $doc.footnotes.count > 1
    $doc.setSelection $doc.footnotes[0].fulltext
    # Apply your favorite paragraph formatting here
    Paragraph Style:Normal
end
Of course how you do this would depend on the kind of formatting you like.

But if you would prefer to select just the paragraphs in a particular style, you can use code like this:

Code: Select all

$doc = Document.active
Find All $doc.styleWithName 'MyStyle'
philip

xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro issues …

Post by xiamenese »

phspaelti wrote:
2020-08-28 06:53:41
xiamenese wrote:
2020-08-28 01:22:32
I can sort the notes out—it is the ruler in the notes that is not being respected—by selecting a note and choosing "All in this style" from the status bar in the window-foot and changing the ruler which is not too onerous, but it would be nice to automate it so they follow the style sheet.
Why do some notes not respect the "ruler"? (We don't really have rulers anymore, since we left Nisus Classic. So do you mean the paragraph formatting?)
The footnote and endnote styles are set with a .25" hanging indent; the "Normal" style has a first line indent of .25"; the compiled RTF is marked as follows:
Screen Shot 2020-08-29 at 15.50.07.png
Screen Shot 2020-08-29 at 15.50.07.png (41.31 KiB) Viewed 4026 times
so they display as:
Screen Shot 2020-08-29 at 15.53.24.png
Screen Shot 2020-08-29 at 15.53.24.png (16.79 KiB) Viewed 4026 times
I want to find a way of making them all have the "Footnote" style hanging indent, without having the "Normal" style first line indent as well. That is why I said "respect the ruler".
phspaelti wrote:Why do you have to use select "All in this style" to select the notes. The notes reside in the special "fulltext". You can just click in a note and do "Select all" twice to get all the notes.
That works for the actual notes and I can adjust the indents that way, but I'll experiment with the code you give below. But it doesn't work for the markers in the text; it is there that I have had to select "all in this style. That said, I have actually been looking through the Macro Guide and have succeded in modifying my existing macro to deal with the markers the way I want.
phspaelti wrote:If you wanted to automate this, for footnotes for example, you could use code like this:

Code: Select all

$doc = Document.active
if $doc.footnotes.count > 1
    $doc.setSelection $doc.footnotes[0].fulltext
    # Apply your favorite paragraph formatting here
    Paragraph Style:Normal
end
Of course how you do this would depend on the kind of formatting you like.

But if you would prefer to select just the paragraphs in a particular style, you can use code like this:

Code: Select all

$doc = Document.active
Find All $doc.styleWithName 'MyStyle'
As I say, I'll experiment with the former.

Mark

martin
Official Nisus Person
Posts: 4826
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro issues …

Post by martin »

phspaelti wrote:
2020-08-27 17:40:45
I just tried a simple test and see that the current version of default text does update existing footnotes. In which case these two macros can be assigned to the dustbin of history.
That's true now. The footnote style does enforce "after text" for note references, and updates it automatically. But long ago Nisus Writer didn't have this feature and you would have to update the default text manually if you wanted to change it (or use one of these macros).

martin
Official Nisus Person
Posts: 4826
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro issues …

Post by martin »

xiamenese wrote:
2020-08-29 12:28:59
The footnote and endnote styles are set with a .25" hanging indent; the "Normal" style has a first line indent of .25";
...
I want to find a way of making them all have the "Footnote" style hanging indent, without having the "Normal" style first line indent as well. That is why I said "respect the ruler".
You can do that by selecting all footnotes and removing the undesired paragraph style(s). Here are the steps if you do it manually:

1. Place the selection/caret inside any footnote.
2. Use "Select All" to select the entire footnote.
3. Use "Select All" again to expand the selection to all footnotes in your document.
4. Use the menu Format > Paragraph Style > Remove Paragraph Styles

That will clear away all applied paragraph styles, leaving only the Footnote style intact to enforce whatever paragraph formatting your style sheet defines. If you want to run a macro that accomplishes this task, here's some code:

Code: Select all

$doc = Document.active
$notes = $doc.footnotes
If $notes.count > 0 
	$noteText = $notes[0].fullText
	$doc.setSelection($noteText)
	Menu "Format:Paragraph Style:Remove Paragraph Styles"
End

xiamenese
Posts: 459
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro issues …

Post by xiamenese »

Martin,

Thank you for coming in on this. I've been away from my computer, so couldn't reply earlier.

Your version of the footnote macro has solved one of my problems in that the code I was using worked for footnotes, but I couldn't get it to work on endnotes, leave alone in a document which had both note streams. Now that works fine; I still have a bit of work to do on other matters, but I am slowly learning.

:)

Mark

Post Reply