Reply to topic  [ 11 posts ] 
applying menu commands and importing stylesheets 
Author Message

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
I have never written any macros before and have tried looking through the macro reference under the help menu in NWP, but haven’t been able to find what I want. I am trying to put together a macro to do four tasks in relation to an RTF exported from Scriivener which has endnotes. Scrivener exports the endnotes at the end of the entire document with the index number followed by a “.” and a space; I want the endnotes to be placed at the end of the relevant sections with a hanging indent and a tab separating the index from the note text. A further point about the exported file is that Scrivener doesn’t currently have styles, but the endnotes are recognised by NWP as endnotes and are given a zero-style marker (if I can put it that way) which must be removed, which I do currently using Paragraph —> Remove Attributes (thanks to Martin for pointing that out to me). In Scrivener, I begin each endnote with a '»', so I can use Powerfind (Pro) to search and replace all instances of ‘. »’ with a tab, and on exporting from Scrivener, a placeholder ‘‡‡‡‡‡’ is put to mark what will be the ends of sections.

So, the whole process needs four operations: (1) select all the endnotes and remove paragraph attributes; (2) replace all ‘. »’ with tab; (3) convert section placeholders to ‘Section Break Next Page’; (4) import the relevant stylesheet from the styles library, replacing where necessary the basic styles that NWP assigns on opening the document.

I have macroized the Replace All ‘. »’ I had worked out for stage (2) and combined that with a macro that Martin kindly set up for me to replace the section placeholders in (3). But what I would really like to do is macroize (1) and (4). At the moment, I have a keystroke to invoke Paragraph —> Remove Attributes, but I have to select all the endnotes before I can apply it, and a keystroke to call my combined macro for (2) & (3); I then have to go to the styles view to import the stylesheet from the library. I have no idea how to write macros to perform operations (1) and (4), so would be eternally grateful if someone could point me in the right direction, so that, eventually, I could make a single macro to do the whole job at one go.

Thanks for reading and for any help. :)

Mark


2014-01-25 06:12:30
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hello Mark,
from the way you describe it, it seems that Nisus in fact doesn't recognise the endnotes. Wouldn't you want the notes converted to real endnotes, i.e., notes linked to a reference marker in the text, with the numbering and formatting handled by the endnote style? If so you might have to approach the whole problem a bit differently.

But putting that aside, let's consider how you would do the two tasks you are asking about.
For task (1) you can just do it exactly as you describe it: you select the endnote paragraphs, then do Paragraph —> Remove Attributes. Selection would need to be handled via Powerfind (Pro). This would look approximately like this:
Code:
Find All '^\d+\. .+', 'Ea'
Format:Paragraph:Remove Paragraph Attributes


For task (4) 'importing a style sheet' really just means adding certain styles to your document. This can be done easily with the following lines of macro code:
Code:
$doc = Document.active
$doc.addStyles 'Paragraph style 1', 'replace'
$doc.addStyles 'Paragraph style 2', 'replace'


Repeat for as many styles as you want to add. Just make sure to apply the actual style to the '.addStyles' lines, since that is how Nisus imports the style.

Hope this helps. Ask again if you still have questions.

_________________
philip


2014-01-25 10:25:58
Profile

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
Thanks so much for your help; it’s really appreciated.

phspaelti wrote:
Hello Mark,
from the way you describe it, it seems that Nisus in fact doesn't recognise the endnotes. Wouldn't you want the notes converted to real endnotes, i.e., notes linked to a reference marker in the text, with the numbering and formatting handled by the endnote style? If so you might have to approach the whole problem a bit differently.

In fact, NWP does recognise the notes as endnotes — or footnotes if preferred. Keith has modified the standard Apple Text Engine and the RTF produced by the exporter, so that they do have the necessary codes. When I said “a zero style-marker”, I should have found a better way of putting it; they are marked as endnotes, so for instance have the relevant indices in the text and the note, the separator line, respond to changes in the style sheet, etc.; but they have no attributes, such as ruler with hanging indents, different font size from main text, etc. Those are all set in the “Book” style-sheet I have created, and when that is imported using Martin’s macro, they change immediately. That part is not a problem. The point is, if you like that the only "styles" exported by Scrivener are endnotes, footnotes and comments, but they are minimalist in the sense that they use the default ruler, font, etc. All else, for the moment, is a way of storing and ‘painting’ paragraph attributes during editing, which can be retained or over-ridden during export, but which are not fully fledged paragraph styles as in NWP, Word, etc. and changing the attributes of any stored format doesn’t change any existing paragraphs that have used that preset style.

phspaelti wrote:
But putting that aside, let's consider how you would do the two tasks you are asking about.
For task (1) you can just do it exactly as you describe it: you select the endnote paragraphs, then do Paragraph —> Remove Attributes. Selection would need to be handled via Powerfind (Pro). This would look approximately like this:
Code:
Find All '^\d+\. .+', 'Ea'
Format:Paragraph:Remove Paragraph Attributes

