Reply to topic  [ 2 posts ] 
Macro to Traspose Characters 
Author Message

Joined: 2007-07-30 12:06:49
Posts: 30
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:
$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.


2007-08-07 13:13:19
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$locationofcursor = selection location
Select Start
Set Selection $locationofcursor, 1
Cut
Begin Perl
     $locationofcursor = $locationofcursor - 1;
End
Set Selection $locationofcursor , 0
Paste
Select End


2007-08-07 13:52:13
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 2 guests


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