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: