Select characters on both sides of insertion point

Get help using and writing Nisus Writer Pro macros.
Post Reply
Bob Stern
Posts: 171
Joined: 2006-03-12 12:32:47

Select characters on both sides of insertion point

Post by Bob Stern »

For use in a kerning macro, I need a way to select the two characters on opposite sides of the insertion point. The only solution I could figure out is the following, which seems rather complicated for such a simple task. Is there a simpler solution?

### Begin Macro: ###

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

If $selection.length == 0
$location = $selection.location

# Optionally insert here a test for insertion point at beginning of document.

$selection2 = TextSelection.newWithLocationAndLength ($doc.text, $location-1, 2)
$doc.setSelection ($selection2)
end

### End of Macro ###


# Optional commands to be inserted where indicated above:
If $location == 0
Prompt 'Error: Insertion point is at beginning of document.'
Exit
end


FYI, AppleScript has a fairly simple way to select the characters surrounding the insertion point:
tell application "System Events" to tell process "Nisus Writer Express"
key code 123 --left arrow
key code 124 using shift down --right arrow
key code 124 using shift down
end tell
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Select characters on both sides of insertion point

Post by martin »

Here's a simpler way, at least in terms of lines of code, but really the idea is the same:

Code: Select all

$selectedRange = TextSelection.activeRange
If 0 == $selectedRange.length
    $newRange = Range.new($selectedRange.location - 1, $selectedRange.length + 2)
    TextSelection.setActiveRange($newRange)
End
Or, if you want to stay away from the new object commands:

Code: Select all

$length = Selection Length
If 0 == $length
    $location = Selection Location
    Set Selection $location - 1, 2
End
Bob Stern
Posts: 171
Joined: 2006-03-12 12:32:47

Thanks, Martin!

Post by Bob Stern »

Just what I was looking for!

What is the disadvantage of the "less modern", "superseded" selection commands compared with the new range object commands?
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Select characters on both sides of insertion point

Post by martin »

There's no disadvantage in this case, the code does exactly the same thing. However, the older commands don't have the same flexibility as the newer commands. For example, there's no way to create a noncontiguous selection using the old selection commands.
Post Reply