[Newbie First Macro] Insert External Documents (Best practic

Get help using and writing Nisus Writer Pro macros.
Post Reply
luciano
Posts: 2
Joined: 2015-06-18 10:14:39

[Newbie First Macro] Insert External Documents (Best practic

Post by luciano » 2015-06-18 12:42:12

Hi i am new in macro programming in Nisus this is what i would like to achieve:

Main Document:

example.rtf
======

This is text, etc etc,.

@@disclaimer@@

@@sign@@

=======

where @@disclaimer@@ i want to introduce the content of disclaimer.rtf from a pre-configured folder
where sign i want to insert the content of a file with the name sign.rtf

Then put the 'full' document at the same directory with an added part to his name like example_print.rtf

I have no experience in macros with Nisus nor perl but i handle regular expressions pretty well and also bash, php, and Apple Script.

With some guidance i know i can do it, any recommendations about 'best practices' or where i can start before noodling inelegant code?

I really want to do my first steps the best i could and write efficient code, and probably here there are many good power users that can share they experience and show me the way to have a better start with Nisus.

By the way, i am new to the community and I want to say hi to the whole community.

Regards from Argentina

Luciano

PD: I do my best to write as clear as i could but english is not my first language if you find obscure writing please let me know.

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

Re: [Newbie First Macro] Insert External Documents (Best pra

Post by phspaelti » 2015-06-20 01:55:31

Hello Luciano, and welcome to Nisus.

You ask about recommendations for 'best practices'. That's an interesting question. Unfortunately there is still no good introduction into how to write macros with Nisus. The current resources are the (rather thin and a bit outdated) explanations in the manual, and the Nisus Macro Reference in the Help menu. The latter is essential, but it is a reference, and aims to cover everything rather than be "introductory".
Otherwise the best ideas are to look at macros written by others.
My own philosophy is that, since most macros are about getting tasks done, is to write them as short as possible. If I do plan to keep them around for longer, then I add comments to remember what I was doing. I also like to use variable names that are as descriptive as possible, which means that I often go back and change the variables once I'm done and understand better how the code works.

Finally as to what you will need. The Nisus Macro language is very good and can do pretty much everything you need now, so there is really no need for perl or applescript. The place to start is usually the Find/Replace dialog. Use it to find the parts you want to change, and then use "Macroize" to start the macro. If you need concepts from the Nisus Macro language you will usually add a command like this at the beginning of your code:

Code: Select all

$doc = Document.active
This will allow you to access all the document properties. Using "$doc" as the variable name for the document seems to be a 'best practice'. I only use other names if I am dealing with more than one document.

Now to what you want to do. The outline would look like this:
  1. Find "@@disclaimer@@" (and save the selection) / Terminate if not found.
  2. Open "disclaimer.rtf"
  3. Copy the text of the disclaimer and insert at the selection (You can use do this without using copy and paste, if you use macro commands instead.)
  4. Close "disclaimer.rtf"
  5. Repeat the steps for the signature
  6. Save at your chosen path
Last edited by phspaelti on 2015-06-22 21:30:08, edited 3 times in total.
philip

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

Re: [Newbie First Macro] Insert External Documents (Best pra

Post by phspaelti » 2015-06-20 02:10:24

Here is my version of code that would insert the disclaimer into an open document. I saved the disclaimer in the Nisus Document folder:

Code: Select all

$doc = Document.active
If ! Find "@@disclaimer@@"
exit 'No disclaimer in this file'
end

$dmFolder = User Property "document manager folder path"
$disclaimerPath = $dmFolder.filePathByAppendingComponent "disclaimer.rtf"

if File.existsAtPath $disclaimerPath
	$disclaimer = Document.open $disclaimerPath, @false
	Insert Text $disclaimer.text
	$disclaimer.close
end
philip

luciano
Posts: 2
Joined: 2015-06-18 10:14:39

Re: [Newbie First Macro] Insert External Documents (Best pra

Post by luciano » 2015-06-22 20:36:54

Thank you, it really helps me to start with the right feet.

Post Reply