Selecting All Text with the same background color

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

Selecting All Text with the same background color

Post by js »

Is it possible to select All Text with the same background color, no matter what other attributes are going with that text?
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Selecting All Text with the same background color

Post by xiamenese »

Yes. Put your cursor in an area of text with the colour in question and there should appear on the status bar at the bottom of the window a spot of the colour; click on that and from the menu that appears select all.

:)

Mark

Sorry, I misread your question and also forgot I was in the Macros forum. Someone more on the ball should be along soon, I'm sure.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting All Text with the same background color

Post by phspaelti »

Well just to continue where Mark left off, first of all, if you just want to select those parts you could use Kino's macro "Select By Attributes". Moreover if you want to know how to do this yourself, you can look there to see how Kino does it. The basic idea for selecting by attribute is to use a "Kino loop", which looks like this:

Code: Select all

# get a text object and put it in $text
$i = 0
while $i < $text.length
	$attr = $text.displayAttributesAtIndex $i
	$range = $text.rangeOfDisplayAttributesAtIndex $i
	# check and do stuff here
	$i = $range.bound
end
In your case you will want to check the attributes for the appropriate background color and then use the range to create a selection for the bits you want, and put those selections into an array. Then at the end you can use that selection array as an argument to the .setSelection command.

So the whole thing should be like so:

Code: Select all

$doc = Document.active
$text = $doc.text
$desiredColor = Color.new…  # set appropriately  (see macro reference)
$bgcolorSels = Array.new
$i = 0
while $i < $text.length
	$attr = $text.displayAttributesAtIndex $i
	$range = $text.rangeOfDisplayAttributesAtIndex $i
	if $attr.textBackgroundColor == $desiredColor
		$bgcolorSels.push TextSelection.new($text, $range)
	end
	$i = $range.bound
end
$doc.setSelection $bgcolorSels
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Selecting All Text with the same background color

Post by js »

Thank you Philippe. This is certainly good adivice for the procedure as such.
This said I am not sure about a Kino micro called "select by attributes". I see that there are macros selecting style, language and font. Where you referring to one of these?

But probably my question was not as precise as I had intended . I see now that the word background color is used for text as well as for the shading of a whole paragraph. What I wish to extract from certain documents are paragraphs with the same shading, not considering differences in text attributes.

Would you know how to obtain the paragraph shadings?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting All Text with the same background color

Post by phspaelti »

js wrote:Thank you Philippe. This is certainly good adivice for the procedure as such.
This said I am not sure about a Kino micro called "select by attributes". I see that there are macros selecting style, language and font. Where you referring to one of these?
Exactly it's one of those. At least on my set-up there is also one called "Select by Attributes". In fact all of those macros work on the same basic principle of the "Kino loop".
js wrote: But probably my question was not as precise as I had intended . I see now that the word background color is used for text as well as for the shading of a whole paragraph. What I wish to extract from certain documents are paragraphs with the same shading, not considering differences in text attributes.

Would you know how to obtain the paragraph shadings?
Yes, the macro will look practically the same just replace .textBackgroundColor with .paragraphShadingAttributes. Paragraph Shading Attributes are a bit more complicated. In the simplest case you can just compare .backgroundColor to the $desiredColor variable. So perhaps

Code: Select all

if $attr.paragraphShadingAttributes.backgroundColor == $desiredColor
would work for your case. But Paragraph Shading Attributes can be more complicated and allow for Foreground Color as well, and also allows for blended combinations and opacity settings.

One option might be to have the macro find all parts that have the same Paragraph Shading Attributes as the current selection. It really depends a bit on what you want.

But actually I have to say that if you are looking for paragraph shading attributes, unlike other attributes they will only change for whole paragraphs. So you could probably simplify the macro a bit by only moving from paragraph to paragraph, and checking the attributes once for each paragraph. But the basic idea of looping through the text and collecting the ranges/selections into an array stays the same.
philip
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Selecting All Text with the same background color

Post by js »

Thank you again.
In fact my start position is not to chose a background color. The idea is to put the cursor into a paragraph X with para bg color Y and to have the macro select all other paragraphs that have para bg color Y. So rather than to define a para bg color I wonder what I should do to get it, for the para with the insertion point.

But now I discover, a bit by chance and to my surprise the following: If I select, in one of those colored paragraphs, a character written in standard black default font, and copy it to the find window, replacing it there with ".*" and select attribute sensitive, a regex find all will select all paragraphs with the same bg color. What surprises me: all text in the desired paragraphs is selected, including bold, underlined, colored text etc. Well, this is in fact what I wanted.
Post Reply