A macro to move a selection after a title

Get help using and writing Nisus Writer Pro macros.
Post Reply
cirillof
Posts: 12
Joined: 2011-05-01 23:38:35

A macro to move a selection after a title

Post by cirillof »

Sometimes when I write a chapter of a book I feel the need to write down a note (ie todo, question, ...) in the text.
I press return, write the note, assign a style to it (ie "todo" style changes color and font size), I press return and keep writing.
When I reread the chapter I would like to move either all of those notes or a single note under a different heading named Todos

This is my objective:
I would love to be able to create two macros able to (1) move a single selection or (2) all of those todo styled paragraghs right after a heading with a certain name (Todos) (usually at the beginning of the chapter).

I just learned how to select a particular style, but can't get how to move a selection under a styled header (Header2) with a predefined name ("Todos").

Any hints?

Thanks

Francesco
Groucho
Posts: 497
Joined: 2007-03-03 09:55:06
Location: Europe

Re: A macro to move a selection after a title

Post by Groucho »

Hello, Francesco.

I assume that each of your todos is a paragraph. If so, you can append-copy them while you go. Select the paragraph(s) by clicking four times and then right-click and select Append Copy (I don’t know what’s its name in your language). When you’re done with it, go to where you want to gather all your todos and paste.

Cheers, Henry.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: A macro to move a selection after a title

Post by martin »

You could also select all those "to do" paragraphs, copy them all at once (NWP will copy a multipart selection), and then paste them wherever you like. Let us know if you have any trouble.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: A macro to move a selection after a title

Post by martin »

Oh, I forgot to mention, in case it wasn't apparent, how to select all those paragraphs at once:
1. Place the selection in any paragraph using your "to do" paragraph style.
2. Use the menu Format > Paragraph Style > Select All Style (or the menu from the paragraph style tag).
cirillof
Posts: 12
Joined: 2011-05-01 23:38:35

Re: A macro to move a selection after a title

Post by cirillof »

Hi everybody,

Re: I assume that each of your todos is a paragraph. If so, you can append-copy them while you go...

Thanks Henri for your help. I appreciate the append-copy solution you offered.

Nevertheless that solution would require too much effort on my part :-). I just want to run a macro and it is able to move the current paragraph directly under a styled header (ie header 2) with a predefined name (ie "To Dos"). Or to execute a second macro, that is able to take all of the "some sort of style" (ie "Todo" Styled paragraph) paragraphs and move them under a styled header (ie header2)

Re: Martin - Yes I think this might work. I start working on this: Format > Paragraph Style > Select All Style

I am not sure how a macro can identify the point after a paragraph with style "Header2" and with content "Todos", programmatically. I'll work on that too.

I'll let you know. I'm kind of a rookie with nisus macros... Thanks for you help

Francesco
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: A macro to move a selection after a title

Post by martin »

cirillof wrote:I just want to run a macro and it is able to move the current paragraph directly under a styled header (ie header 2) with a predefined name (ie "To Dos").
When you say "predefined name", do you mean that your paragraph's text is "To Dos"? Like the actual text in your document body is typed as "To Dos"? If so, here's a macro that moves the paragraph containing the current selection underneath such a target paragraph:

Code: Select all

Select Paragraph
Cut
If Find Next '^To Dos\n', 'E'
	# paste after the target paragraph
	Select End
	Paste
Else
	# could not find the target paragraph
	Undo
	Prompt 'Could not find a "To Dos" paragraph.'
End
That macro doesn't care what style you have applied to the target paragraph, so long as its text is exactly just "To Dos".
cirillof
Posts: 12
Joined: 2011-05-01 23:38:35

Re: A macro to move a selection after a title

Post by cirillof »

re: When you say "predefined name", do you mean that your paragraph's text is "To Dos"?

correct

Your code is ok and simpler than expected.

Question:

If Find Next '^To Dos\n', 'E'

Is it possible to put a double check:
1 the paragraph = '^To Dos\n'
>> AND <<
>> 2 the paragraph has style "header 2" <<

?

Thanks Martin

Francesco
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: A macro to move a selection after a title

Post by martin »

Glad to help. As to your question, there are two ways to make sure the target paragraph also has the proper heading style applied.

Here's the explicit way:

Code: Select all

If Find Next '^To Dos\n', 'E'
	If Menu State 'Paragraph Style:Heading 2'
		# paragraph found and in proper heading style
		...
You could also use an attribute sensitive find expression. You do that by applying the desired attributes to the code in the macro, and adding 'u' to the options string, eg:

Code: Select all

If Find Next '^To Dos\n', 'Eu'
Just be careful that your "Find Next" macro code has only exactly the attributes you want.
cirillof
Posts: 12
Joined: 2011-05-01 23:38:35

Re: A macro to move a selection after a title

Post by cirillof »

Hi Martin,

Yes, "Menu State" works.

Question:

Code: Select all

If Find Next '^To Dos\n', 'Eu'
I am not clear on how to use the attribute to define a particular "style state" in a macro using u.

I took a look at the macro specification but wasn't able to get to how to represent paragraph properties by using "u".

At the same time the idea seems promising even for other macros I would need.

Thanks again.

Francesco
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: A macro to move a selection after a title

Post by martin »

cirillof wrote:I am not clear on how to use the attribute to define a particular "style state" in a macro using u.
The 'u' option turns on the attribute sensitive matching for the whole find expression/pattern (eg: ".+"). So whatever formatting you have applied to the expression in the actual macro code will be used when matching. I'll attach an example macro that does some simple matching that should give you the idea.
Attachments
SelectExample.nwm
(22.48 KiB) Downloaded 653 times
Post Reply