Hello Philip,
Thank you for your reply.
> On Jul 14, 2016, at 1:29 PM, spaelti <[log in to unmask]> wrote:
>
> ...
>
> 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 ;-)
Yes, it worked!
> 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.
Yes, thank you.
>
> 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
How clever!
>
> 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 …
How even more clever!!
>
> 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.
But all this is at the same time so clever and so new for me that I think I will never be able to do like this...
But thank you again, anyway.
All the best,
Nobumi
P.S. Sorry, I sent this reply to [log in to unmask], and it came back. Always the same trouble.
And thank you very much for your two macros. I tried the first one, and it worked like a charm! Thank you so much!
I will write you when I will test the other one.
|