Macro to Traspose Characters

Get help using and writing Nisus Writer Pro macros.
Post Reply
lawrencegoodman
Posts: 30
Joined: 2007-07-30 12:06:49

Macro to Traspose Characters

Post by lawrencegoodman » 2007-08-07 13:13:19

I want to write a macro where it changes the order of two letters. So if the cursor is between the 't' and 'h' in the word 'the' and I trigger the macro, the word will change to 'hte'.

I have written the following macro:

Code: Select all

$locationofcursor = selection location
Select Start
Set Selection $locationofcursor, 1
Cut
set selection location -1
Set Selection $locationofcursor , 1
Paste
Select End
I get an error because evidently you can't use negative numbers. So how do I move backwards one space? I am trying to do this without perl if I can so I can keep it simple and understand what I am doing.

Thanks for your help.

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

Re: Macro to Traspose Characters

Post by martin » 2007-08-07 13:52:13

lawrencegoodman wrote:I get an error because evidently you can't use negative numbers.
Correct, the Set Selection Location command takes an absolute character location/offset in the document. Thus if your document contains (N) characters then only (1) through (N + 1) are valid locations.

As macros currently do not have any commands that perform arithmetic you'll have to resort to Perl to calculate the character location you need. However, I think you'll find the code understandable:

Code: Select all

$locationofcursor = selection location 
Select Start 
Set Selection $locationofcursor, 1 
Cut 
Begin Perl
     $locationofcursor = $locationofcursor - 1;
End
Set Selection $locationofcursor , 0
Paste 
Select End

Post Reply