Last Character in a Line (Soft Hyphens)

Get help using and writing Nisus Writer Pro macros.
Post Reply
adryan
Posts: 198
Joined: 2014-02-08 12:57:03
Location: Australia

Last Character in a Line (Soft Hyphens)

Post by adryan » 2018-01-31 02:24:59

G’day, all

My world seems to be revolving around hyphens at the moment.

My current interest is the manual insertion of hyphens in words that are too long to fit at the end of one line but whose wrapping to the next line leaves too much white space in the (fully justified) previous line. I think that the use of soft hyphens in such cases could avoid the problem associated with “conventional” hyphens which persist inappropriately when subsequent textual manipulations (such as insertions or deletions) result in the word’s appearing all on the one line: soft hyphens, by contrast, magically appear and disappear as required.

The problem with soft hyphens is that Nisus Writer Pro displays a soft hyphen differently from a conventional hyphen, so all instances of the former which occur at the end of a line — and only those that occur at the end of a line! — will eventually need to be replaced by instances of the latter. Once this is done, it’s easy enough to delete the remaining soft hyphens if desired.

So — and this is another recurring theme in my life that I haven’t yet mastered — I somehow need to be able to determine whether a given character — in this case, the soft hyphen character — occurs at the end of a line or not.

The aim is to have a macro that one runs as part of one’s suite of processes just before a document is gift-wrapped for the world at large.

Any help would be appreciated.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

User avatar
phspaelti
Posts: 941
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Last Character in a Line (Soft Hyphens)

Post by phspaelti » 2018-01-31 02:52:47

Hello adryan,

don't know much about soft-hyphens, but the following code should return an array of text selections for all line-final hyphens:

Code: Select all

$doc = Document.active
$text = $doc.text

$hyphen = '-'
$hyphenSels = $text.find $hyphen, 'a' 

$lineEndHyphens = Array.new
foreach $sel in $hyphenSels
    if $sel.bound == $text.rangeOfLineAtIndex($sel.location).bound
        $lineEndHyphens.push $sel
    end
end
Obviously you will have to add code to actually do something with those selections. If you are working with soft-hyphens you should be able to get the same to work by replacing the $hyphen assignment. It might be best to work with character codes, so something like:

Code: Select all

$hyphen = Text.newWithCharacter XXX
philip

User avatar
phspaelti
Posts: 941
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Last Character in a Line (Soft Hyphens)

Post by phspaelti » 2018-01-31 02:57:13

Come to think of it, I think your full macro would look something like this:

Code: Select all

$doc = Document.active
$text = $doc.text

$hyphen = '-'
$softHyphen = Text.newWithCharacter XXX # replace with actual code
$hyphenSels = $text.find $softHyphen, 'a' 

foreach $sel in $hyphenSels
    if $sel.bound == $text.rangeOfLineAtIndex($sel.location).bound
        $text.replaceInRange $sel.range, $hyphen
    end
end
philip

adryan
Posts: 198
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Last Character in a Line (Soft Hyphens)

Post by adryan » 2018-01-31 14:23:04

G’day, Philip et al

That’s really great, Philip! Thanks for that. I think I now understand the workings of the rangeOfLineAtIndex command better.

Your second macro does the trick with 173 as the code for a soft hyphen. (I got this code from the Entities Palette of BBEdit.)

There is, however, one teensy-weensy problem; viz, the end-of-line hyphens inserted by the macro do not assume the attributes of the surrounding characters. (This seems to be a consequence of the soft hyphen’s failing to do so in the first place, which might be considered a bug in Nisus Writer.) I can easily fix this with a simple PowerFind Pro operation, but I assume there is some way it could be handled by the macro language.

When the macro is finalized, I suggest naming it “EOL Soft Hyphens to Hyphens”, unless you prefer something else.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

User avatar
phspaelti
Posts: 941
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Last Character in a Line (Soft Hyphens)

Post by phspaelti » 2018-01-31 17:33:43

adryan wrote:There is, however, one teensy-weensy problem; viz, the end-of-line hyphens inserted by the macro do not assume the attributes of the surrounding characters. (This seems to be a consequence of the soft hyphen’s failing to do so in the first place, which might be considered a bug in Nisus Writer.)
Really?
I would try the following first. Change the assignment in the macro to

Code: Select all

$hyphen = Cast to String '-'
adryan wrote: I suggest naming it “EOL Soft Hyphens to Hyphens”, unless you prefer something else.
Feel free to consider it your macro.
philip

adryan
Posts: 198
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Last Character in a Line (Soft Hyphens)

Post by adryan » 2018-01-31 20:31:17

G'day, Philip et al

And another big thank-you, Philip. It all now works as advertised. A nice addition to the macro collection.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Sierra 10.12.6
Nisus Writer user since 1996

Post Reply