Part of Nobumi Iyanaga's website. n-iyanag@ppp.bekkoame.ne.jp. 5/5/04.

logo picture

Batch Process Macros

I present here two Nisus macros that may be useful for the users of Nisus Writer in general, and more specifically for those people who write themselves macros. The two macros, that can be called "batch process macros" are somehow "frame macros". To be useful, you must create yourself macros that will be called from within these macros.

Batch Process macro:
Here is how the macro "Batch Process" was developped. All began with this message of George Maschke on the Nisus mailing list, on Sun, 28 Feb 1999 (http://listserv.dartmouth.edu/scripts/wa.exe?A2=ind9902&L=nisus&D=0&O=A&T=0&P=51433):

I need to run a macro on all files in a single folder, and am wondering if anyone has already figured out a way to automate this?

I suspect it cannot be done with macros alone (though I'd be delighted if someone could show me otherwise), but I think it should be doable with AppleScript or Frontier. ...

There were several replies to this posting, among which one that I wrote (http://listserv.dartmouth.edu/scripts/wa.exe?A2=ind9902&L=nisus&D=0&O=A&T=0&P=51844). George worked on the idea that I presented, and wrote this macro (http://listserv.dartmouth.edu/scripts/wa.exe?A2=ind9903&L=nisus&T=0&F=&S=&P=1914). Here is how he described his macro:

I adapted the method that Nobumi Iyanaga described and put together a macro that will run another macro on all files in a folder and its subfolders.

I hope that this code may be of some use to others who use Nisus Writer. I have written a number of macros that convert files from other word processing formats (mostly old Arabic or Persian word processors) into Nisus format. These macros often take a long time to run, even on a G3 Macintosh.

However, with the macro below, I can have my Mac work on a whole folder full of files overnight or while I'm away from my computer. If I were converting each file individually, it would require a lot of attention from me at times that would be hard to predict.

One thing I noted in adapting Nobumi's code is that it is important to clear the Search List before adding files from the currently-selected folder to it. Otherwise, any file which might have already been on the Search List will also be selected for processing.

Before each file is processed, the macro will speak the number of files remaining. Some may consider this a nuisance, but I find it helpful for guaging progress. I also added code to generate a little report at the end of processing. The report provides a list of all files that were processed, along with their paths.

This was in 1999, that is the time of Nisus Writer 5.x. There were some bugs in Nisus macro language in this version, which were affecting directly this macro, so that George (and I) had to add some lines in order to work around them. Now, with Nisus Writer 6.5, these bugs are fixed -- so that the macro as written by George would not work...! I tried to contact George to ask him either to revise his macro, or allow me to do that and upload the macro to my web site. Unfortunately, his old mail address didn't work -- so I take the liberty of doing that without his permission. I think/hope that he would not be too injured.

Please read carefully the comments inside the macro to understand how to use it. Essentially, you will:

1. write a macro that will work on a file; normally it should be a macro that will change it or convert it in some way [be sure that this is a well-written macro, tested on many files. If it fails on one file, the process will stop there!];
2. you will locate in the macro "Batch Process" the line written in red: currently, it is written "Batch_process_macros:ex_concatenate_files". "Batch_process_macros" is the name of the macro file; "ex_concatenate_files" is the name of the example macro that I wrote for this release. -- You will overwrite this line (or you may simply comment it out with "\//" at the beginning of the line), and write there your_macro_file_name:your_macro_name.
3. you will open the Catalog Window, and select the files that you want to process with your macro. You can select folders as well; be aware that if you select folders, all the sub-folders and files inside them will be processed.
4. you will run the macro "Batch Process": it will open successively all the files that you have selected in the Catalog Window, and run your macro on these files. -- Note that in typical cases, there should be no window opened at the moment of the execution of the macro. But there may be other "non-typical" cases.

Please look at the line in blue, just below the line in red, which says currently:
//Save // commented out for the macro "ex_concatenate_files"
Normally, you would rather remove the comment sign "//" from this line -- that will save the changes made by your macro in the files. -- BUT be aware that that will OVERWRITE your files. It would be safer to use duplicated files, keeping your original files somewhere else.

Instead of the writing your_macro_file_name:your_macro_name on the line in red, you may write there your macro lines as well.

Be aware that if you process many files at once, and/or if your macro is a complicated one, using much memory, etc., then Batch Process macro will use MUCH memory; you might want to allocate more memory to Nisus Writer.

The example macro "ex_concatenate_files" is not a typical macro for Batch Process macro. To use it, you will:

1. Open a new document;
2. Select files to be concatenated in the Catalog Window;
3. Run the macro "Batch Process".

This macro will not change the selected files in the Catalog Window (this is why I commented out the line "Save"); instead, it will make a new document in which all the opened files will be gathered. So, of course, this macro will "eat" MUCH memory if you select many big files.

You can use this macro for example to gather the texts of different chapters of a book you wrote in different files.

You might want to use Batch Process macro with one of the converting macros contained in my "font<->HTML_Entities macros" that you will find at my page East Asian Diacritical Fonts and Unicode. In that case, the "red line" will be, for example:

font<->HTML_Entities macros:ITimesSkRom2HtmlEntity

Batch Process by Find macro:
This macro is rather for an educational purpose. The main part of this macro is:

Find All "xxx" "gaA-S"
s -> push (StartEnds)

loop:
if (! s -> size) GoTo finish
SetSelect (s -> pop, s -> pop)

[macro lines]

GoTo loop

finish:

This is the typical form of many Nisus macros: it will do a "Find All" with a regex pattern, and go through every found string in the file, beginning from the end of the file. The macro "Batch Process by Find" provides this frame, letting the users to enter the Find pattern, and the "macro lines" that will work on each found string.

Here is how this macro works:

1. A dialog box asking the user to enter a search pattern or use the Find Box pattern will appear, with the default answer "fbox".
2-a If the user press the "OK" button (or press "Return" or "Enter" key), the search pattern in the Find Box will be used to do a "Find All" in the current window (the search options will be those of the Find Box) [so, before running this macro, the user must have entered an appropriate Find pattern in the Find Box, with appropriate search options];
2-b The user can enter the search pattern in the dialog box; a "Find All" will be executed with that pattern; the search options will be those hard-coded in the macro [they are in BLUE characters below] -- you should change these options so that they are appropriate for your search. -- By the way, you may use the option "*" which means "Use current state of the Find/Replace dialog to determine any unspecified option (this option must be first to have an effect)" -- but be aware that you cannot do any style attribute search with this method;
2-c If the user delete "fbox" in the dialog box and press "OK", the macro will be canceled; nothing will happen.
3-a If nothing is found with the Find pattern, the macro will stop there.
3-b If there are found strings, a loop will select each occurrence of the found strings, beginning from the end of the text, executing a macro that the user will have entered on the RED line below.

So, before running this macro, the user will have:
1. Tried the search pattern that will be used; he will have decided either to use the Find Box, or the Dialog Box pattern (in this case, he should examine the search options in the macro, and change them if necessary);
2. The user will have written a macro to be executed on each of the found string, and entered its file name and macro name in this macro.

The global variables "btprocess_i" and "btprocess_ct" can be accessed from the called macro. You will find an example of how to use one of these variables ("btprocess_ct") in the example macro "ex_number_found_string". The variable "btprocess_i" represents the number of found strings * 2; the variable "btprocess_ct" represents the number of loops.


Please download Batch Process macros with ReadMe from here (16 KB)


Mail to Nobumi Iyanaga


frontierlogo picture

This page was last built with Frontier on a Macintosh on Wed, May 5, 2004 at 11:15:35 PM. Thanks for checking it out! Nobumi Iyanaga