Page 1 of 1

Filtering lines with a selection

Posted: 2010-05-06 13:01:44
by js
Is there a simple means to filter all lines of a text that have a selection, copying those lines into a new document?

Re: Filtering lines with a selection

Posted: 2010-05-07 05:49:49
by Kino
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.

Re: Filtering lines with a selection

Posted: 2010-05-07 10:41:36
by js
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?

Re: Filtering lines with a selection

Posted: 2010-05-07 18:13:19
by Kino
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.

Re: Filtering lines with a selection

Posted: 2010-05-07 22:35:12
by js
Thank you. Everything fine now.

Re: Filtering lines with a selection

Posted: 2010-09-15 10:18:26
by js
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?

Re: Filtering lines with a selection

Posted: 2010-09-15 18:21:32
by Kino
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.

Re: Filtering lines with a selection

Posted: 2010-09-16 03:08:27
by js
I fear this is what I did. Glad to learn about both ways to remedy the problem. Thank you.