NISUS Archives

December 2010

NISUS@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Mon, 27 Dec 2010 02:27:12 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (143 lines)
Hello, again,

On Dec 25, 2010, at 10:19 PM, Nobumi Iyanaga wrote:

> For example, I completely forgot to write Classic Nisus Writer macros...

Me too... Do you remember? We had to invent something very tricky just to get a Hiragana equivalent of [A-Za-z]?

> And it depends also on the ideas about what kind of macros can be useful for one's work.

Fortunately or unfortunately, I'm using NWP macros a lot for making money. Cleaning up OCRed files, making formatting elements and style attributes consistent, proof reading, etc. It is astonishing to find how many and how much unimaginable errors may occur and affect a file produced by Japanese OCR program(s?).

>> For example, someone may have difficulties in understanding the zero-based character index, the concept of Text object, that of Text Selection object, etc.
> 
> The concept of Text object and that of Text Selection object are really very hard to understand. Could you explain them in some examples?...

But you understand them. You use them in your macros correctly. When I wrote the above-quoted words, I had in mind those who have not been very familiar with NW Classic macro (especially with its programming dialect), not you.

Anyway, I try to explain. As far as I understand, various kinds of object are called objects because you can get information (properties) about an object (e.g. $textObject.length returns length of $textObject) and you can modify it using a writable property or a command (e.g. $textObject.deleteInRange $range).

In NWP macro, any kind of text is called Text object. It may belong to a document (e.g. $doc.text) or may be something defined in a macro (e.g. $digits = '0123456789'). And all Text objects are treated in the same way. characterAtIndex command returns a decimal Unicode value of a character at a given character index whether it belongs to a document or to a variable defined by you in the macro file.

And TextSelection object is not different from NW Classic macro's pair of Start and End, except that TextSElection object consists of not only a range of selection but also a Text object to which the selection belongs. For me (at least), that is a great improvement. Now we can manipulate a document with a macro regardless of what the active Text object is, in other words, regardless of the text area in which the insertion point is situated, e.g. main body, footnotes, header, etc., even if NWP is NOT the frontmost application.

Of course, range of selection in TextSelection object is defined with location (Start in NW Classic macro) and length by default and presumably internally. That is different and may make a problem for those who need translating NW Classic macros into NWP macro langage. However, TextSelection.newWithLocationAndBound command is available, which enables you to define a TextSelection object with Start (location) and End (boundary).

For me, what was really difficult to understand is Nisus Macro Reference.zrtf's discussion about Storage Semantics. But what makes it look difficult turned out to be just the wording (sorry, Martin). Perhaps it is due to my poor English? Are "value semantics" and "reference semantics" intuitive enough for native English speakers like many of you???

In *my* understanding (again), this is a very simple story: usually you can use "=" to make a separate copy of what is on the right of "=". For example, $b = $a in

	$a = 'One and one and one is three'
	$b = $a

is (or "can be") regarded as a convenient alternative of

	$a = 'One and one and one is three'
	$b = 'One and one and one is three'

However, for Text object returned by a certain kind of properties or commands (e.g. text, textAtRowAndColumn). This symbol (=) creates an alias or shortcut to the original object. Your making changes to the alias/shortcut does change the original object.

s
I'm afraid perhaps I misunderstood your question. If there is anything still unclear to you, please specify that anything.

And I *did* misunderstand. Below is the latter half of the first (and interrupted) draft of this reply, which *may* help someone on the list in a certain scope, I hope.


Kino



---
I try to explain.


A document consists of multiple text objects. What kind of objects? For example, try to run the following macro on Nisus Macro Reference.zrtf.

### macro 1 ###

$doc = Document.active
$output = undefined
foreach $text in $doc.allTexts
	$output &= $text.documentContentType & "\n"  # &=: append text
end
Document.newWithText $output

### end ###


You may be surprised to find the Macro Reference contains so many tables. However, exactly speaking, they are not tables as we understand them but table cells. Each and every table cell is a separate object.

Anyway, if you want to see something more legible, try this one.

### macro 2 ###

$doc = Document.active
$types = Hash.new
foreach $text in $doc.allTexts
	$types{$text.documentContentType} += 1  # NWP seems to assume $Hash{<new value>} to be 0
end

$output = undefined
foreach $type in $types.keys
	$output &= $types{$type} & " $type\n"  # \n: newline (return)
end

exit $output

### end ###


As you see in the examples above, when you want to repeat the same operation(s) on all text objects, you can use this construction:

	$doc = Document.active
	foreach $text in $doc.allTexts
		<operation 1>
		<operation 2>
		<operation 3>
		...
	end


Then, what to do with those text objects? You can gather information about them. For example, you can use "length" property to know the length of a text object, i.e. how many characters a text object consists of.


### macro 3 ###

$doc = Document.active

$output = undefined
foreach $text in $doc.allTexts
	$type = $text.documentContentType
	$len = $text.length
	$output &= "$type: $len characters\n"  # \n: newline (return)
end

Document.newWithText $output

### end ###


It would be a nice exercise to try to improve the macro 3 so that it outputs something more legible like that of the macro 2.


###   ###

$doc = Document.active
$types = Hash.new
$sels = $doc.text.findAll 'Nisus', '-iw' # case-sensitive, whole words
foreach $sel in $sels
	$types{$sel.text.documentContentType} += 1
end

$output = undefined
foreach $type in $types.keys
	$output &= $types{$type} & " in $type\n"  # \n: newline (return)
end

exit $output

###   ###

However, this constructions seems to be of no use except on some relatively rare occasions, for example, when you need parsing style attributes (like in the macro I posted in the first message of this thread) or running transliterateInRange command on the whole document.

ATOM RSS1 RSS2