Filtering lines with a selection

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 229
Joined: 2007-04-12 14:59:36

Filtering lines with a selection

Post by js » 2010-05-06 13:01:44

Is there a simple means to filter all lines of a text that have a selection, copying those lines into a new document?

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Filtering lines with a selection

Post by Kino » 2010-05-07 05:49:49

It is not clear to me what you mean by “filter” and by “lines”. Anyway...

Code: Select all

 $doc = Document.active
$sels = $doc.textSelections
$lines = Array.new

foreach $sel in $sels
	$a = $sel.text.rangeOfLineAtIndex $sel.location
	$sel.location = $a.location
	$b = $sel.text.rangeOfLineAtIndex $sel.bound
	$sel.bound = $b.bound
	if $lines.lastValue != $sel.subtext
		$lines.appendValue $sel.subtext
	end
end

$LF = Cast to String "\n"
$lines = $lines.join $LF
Document.newWithText $lines
If you mean “paragraphs” by “lines”, replace rangeOfLineAtIndex with rangeOfParagraphAtIndex.

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: Filtering lines with a selection

Post by js » 2010-05-07 10:41:36

This is a great and extremely useful macro, thank you.
I wanted the paragraphs rather than lines and made the necessary changes. There is a little problem though I don't quite understand: I don't need/want the empty lines between the citations. I thought I could just delete the two lines of your macro which have "$LF" in them. But I d not get the desired result. Why is that?

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Filtering lines with a selection

Post by Kino » 2010-05-07 18:13:19

js wrote:I don't need/want the empty lines between the citations. I thought I could just delete the two lines of your macro which have "$LF" in them. But I d not get the desired result.
Use $lines = $lines.join '' instead of the two lines.
Why is that?
See the explanation of join command in the Macro Reference.

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: Filtering lines with a selection

Post by js » 2010-05-07 22:35:12

Thank you. Everything fine now.

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: Filtering lines with a selection

Post by js » 2010-09-15 10:18:26

While using Kinos macro to delete all lines that have a selection, I sometimes found that individual paragraphs were not deleted. In the end I found the origin of the problem: I had sometimes done selections by dragging over several paragraphs at once in order to have them deleted. Of course this selects also the returns between the paragraphs. In this case the macro does not delete the first line. I wonder if this can be remedied?

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Filtering lines with a selection

Post by Kino » 2010-09-15 18:21:32

js wrote:While using Kinos macro to delete all lines that have a selection, [ . . . ]
You seem to have confused this thread with another one to which I have posted a revised version of the macro.

As to the macro posted to this thread, it can be simplified as follows.

Code: Select all

Menu ':Edit:Select:Select Paragraph'
# or Send Selector 'selectParagraph:'
Menu ':Nisus Writer Pro:Services:New Nisus Document With Selection'
In Snow Leopard, the last command does not work unless it is enabled in the Keyboard System Pref.

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: Filtering lines with a selection

Post by js » 2010-09-16 03:08:27

I fear this is what I did. Glad to learn about both ways to remedy the problem. Thank you.

Post Reply