Reply to topic  [ 4 posts ] 
Select characters on both sides of insertion point 
Author Message

Joined: 2006-03-12 12:32:47
Posts: 138
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


2008-07-22 16:57:09
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Here's a simpler way, at least in terms of lines of code, but really the idea is the same:
Code:
$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:
$length = Selection Length
If 0 == $length
    $location = Selection Location
    Set Selection $location - 1, 2
End


2008-07-22 18:14:44
Profile WWW

Joined: 2006-03-12 12:32:47
Posts: 138
Just what I was looking for!

What is the disadvantage of the "less modern", "superseded" selection commands compared with the new range object commands?


2008-07-22 21:32:34
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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.


2008-07-22 22:05:22
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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