I’ve tested this one. if I open the exported file and run this macro, it does nothing. If I scroll down to the endnote area, place the caret at the beginning of the first note, and run the macro it only selects and removes the attributes in the first note; it doesn’t find all instances. If I select all the endnotes manually, and then run the macro, it then replaces the attributes on all of them. But without the “Find All” working, it is just as quick to use my shortcut keystroke. The current part document has 168 notes; the final document will run to several times that amount.
phspaelti wrote:
For task (4) 'importing a style sheet' really just means adding certain styles to your document. This can be done easily with the following lines of macro code:
Code:
$doc = Document.active
$doc.addStyles 'Paragraph style 1', 'replace'
$doc.addStyles 'Paragraph style 2', 'replace'


Repeat for as many styles as you want to add. Just make sure to apply the actual style to the '.addStyles' lines, since that is how Nisus imports the style.

The stylesheet I import is titled “Book” and actually contains 30 style definitions in total under character, paragraph, notes and lists — many of the main ones are duplicated to handle Chinese and English — and I suspect I will need a few more. Is there a way to import the “Book” stylesheet and all the styles in one go, or am I right in thinking I will need to list all those that are relevant individually — there may be some, say in the “lists” section which are redundant. if so, how to I reference them? Like this:

Code:
$doc = Document.active
$doc.addStyles ‘Heading 1', ‘Document Manager:Styles:Book:Heading 1’


and for inserting a new style rather than replacing an existing one:

Code:
...
$doc.addStyles ‘Document Manager:Styles:Book:Chinese Block Quote’


phspaelti wrote:
Hope this helps. Ask again if you still have questions.

It does help indeed, even though I still don’t know why the first doesn’t work, nor entirely how to set about the second. But I am really grateful all the same.

Mark


2014-01-26 04:49:24
Profile

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
Hi again,

I’ve been doing more experimenting, and I have discovered why the "Find All … Replace Attributes” macro isn’t working … it is doing the find all in the main text of the document and not in the note area. I discovered this by using PowerFind Pro itself to search for

Code:
^[[:digit:]]+\.

leaving out the following space, and it found the few instances of numbered paragraphs in the main document.

So for the moment, as I have to select all the notes anyway, by putting the caret at the beginning and pressing Shift-Cmd-downarrow, I might as well use my current Shortcut to remove the paragraph attributes.

Mark


