Reply to topic  [ 8 posts ] 
Filtering lines with a selection 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
Is there a simple means to filter all lines of a text that have a selection, copying those lines into a new document?


2010-05-06 13:01:44
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
It is not clear to me what you mean by “filter” and by “lines”. Anyway...
Code:
 $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.


2010-05-07 05:49:49
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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?


2010-05-07 10:41:36
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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.

Quote:
Why is that?
See the explanation of join command in the Macro Reference.


2010-05-07 18:13:19
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
Thank you. Everything fine now.


2010-05-07 22:35:12
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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?


2010-09-15 10:18:26
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
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.


2010-09-15 18:21:32
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
I fear this is what I did. Glad to learn about both ways to remedy the problem. Thank you.


2010-09-16 03:08:27
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software