Macro to select table and apply border.

Get help using and writing Nisus Writer Pro macros.
Post Reply
vwnisus
Posts: 10
Joined: 2016-04-24 00:13:31

Macro to select table and apply border.

Post by vwnisus » 2016-07-25 05:57:36

I have a Nisus document with several tables.

I wish to put the cursor in a table (something I will do manually) and then wish to run a macro to

1. to select the table
2. add a border of 1pt, including adding lines within the table (i.e. all round individual cells),with lines being solid (i.e. not dotted, dashed); and
3. increase the padding.

For 1. I can see I need to use:

Code: Select all

Select Table
After this I am lost as how to deal with 2. and 3.

For 2. do I need to use

Code: Select all

 LineAttributes.newWithLineType styleName
?

Even if I do it is not clear how to write the line of macro code (as the description indicates "Returns line attributes that draws using one of the given named styles:", i.e. indicates an attributable but not applies it).

Any help would be gratefully received.

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro to select table and apply border.

Post by phspaelti » 2016-07-26 00:19:51

Hello vwnisus

Since the task you are trying to achieve will require the macro language and use of the macro objects, it's usually a good idea to start with the following line:

Code: Select all

$doc = Document.active
This gives you a "hold" on the Nisus document object, through which you access everything else. For example you will need the table that you have selected. After the above command you can get that as follows:

Code: Select all

$sel = $doc.tableSelection
$table = $sel.table
Now what can you do with the $table? For one thing you can select it, like this:

Code: Select all

$doc.setSelection $table
Ok, now on to your problem. You made a good start here:
vwnisus wrote:I have a Nisus document with several tables.

For 2. do I need to use

Code: Select all

 LineAttributes.newWithLineType styleName
?

Even if I do it is not clear how to write the line of macro code (as the description indicates "Returns line attributes that draws using one of the given named styles:", i.e. indicates an attributable but not applies it).
Well what that last bit means is that you need to assign the line attributes to something, for example a variable:

Code: Select all

$solid = LineAttributes.newWithLineType "solid"
With that line you have solid line attributes in the variable $solid. Now you're ready to apply the line attributes to something. There are two options:
  • The cell object has a command .setLineAttributesForEdges (note the "set" and the plural here)
  • The table selection object has a command .applyLineAttributesToEdges (note the "apply" here)
If you read the fine print on these two, you'll notice that there are some differences, and you'll quickly see that the latter will be way more convenient in your case. But it requires that we have (A) a table selection, and (B) that that selection encompass the entire table.
The reason why I stress this point is that earlier I already retrieved a table selection (with the command "$sel = …"). The only problem is that that selection is whatever you may have clicked before starting the macro. So it might just be one cell of the table. But now we do have the whole table selected, so we can easily retrieve a new table selection that covers the whole table.

Code: Select all

$sel = $doc.tableSelection    # update $sel to encompass the whole table
$sel.applyLineAttributesToEdges $solid, "Normal"
(The "Normal" means all the orthogonal edges. Look in the Macro Language Reference for other possible patterns.)

OK. So let's put it all together.

Code: Select all

$doc = Document.active
$sel = $doc.tableSelection    # Get the selection
$table = $sel.table
$doc.setSelection $table    # Select the whole table. Could have done this with "Select Table" instead
$solid = LineAttributes.newWithLineType "solid"     # Create the line attributes
$sel = $doc.tableSelection    # Update $sel to encompass the whole table
$sel.applyLineAttributesToEdges $solid, "Normal"  # Apply the line attributes to the table
Ok. Now the only thing left is the padding. Piece of cake. And here we find … [drumroll] … :? :o There is no way to do it ! Yeah, that's right. I guess the programmer dude at Nisus was out surfing that day :P
There is no menu command to control padding, and no macro command either. So that's a bummer. Somebody once tried using AppleEvents to click in the drawer, but short of that you're just going to have to do it by hand after the macro finishes.

Anyhow hope that helps

PS: Once you get used to the macro language, you'll see that you can write the whole thing a little more compactly without all those variables, like this:

Code: Select all

Select Table
$doc = Document.active
$doc.tableSelection.applyLineAttributesToEdges(LineAttributes.newSolid, 'Normal')
:wink:
philip

User avatar
martin
Official Nisus Person
Posts: 4251
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro to select table and apply border.

Post by martin » 2016-09-21 15:51:24

phspaelti wrote:Now the only thing left is the padding. Piece of cake. And here we find … [drumroll] … :? :o There is no way to do it ! Yeah, that's right. I guess the programmer dude at Nisus was out surfing that day :P
There must have some bodacious waves out at Mavericks that day! ;) But seriously, I'm sorry about this restriction. Some table features are exposed only via the palettes, not via the menus and macros, and that's a shortcoming we should improve.

Post Reply