2014-01-26 05:31:40
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hello again.
I guess I should have listened to what you were saying. Since the notes really are imported as notes, then of course my approach to problem (1) will not work. In that case however you can select the note text just the way you would usually do. (Unless you usually select the note text by clicking on the notes with the mouse. That doesn't work. You need to use the menu commands in a macro.)
Code:
Go to Note Reference
Go to Note Text
Select All
Select All

Note that you need 2 "Select All"s since the first only selects the first note.

This approach should work fine, if [1] you are in the main text, and [2] your document actually has some notes. Personally I don't like macros that depend on such preconditions. So it turns out you can use the macro language to get this done. The code looks like this:
Code:
$doc = Document.active
if ! $doc.allNotes.count
exit 'No notes in this document'
end
$doc.setSelection $doc.allNotes[0].fullText

So this code first checks to see if there are any notes in the documents and then it selects them, or quits the macro with a message. You can add the "Remove Paragraph Styles" command at the end of this code.

Now to problem (4). I did some testing and the following should work:
Code:
$doc = Document.active
$styleSheet = Document.open '~/Documents/Nisus Documents/Style Library⁠/‌____.rtf'
$doc.addStyles $styleSheet.text, 'ignore'
$styleSheet.close

Basically this uses the same command I suggested before, and it does this by using the text of the style sheet (which should contain all the styles you want to add). Notice that I am using 'ignore', so conflicting styles that are already in the document are not replaced. Adjust that as desired.
Also before you run this for yourself, fix the file path of the style sheet to the path appropriate in your case.

Finally one thing. If you are combining all these parts into a single macro, you don't need to repeat the "$doc = Document.active" line each time. Just place that line once at the beginning of the whole macro.

_________________
philip


2014-01-26 19:54:39
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
xiamenese wrote:
The stylesheet I import is titled “Book” and actually contains 30 style definitions in total under character, paragraph, notes and lists — many of the main ones are duplicated to handle Chinese and English — and I suspect I will need a few more. Is there a way to import the “Book” stylesheet and all the styles in one go, or am I right in thinking I will need to list all those that are relevant individually — there may be some, say in the “lists” section which are redundant. if so, how to I reference them? Like this:

Code:
$doc = Document.active
$doc.addStyles ‘Heading 1', ‘Document Manager:Styles:Book:Heading 1’



Just one comment about this question (though hopefully my code above will take care of this).
The .addStyles command somewhat confusingly requires a text object (i.e., a string of text) as the first argument. It actually doesn't care what the content of this text is at all, it just takes the formatting applied to the text string. The command does allow for a second argument, but that second argument must be an exact string of the style 'ignore' or 'replace' (see the reference for details). So:
Code:
$doc = Document.active
$doc.addStyles ‘Heading 1', ‘ignore’


Would add whatever styles are applied to the string 'Heading 1', which might be 'Heading 2' for all we know!

I actually think Nisus should modify this command to allow for '.addStyles' to allow for a style object as an argument (or better yet an array of style objects). But that's for another day.

_________________
philip


2014-01-26 20:20:19
Profile

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
Hi Philip,

I’m getting there with your help. I’ve put your code together with what I had working, giving the following macro:

Code:
# Find all endnotes and remove paragraph attributes
$doc = Document.active
if ! $doc.allNotes.count
exit 'No notes in this document'
end
$doc.setSelection $doc.allNotes[0].fullText
Format:Paragraph:Remove Paragraph Attributes

# Replace hanging indent placeholders
Find and Replace '\. »', '\\t', 'Ea'

# Find all section placeholders
$placeholder = '‡‡‡‡‡'
$breakType = 'Next Page'
$breakMenuPath = ":Insert:Section Break:$breakType"

# Replace all placeholder lines with actual section breaks
Select Document Start
While Find Next "$placeholder\n", 'E'
   Menu $breakMenuPath
End

And it all works perfectly. But when I add the code for importing the stylesheet:

Code:
# Import stylesheet
$styleSheet = Document.open '~/Users/Mark/Documents/Nisus Documents/Style Library⁠/Book.rtf'
$doc.addStyles $styleSheet.text, 'replace'
$styleSheet.close


It barfs with the following error alerts:
Code:
“There was an error on line 24 in the macro “endnote”.
Could not access the file “~/users/Mark/Documents/Nisus Documents/Style Library/Book.rtf” because enclosing folder does not exist.

Code:
There was an error on line 24 in the macro “endnote”.
Attempt to set variable using a command that does not return a value.

Code:
There was an error on line 25 in the macro “endnote”.
The “text” property requires an object, instead found “undefined” from “$styleSheet"

I’ve checked the file path in the Book.rtf stylesheet and I cant see any errors in it. If I leave that code out, run the macro on the file, then import the stylesheet manually, it works perfectly.

Mark


2014-01-28 10:16:42
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
xiamenese wrote:
… But when I add the code for importing the stylesheet:

Code:
# Import stylesheet
$styleSheet = Document.open '~/Users/Mark/Documents/Nisus Documents/Style Library⁠/Book.rtf'
$doc.addStyles $styleSheet.text, 'replace'
$styleSheet.close


It barfs with the following error alerts: …

The second and third error are just on account of the first failure.

Actually '~' in a file path designates the "Home" folder, so I am guessing that you should be writing:
Code:
$styleSheet = Document.open '~/Documents/Nisus Documents/Style Library⁠/Book.rtf'

If that still doesn't work, you can try using an absolute path (one that starts with '/' and does not contain any '~').

_________________
philip


2014-01-28 15:53:16
Profile

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
phspaelti wrote:
The second and third error are just on account of the first failure.

Actually '~' in a file path designates the "Home" folder, so I am guessing that you should be writing:
Code:
$styleSheet = Document.open '~/Documents/Nisus Documents/Style Library⁠/Book.rtf'

If that still doesn't work, you can try using an absolute path (one that starts with '/' and does not contain any '~').

Still doesn’t work, and I can’t find any absolute path, beginning at any point — even starting with the name of the computer with or without an initial ‘/‘ — that works.

Leave it for the moment, Philip; you’ve spent enough of your time on my little problem. I’ll email Martin and see what he says. I’ll let you know when I hear from him.

Cheers,
Mark


2014-01-29 10:14:31
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
Hmm....

Here's a macro that you can run to find the correct file path. (You could also adjust it to open the correct file for you :) )
Attachment:
Find Style Library File 'Book'.nwm [16.57 KiB]
Downloaded 171 times

_________________
philip


2014-01-29 16:37:47
Profile

Joined: 2006-12-08 00:46:44
Posts: 417
Location: London or Exeter, UK
phspaelti wrote:
Hmm....

Here's a macro that you can run to find the correct file path. (You could also adjust it to open the correct file for you :) )


Hmm … as you say! :wink:

Ran the macro, result:

Attachment:
Nisus Writer ProScreenSnapz005.jpg
Nisus Writer ProScreenSnapz005.jpg [ 27.09 KiB | Viewed 4530 times ]

Put that file path in the other macro, result:

Attachment:
Nisus Writer ProScreenSnapz004.jpg
Nisus Writer ProScreenSnapz004.jpg [ 34.36 KiB | Viewed 4530 times ]

If I change the last line to

Code:
open $styleLibFiles[$ind]

'Book.rtf' opens without problem, so I'll see if I can work out how to include that in my macro and to copy the styles across into my target document.

Thanks so much for your continuing support.

Mark


2014-01-30 01:33:04
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 11 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software