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

logo picture

Perl macros for Nisus Writer Express 2.7x

Nisus Writer Express is becoming better and better with its upgrades. With the version 2.7, we can now have sub-menus in the Macro menu, and we can assign shortcuts with two keys pressed in a sequence. These improvements stimulate the incentive to write new macros.

Here is a pair of macros that can be used when you want to write Perl macros or scripts in Nisus Writer Express. I first wrote these scripts for Jedit X (you will find them at Jedit X Library -- download directly from this link); I adapted two of them for Nisus Writer Express.

Where to install the scripts

I would recommend to put them in a new folder in your Nisus Writer Express Macros folder (/Users/[your_account]/Library/Application Support/Nisus Writer/Macros/): create a new folder named "Perl" in that folder, and put the two AppleScript scripts in that folder.

The two scripts are:

  1. check_syntax.scpt
  2. execute_frontScript_on_rearDoc.scpt

How to use the scripts

check_syntax.scpt

Open a new window, and write any Perl script in it. The script must begin with the "shebang" line, that is:
#!/some_path/perl [some_commandline_switch]
where, usually, /[some_path] is /usr/bin.

You would write for example:


#!/usr/bin/perl
print "Hello World!\n";

Then choose Macro > Perl > check_syntax: you will get a dialog saying:

Syntax OK

Now, if you write:


#!/usr/bin/perl
print "Hello World!\n';

Then, you will get the following reply:

Can't find string terminator '"' anywhere before EOF at junk_1.pl line 2.

The line number indicated in the error message may not be very useful, since NWE cannot display line numbers; but at least, the Syntax OK reply can be useful...

If you write:


#!/usr/bin/abc
print "Hello World!\n';

You will get this error message:

The text of the front document does not seem to be a perl script (add #! line...).

execute_frontScript_on_rearDoc.scpt

Suppose that you have a document of addresses, in which you have for example:

Mr/Mrs/Ms First_name Familly_name
street address
City
tel. 000-1234-5678

Etc... And you would like to get the telephone number of each person. In such a case,
Open a new window, and write the following script:


#!/usr/bin/perl

use strict; use warnings;

use utf8; binmode (STDIN, ":utf8"); binmode (STDOUT, ":utf8");

while (<>) { print if /^M[rs]+s/; print $_, "\n" if /^tel.s[0-9-]+/; }

Now, be sure that you have your address document behind your Perl script, and choose Macro > Perl > execute_frontScript_on_rearDoc: you will get a new document where you will have the following result:


Mr First_name Familly_name
tel. 000-1234-5678

Mrs First_name Familly_name tel. 000-1234-5678

Ms First_name Familly_name tel. 000-1234-5678

Your script must have the "shebang" line:
#!/some_path/perl [some_commandline_switch]

It must have also the three lines:


use utf8;
binmode (STDIN, ":utf8");
binmode (STDOUT, ":utf8");

If you forget to put one of these lines, you will get the following warning:

You should add <use utf8;> in your script...
etc.

As the code above shows, the text of the document behind the script window is passed to the script as "STDIN" (standard input), i.e., in Perl notation, "<>", or "<STDIN>".

Note also that the result text will have no style formatting.


How these scripts work

The two AppleScript scripts get the text of the frontmost document (and that of the document of the rear window), and write it in a text file (or two text files) in the folder /private/var/tmp/folders.501/TemporaryItems/.

Each Perl script will be named junk.pl, junk_1.pl, junk_2.pl, etc., and each text file will be named junk.txt, junk_1.txt, junk_2.txt, etc.

So, when you restart the machine, you will have in your Trash a folder named "Recovered Items", in which you will find these "junk" files (at least on OS 10.4 and later).

The check_syntax script checks the syntax of the written junk.pl file with the command:
perl -c junk.pl

And the script execute_frontScript_on_rearDoc will execute the "junk.pl" on the "junk.txt" with the following command:
perl junk.pl < junk.txt
The result is output to the Standard Output by the print...; command, which will be displayed in a new window in Nisus Writer Express.


Package and download

The package will contain:

Perl/
	check_syntax.scpt
	execute_frontScript_on_rearDoc.scpt
ReadMe.rtf	This file.

Please download the package from this link (86K to download).

Please send me your feedback, bug reports or feature requests.


Go to Research tools Home Page
Go to NI Home Page


Mail to Nobumi Iyanaga


frontierlogo picture

This page was last built with Frontier on a Macintosh on Mon, May 1, 2006 at 1:32:24 PM. Thanks for checking it out! Nobumi Iyanaga