Move Comments to Marked Text (in one swell foop)—Solved

Get help using and writing Nisus Writer Pro macros.
User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by phspaelti » 2018-07-08 19:14:34

Hi jb. That's clever! Just use Nisus built in default output for the Comment object.
Here is a slightly modified version of your macro, that directly gives you the list without needing to use Find/Replace to clean it up.
Attachments
GatherComments.nwm
(5.87 KiB) Downloaded 74 times
philip

jb
Posts: 89
Joined: 2007-11-09 15:27:25

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by jb » 2018-07-10 11:38:14

Thanks, Philip.
Of course I was thinking of you when I mentioned the macro guru, but it's been five years (!).
I'm traveling now so can't try out your improved version.
Will do so next week when I'm back in town and thank you properly then.

Cheers,
James

jb
Posts: 89
Joined: 2007-11-09 15:27:25

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by jb » 2018-07-18 16:26:05

Yes, your version is a big improvement (of course).
So: Thank you!

I wondered why $listDoc = is in the final line
$listDoc = Document.newWithText $commentList.join("\n")

Of course it was in the macro I put up recently, but I didn’t really understand it. It came along for the ride when I copied/pasted from another, earlier macro.

Then I deleted the part that mystified me ( $listDoc =), and I discovered that it isn’t necessary.
My guess now is that it was used to do two things at once: to create a new Document and also to define that new document as a named object. And since I don’t need to do anything more than create the new document whose text is the list, I’ve just deleted that part of the last line.

I need to read a bit in the Macro Reference Guide to understand arrayByMakingValuesDoCommand


Thanks again

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by phspaelti » 2018-07-18 23:19:03

jb wrote:
2018-07-18 16:26:05
I wondered why $listDoc = is in the final line
$listDoc = Document.newWithText $commentList.join("\n")

Then I deleted the part that mystified me ( $listDoc =), and I discovered that it isn’t necessary.
My guess now is that it was used to do two things at once: to create a new Document and also to define that new document as a named object. And since I don’t need to do anything more than create the new document whose text is the list, I’ve just deleted that part of the last line.
That is exactly how it is.
I need to read a bit in the Macro Reference Guide to understand arrayByMakingValuesDoCommand
The arrayByMakingValuesDoCommand command (that's quite a mouthful :) ) is a bit of macro magic that takes one array and creates a second array of the exact same size by applying a command to each value in the first array. Since the $doc.allComments is an array of Comment objects, but we only want the text of the comments, we apply the text command (ok, technically it's a property, not a command, but whatever…) to each object in the array and get an array of texts. So this one command is equivalent to writing a loop code. Something like this:

Code: Select all

$newArray = Array.new
foreach $value in $oldArray
    $newArray.push $value.doCommand
end
I think the name of the command is supposed to come out as something like "create a (new) array by making the values of the array do the command" and then the name of the command to do follows as an argument.

Hope that helps.
philip

Vanceone
Posts: 123
Joined: 2013-05-03 07:06:31

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by Vanceone » 2018-07-20 08:39:23

In short: arrayByMakingValuesDoCommand is "Map" in other languages. I wonder: is there an equivalent to "Filter" and "Reduce?"

User avatar
phspaelti
Posts: 956
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by phspaelti » 2018-07-20 19:12:44

Vanceone wrote:
2018-07-20 08:39:23
In short: arrayByMakingValuesDoCommand is "Map" in other languages. I wonder: is there an equivalent to "Filter" and "Reduce?"
I see. So JavaScript has this kind of thing.

Nisus arrayByMakingValuesDoCommand is a bit more limited than map, since you can only use it for maps of arrays of objects using properties/commands that apply to that object. For example you can take an array of selections and get their lengths, but you couldn't extend all the selections by n characters, since the selection object does not have a an "extend by n characters" command.
And if you have an array of number values you can't use arrayByMakingValuesDoCommand at all, since number values are not objects.

And there is nothing like "Filter" or "Reduce". But you can always write defined commands for doing things like that.

In the end Nisus macro language is tailored for the tasks you will most likely need as a (text) document creator. And if you need more technical stuff you can always insert a Perl block.
philip

Vanceone
Posts: 123
Joined: 2013-05-03 07:06:31

Re: Move Comments to Marked Text (in one swell foop)—Solved

Post by Vanceone » 2018-07-26 10:07:48

Hmm. That's a subtle quirk there Philip. The Macro language guide, under the "Call" section, explains how to build a proper "Map" command, though you apparently have to hand build the function. You can no doubt do "filter" and "Reduce" the same way. I wish it was built in, though.

The macro language is a curious combination of functional and object oriented.

Post Reply