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:
Nobumi Iyanaga <[log in to unmask]>
Reply To:
Date:
Sat, 25 Dec 2010 22:19:34 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (263 lines)
Hello Kino,

On Dec 25, 2010, at 3:23 AM, Kino wrote:

> I agree with you. But at the same time, I tend to think the most  
> efficient way to understand and master the NWP macro language, or  
> script languages in general, is to write macros which show how  
> macro commands work and how a document is structured from the macro  
> interpreter's point of view. Seeing is knowing.

I entirely agree with you. All depends on the time devoted to try to  
write -- and the continuous effort, because we easily forget. For  
example, I completely forgot to write Classic Nisus Writer macros...  
And it depends also on the ideas about what kind of macros can be  
useful for one's work. If we don't have enough incentive to write  
macros, then, of course, we would never write any.

>
> 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?...

> BTW, for such a purpose, I prefer "exit" command to "Prompt" for  
> the Exit dialog box allows you to copy text displayed in it while  
> the latter does not. And I have "TestMacro.nwm" with a shortcut so  
> that I can try this kind of investigations easily.

Since the time of Classic Nisus, I always have a shortcut for "Macros  
 > Run selection as macro" (or "Execute Selection") (Command + E +  
S); I write chunks of macro, select one of them, and try to run it...

> Yes, exactly. And it merges continuous Text Selection objects. As I  
> explained before, "Author's <i>Title<i>" is treated as two Text  
> Selection objects by attributesAtIndex and displayAttributesAtIndex  
> commands because of the italic style because of the italic style  
> although it is irrelevant for the macro. If you don't merge them,  
> you will get "Author's [url text]<i>Title<i>[url text]" which is  
> undesirable.

Yes, that part of the macro is the "advanced part", which would be  
not really necessary to understand for beginners such as me...

>
> Perhaps ShowAttributes macro may be helpful to understand the  
> difference between Attributes and Display Attributes. I wrote this  
> one to understand that difference.
> <http://www2.odn.ne.jp/alt-quinon/files/NWPro/misc/ 
> ShowAttributes_nwm.zip>

This is very helpful. Thank you. The line
$displayAttrIndex.doCommand $attr
is especially interesting.

> Also, I'd like to recommend a regular expression tutorial available  
> at:
> <http://www.regular-expressions.info/tutorial.html>
> (A few regex features are not supported by NWP or supported with a  
> different meta character. So don't be surprised if some examples do  
> not work in our beloved word processor.)

And there is also the excellent <http://www2.odn.ne.jp/alt-quinon/ 
files/NWPro/PowerFindPro_rtf.zip>...

>
> Make Link Visible macro does not use any regular expression, or  
> rather no find (and replace) command is used in it. But this is  
> exceptional. Many macros do use and require regular expressions and  
> NWP is an ideal environment for make yourself acquainted with them.  
> What is nice with the regex is that it is platform/application  
> independent. If you will have mastered its basics, you can use it  
> anywhere.

Perhaps I will ask you another question about how to write a macro  
with a regex pattern...

>
>> Can I ask you to add comments to your macro lines, so that we  
>> would be able to understand a little better their meaning; then  
>> perhaps I would ask you more questions about some of the points  
>> which will be more difficult to understand, etc.
>
...
> Anyway, below is a commented version of the macro. And I replaced  
> the uploaded file with this one which may look more legible  
> (comments in dark green).
> <http://www2.odn.ne.jp/alt-quinon/files/NWPro/link/ 
> MakeLinkVisible_nwm.zip>

This is very helpful too. Thank you very much!
>
> ### Make Link Visible ###
>
> # Insert link destination text after each link with $prefix and  
> $suffix, e.g.
> # Apple will be Apple [http://www.apple.com/]
> # Language of the current insertion point will be applied on texts  
> to be inserted.
>
>
> # define $prefix, $suffix, $highLightColor and $textColor used for  
> the output
> # the purpose of "Cast to String" is to prevent style attributes
> # in the macro file from affecting the output
> $prefix = Cast to String ' ['
> $suffix = Cast to String ']'
> $highLightColor = Color.newWithRGB255 234, 235, 255
> $textColor = Color.newWithRGB255 0, 0, 127
>
> $doc = Document.active  # Get Document object of the frontmost  
> document.
> if $doc == undefined  # i.e. if no document is open...
> 	# exit silently for the macro is supposeed to have been run  
> accidentally.
> 	exit
> end
>
> # Get the active (and first) Text Selection object.
> $selActive = TextSelection.active

I guess this "active (and first) Text Selection object" may be either  
the first actually selected text, or, when there is no selection, the  
first character of the document, or perhaps more probably the first  
character after the insertion point...?

