Page 1 of 1

Searching backward from a selection

Posted: 2017-05-21 15:32:59
by allenwatson
First, me: I am fair-to-middling in AppleScript (although not scripting Nisus Writer Pro), and a total neophyte with Nisus macros.

I have a rather complex AppleScript I've been using in BBEdit that I am attempting to replicate in Nisus Writer Pro. I've begun using AppleScript, and have gotten to this point:

I have a selection of text in a book-length document. I want to determine the location (index?) of the start of the selection within the document, and then to search backward from that point for a particular tag I've imbedded in the document, using grep, and resulting in the text contents of that tag. Hopefully without disturbing the selection.

I don't care if I do this within AppleScript, or if I have to use a Nisus macro to do all or part of it (just the location would be enough, I think, although it would be nice if Nisus would perform the search for me as well).

Any suggestions would be most welcome.

The AppleScript code I've been using in BBEDit is this:

Tell application "BBEdit"
set loc to characterOffset of the selection
set firstPara to found text of (find "\\d+\\." searching in (characters 1 thru loc) options {backwards:true, search mode:grep})
find "\\d+\\." searching in selection options {backwards:true, search mode:grep}
end tell

The first find looks backwards from the selection to the first match preceding the selection; the second find looks backward *within* the selection to locate the last match (if any; I test the result later).

Re: Searching backward from a selection

Posted: 2017-05-21 19:13:59
by phspaelti
Hello Allen,

you will definitely want to do this with the Nisus macro language:
  1. This is Nisus' speciality. It's much easier to do this in Nisus, than in AppleScript
  2. To do this in AppleScript you would have to use the "Do Menu Macro with macro" command, which amounts to doing it in Nisus macro language anyhow
Here is the code for doing this in a Nisus macro:

Code: Select all

if Find '\\d+\\.', 'Er-W'
    $tag = Read Selection
Note that with Nisus you set the options for the find statement using a string with letter abbreviations: "E" stands for 'grep search mode', "r" stands for "reverse search direction, and "-W" stands for 'don't wrap around'.
I embedded the search in an 'if' statement, so if it fails it will do nothing. The text of your found expression will be read into the variable $tag, which you could use to do further processing.

The above method is the basic approach. You also say that you want to "not disturb the selection". I take it that you mean you would like to keep the current selection. The above code will not do that. So you have two options. Method one is to save the selection and then restore it. Method two would be to use the object commands.

Using the first method would look like this:

Code: Select all

$doc =
$sel = $doc.selection
if Find '\\d+\\.', 'Er-W'
    $tag = Read Selection
$doc.setSelection $sel

Re: Searching backward from a selection

Posted: 2017-05-23 15:30:41
by allenwatson
Thanks so very much, Philip! I will definitely give this a shot as soon as I can. Your example helps so much to point me in the right direction...

Re: Searching backward from a selection

Posted: 2017-05-26 21:01:59
by allenwatson
That helped tremendously! I'm well on the way to doing what I want. I've already expanded on the macro with two additional searches, including one with a named subtext in the found string...That was a pleasant find in the macro manual, which isn't exactly an easy read!