Reply to topic  [ 3 posts ] 
Transpose macro 
Author Message

Joined: 2013-07-17 01:14:22
Posts: 2
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


2013-07-17 01:18:54
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$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:
# 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:
# 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:
Send Selector 'transpose:'


2013-07-17 13:03:05
Profile WWW

Joined: 2013-07-17 01:14:22
Posts: 2
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


2013-07-17 13:52:10
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 5 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:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software