Highlight paragraphs with border(s)

Get help using and writing Nisus Writer Pro macros.
Post Reply
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Highlight paragraphs with border(s)

Post by johseb »

I'm trying to write a simple macro that checks every paragraph of the open document and highlight it if the paragraph has one or more borders.

Here's my first attempt:

Code: Select all

$doc = Document.active
$paras = $doc.text.find('^.+\n','Ea')
$green = Color.green

forEach $para in $paras
	$attr = $para.text.displayAttributesAtIndex $para.location
	if ?????? how to check for borders ??????
		Push Target Selection $para
		Set Highlight Color $green
	End
End
I'm stuck when it comes to check the presence of the borders. I tried using paragraphLineAttributesForEdge to no avail.
Also the Push selection step could probably be avoided.

Any help would be greatly appreciated :)
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Highlight paragraphs with border(s)

Post by phspaelti »

johseb wrote: 2020-08-23 08:47:27 I tried using paragraphLineAttributesForEdge to no avail.
That's what it says in the macro language reference in two locations. I'm suspecting that there may be a typo somewhere (maybe even in the code :? ). I've tried a few variations, but they all come back with errors. So I can't seem to check paragraph borders either.

But assuming that would work your code should look something like this (your initial lines omitted):

Code: Select all

$solid = LineAttributes.newSolid

$highlightedParas = Array.new
foreach $para in $paras
    $attr = $para.text.displayAttributesAtIndex $para.location
    if $attr.paragraphLineAttributesForEdge('top') == $solid
        $highlightedParas.push $para
     end
end
$doc.setSelection $highlightedParas
Set Highlight Color $green
So yes, you should "push" the paras you find. Best to do the highlighting all at once. In the above code you might want to check after the loop whether anything was found at all before you select and highlight.
philip
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Highlight paragraphs with border(s)

Post by johseb »

Hi philip, thanks for your constant support on the forum!
At this point only @martin can probably throw some light on the matter.

Building a macro is not an easy task. Once a year, maybe less, I put togheter a few pieces of code stolen here and there but I must admit that I never got to grasp the language.
For instance, this is what the macro language reference has to say about the property in object:
Screen Shot 2020-08-24 at 00.18.33.png
Screen Shot 2020-08-24 at 00.18.33.png (49.56 KiB) Viewed 15316 times
so I naively tried

Code: Select all

$attr.paragraphLineAttributesForEdge "top"
while your code uses

Code: Select all

$attr.paragraphLineAttributesForEdge('top')
How can a user guess, reading the reference guide, the correct syntax for a specific command/property?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Highlight paragraphs with border(s)

Post by phspaelti »

johseb wrote: 2020-08-23 14:40:55 How can a user guess, reading the reference guide, the correct syntax for a specific command/property?
Both are equally valid.

I like to put parentheses when things get long. There are some circumstances when you are required to use parentheses, basically when you need to disambiguate something. You can always use parentheses. The "Nisus style" in the reference is to generally use without parentheses as much as possible it seems.

If in doubt you can avoid such issues by writing code more step by step, assigning intermediate values to named variables.

Similarly for the use of single vs. double quotes. Double quotes will interpolate, but since in this case the names are simple text strings there will be no difference. Again, I just have a personal preference for single quotes in such cases.
philip
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Highlight paragraphs with border(s)

Post by johseb »

Thanks Philip, very instructive.

So, given that paragraphLineAttributesForEdge returns a LineAttributes object, I guess I can write something like:

Code: Select all

if $attr.paragraphLineAttributesForEdge('top').lineType == "Solid"
or even (to detect any type of border):

Code: Select all

if $attr.paragraphLineAttributesForEdge('top').lineType != "Invisible"
while the same approach will probably not work with the syntax with spaces:

Code: Select all

if $attr.paragraphLineAttributesForEdge 'top'.lineType == "Solid"
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Highlight paragraphs with border(s)

Post by phspaelti »

Exactly.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Highlight paragraphs with border(s)

Post by phspaelti »

It occurs to me that there is a workaround to checking for paragraph borders. If you check the (display) attributes you can see that the paragraph borders are included, as expected. So if you cast the attributes object to a string you can search in the string representation for the presence of the relevant attribute. This works like this:

Code: Select all

$sel = TextSelection.active
$attr = $sel.text.attributesAtIndex $sel.location
$attrStr = Cast to String $attr
if $attrStr.find "paragraph.{,10}Edge=YES", 'E'
    prompt "Got borders"
else
    prompt "No borders"
end
In this version you don't even have to be specific about which type of edge you are looking for.
philip
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Highlight paragraphs with border(s)

Post by johseb »

Philip, that is really ingenious!
Thanks again; the macro now works perfectly. Here it is in the final form if anyone else is interested:

Code: Select all

$doc = Document.active
$paras = $doc.text.find('^.+\n','Ea')
$green = Color.green
$highlightedParas = Array.new

foreach $para in $paras
    $attr = $para.text.displayAttributesAtIndex $para.location
    $attrStr = Cast to String $attr
    if $attrStr.find "paragraph.{,10}Edge=YES", 'E'
        $highlightedParas.push $para
    end
end

$doc.setSelection $highlightedParas
Set Highlight Color $green
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Highlight paragraphs with border(s)

Post by martin »

phspaelti wrote: 2020-08-23 10:17:18 That's what it says in the macro language reference in two locations. I'm suspecting that there may be a typo somewhere
It looks like paragraphLineAttributesForEdge is completely non-functional and MIA, sorry about that! I'll get it scheduled to be fixed in the next software update. Thank you for pointing it out.
Post Reply