>
> # Get the display attributes of $selActive.
> # It is *display* attributes in order to cover attributes defined  
> in styles too.
>
> $attr = $selActive.text.displayAttributesAtIndex $selActive.location
> $lang = $attr.language  # will be applied on the output.
> $fontSize = $attr.fontSize - 1  # will be applied on the output.
>
> # Create/initialize a new array to store Text Selection objects
> # for text portions having hyperlink.
> # Each Text Selection object consists of a Text object ID,
> # a character index of the location and the length of the selection.
> $sels = Array.new
>
> # The foreach loop below will go through every $text (Text object  
> of the document).
> # A document consists of multiple Text objects, body text, header,  
> footer,
> # footnotes, endnotes, table cells (a table cell is an independent  
> text object).
>
> foreach $text in $doc.allTexts
> 	# Set $i (character index) to 0 (the very begining of $text [Text  
> object])
> 	$i = 0
> 	# As character index is zero based, the index of the last character
> 	# of a Text object is always "$text.length - 1"
> 	while $i < $text.length
> 		# Get attributes at $i.
> 		# Instead of attributesAtIndex, you can use displayAttributesAtIndex
> 		# command which is safer when you are not sure if attribute(s)  
> you are
> 		# looking for can be detected by attributesAtIndex command.
> 		$attr = $text.attributesAtIndex $i
> 		# Get range of text having the same attributes.
> 		# You can use rangeOfDisplayAttributesAtIndex command as well and
> 		# it may be preferable for the reason mentionned in the last  
> comment.
> 		$range = $text.rangeOfAttributesAtIndex $i
> 		# if $attr.link is defined, i.e. if $i has hyperlink url...
> 		if Defined $attr.link
> 			# Create a Text Selection object corresponding with
> 			# $text (Text object) and $range (location and length)
> 			$sel = TextSelection.new $text, $range
> 			# See if this Text Selection object is continuous to
> 			# to the previous and last Text Selection object.
> 			# Let's suppose it is not continuous.
> 			$isContinuous = false
> 			# If $sels is empty, there is no possibility of the continuity
> 			# for there is no previous Text Selection object.
> 			if $sels.count  # i.e. if $sels.count is one or more...
> 				# AND if the end of the last Text Selection object
> 				# coincides with the beginning of $sel
> 				# (newly created Text Selection object)...
> 				if $sels.lastValue.bound == $sel.location
> 					# AND if both of them belong to the same Text object...
> 					if $sels.lastValue.text.isSameObject $text
> 						# then, they are continuous.
> 						# So extend the length of the last Text Selection object
> 						# instead of adding this $sel to $sels.
> 						$sels.lastValue.length += $sel.length
> 						# Change the value of $isContinuous to true
> 						# to prevent "$sels.appendValue $sel" below
> 						# from being executed.
> 						$isContinuous = true
> 					end
> 				end
> 			end
> 			# If $isContinuous remains false, this means
> 			# that this $sel is NOT continuous to the previous and last
> 			# Text Selection object.
> 			if $isContinuous == false
> 				# So let's add this $sel to $sels.
> 				$sels.appendValue $sel
> 			end
> 		end
> 		# Move $i to just after $range.
> 		$i = $range.bound
> 	end
> end
>
> if ! $sels.count  # i.e. if $sels.count is zero...
> 	exit 'No link found, exiting...'
> end
>
>
> # Inset hyperlink destination text (url) with $prefix and $suffix
> # just after each text portion having hyperlink from the document end
> # to the document start.
>
> # It is convenient to process from the end to the beginning
> # for inserting something will change the location of
> # the remaining document text.
>
> foreach $sel in reversed $sels
> 	# Get attributes at the location (starting point) of $sel.
> 	$attr = $sel.text.attributesAtIndex $sel.location
> 	# Put $prefix and $attr.link (hyperlink destination) in $linkText.
> 	$linkText = $prefix & $attr.link
> 	# Append $suffix to $linkText.
> 	# You cannot use "$linkText = $prefix & $attr.link & $suffix"
> 	# for the macro interpreter does not allow youto put
> 	# more than a single command (& in this case) on the right of =.
> 	$linkText &= $suffix
> 	# Use "Push Target Text $linkText ... Pop Target Text"
> 	# to apply style attributes on a variable ($linkText).
> 	Push Target Text $linkText
> 		$lang.apply
> 		Set Font Size $fontSize

These two lines seem to apply to the $linkText the "language  
attribute" and the "font size attribute - 1" of the "active (and  
first) Text Selection object", which were found at the beginning of  
the macro. I guess these attributes may not be the same at the place  
of the insertion, don't they...?

> 		Set Text Color $textColor
> 		Set Highlight Color $highLightColor
> 	Pop Target Text
> 	# Inset $linkText at the end of $sel
> 	$sel.text.insertAtIndex $sel.bound, $linkText
> end
>
> ### end of macro ###

Thank you so much for all these explanations. They are very clear. I  
think I could understand them...

Best regard,

Nobumi Iyanaga
Tokyo,
Japan

ATOM RSS1 RSS2