Page 1 of 1

[Newbie First Macro] Insert External Documents (Best practic

Posted: 2015-06-18 12:42:12
by luciano
Hi i am new in macro programming in Nisus this is what i would like to achieve:

Main Document:


This is text, etc etc,.




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


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.

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

Posted: 2015-06-20 01:55:31
by phspaelti
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 =
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

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

Posted: 2015-06-20 02:10:24
by phspaelti
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 =
If ! Find "@@disclaimer@@"
exit 'No disclaimer in this file'

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

if File.existsAtPath $disclaimerPath
	$disclaimer = $disclaimerPath, @false
	Insert Text $disclaimer.text

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

Posted: 2015-06-22 20:36:54
by luciano
Thank you, it really helps me to start with the right feet.