NISUS Archives

July 2016

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:
Thu, 14 Jul 2016 13:29:27 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (80 lines)
Hello Nobumi,

first the easy answer. You seem to have fallen into the same trap again.
The “.text” property of the selection is not the selected bit, but rather it is the entire text from which the bit is selected. A text selection is not just two numbers as in NW Classic. It’s two numbers and the text from which those numbers select something.
The selected bit of text is called the “.subtext”. So if you change the line

> $note_texts.appendValue $index.text

to 

> $note_texts.appendValue $index.subtext


your macro should work. (I didn’t test this ;-)
But let me make a few more comments below.

> On 2016 Jul 14, at 08:06, Nobumi Iyanaga <[log in to unmask]> wrote:
> I am trying to write a macro, and have a question. I have a document from which I want to extract all the line contents in an array of styled text. Here is what I did:
> 
> $doc_notes = Document.active
> 
> $found_num = Find '^\d+.+$', 'Ea'
> 
> $note_texts = Array.new
> $selections = Array.new
You don’t actually need to create the $selections array here, since the next assignment below creates it. In fact if the assignment below assigned something other than an array, this declaration would be wiped away anyhow.

Creating $note_texts is necessary for the loop, but see below.

> 
> if $found_num
> 
> 	$selections = $doc_notes.textSelections
> 
> 	foreach $index in reversed $selections
> 		$note_texts.appendValue $index.text
> 	end
> 
> 	prompt $note_texts.firstValue
> 
> end
> 

While writing a loop to collect the subtexts will work, it really isn’t necessary, if instead you make $selections a Selection object, you can get the $note_texts directly in one step.

$selections = $doc_notes.selection
$note_texts = $selections.subtexts

Note however that your $note_texts will still be in document order, so if you want to loop through them backward you will still need to use “reversed”. But anyhow now your macro would look like this:

$doc_notes = Document.active
Find …
$selections = $doc_notes.selection
$note_texts = $selections.subtexts

Note that I’m not bothering with the $found_num. If you do want to know that you can always do
$found_num = $selections.substringCount
but you probably don’t need this anyhow, so why bother?

Now using “Find” like this works via the document / GUI, but there is no need for this either. You can do the same thing directly using the document object. That works like this:

$doc_notes = Document.active
$selections = $doc_notes.text.find …

In this case, in one step, we create an array of text selections using the Text object “.find” command. Be careful. In this case $selections is not a Selection object. So we can’t get the subtexts in one step. But since you will be looping through them anyhow, you can just use the $selections array. So the macro looks like this:

$doc_notes = Document.active
$selections = $doc_notes.text.find …
foreach $sel in reversed $selections
     … do something with  $sel.subtext
end

I’ll address how to apply this to your actual problem in a separate mail.
best
Philip


Philip Spaelti
[log in to unmask]

ATOM RSS1 RSS2