Transpose macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
saggers
Posts: 2
Joined: 2013-07-17 01:14:22

Transpose macro

Post by saggers » 2013-07-17 01:18:54

New user of Nisus Writer Pro. Like it a lot.

In Microsoft Word I created a transpose macro to switch two letters in the middle of a word. Connected to a key combination I found it very useful. I did it by using the record macro feature. I cannot find any way to do this easily (or at all) in Nisus. Could someone please help.

Thanks

Leigh

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

Re: Transpose macro

Post by martin » 2013-07-17 13:03:05

Hi Leigh: unfortunately NWP does not have a record macros feature, but one can definitely write a macro to transpose letters.

But before we get to that, I should say there's already a keyboard shortcut built into every Mac that does what you want: Control + T will transpose the letters in most apps, including NWP. Here's a list of some other default keyboard shortcuts for the Mac.

As to a macro that handles transposing letters, there's definitely more than one way to skin this cat. Personally I'd probably go the most direct route and manipulate the macro text objects directly:

Code: Select all

$doc = Document.active
$selection = $doc.textSelection
$text = $selection.text

# make sure we have letters on both sides of the selection
If 0 == $selection.location
	Exit "No letter before the selection to transpose."
End
If $selection.location >= $text.length
	Exit "No letter after the selection to transpose."
End

# cut the 2nd letter
$afterRange = Range.new( $selection.location, 1 )
$secondLetter = $text.subtextInRange( $afterRange )
$text.deleteInRange( $afterRange )

# insert the cut letter before the 1st
$insertIndex = $selection.location - 1
$text.insertAtIndex( $insertIndex, $secondLetter )
That's a lot of code, and maybe too complicated for the uninitiated, but it does allow for the most control.

An alternative method is to use PowerFind Pro (regex) to do the moving around, so you don't have to worry about character indexes and ranges:

Code: Select all

# cut the 2nd letter
Find Next '.', 'E'
$secondLetter = Read Selection
Delete

# insert the 2nd letter before the first
Find Previous '.', 'E'
Select Start
Write Selection $secondLetter

# place the selection after the affected text
Find Next '.', 'E'
Select End
That code can actually be reduced even further by using PowerFind's back references. It could be as short as:

Code: Select all

# move the selection back a single character
Find Previous '.', 'E'
Select Start

# swap the two letters
Find and Replace '(.)(.)', '\\2\\1', 'E'

# place the selection after the affected text
Select End
And if you happen to know about Cocoa and selectors, you could cheat by writing the macro like this:

Code: Select all

Send Selector 'transpose:'

saggers
Posts: 2
Joined: 2013-07-17 01:14:22

Re: Transpose macro

Post by saggers » 2013-07-17 13:52:10

Martin
After I read your first sentence I realised that I use the transpose function in OS X. I must have switched off in word processor mode. I am grateful for your efforts and I will study the code. it will give me some insight into the nisus macro language

Leigh

Post Reply