Reply to topic  [ 4 posts ] 
Searching backward from a selection 
Author Message

Joined: 2005-10-05 15:05:51
Posts: 12
Location: Portland, OR
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).


2017-05-21 15:32:59
Profile WWW
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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:
if Find '\\d+\\.', 'Er-W'
    $tag = Read Selection
end


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:
$doc = Document.active
$sel = $doc.selection
if Find '\\d+\\.', 'Er-W'
    $tag = Read Selection
end
$doc.setSelection $sel

_________________
philip


2017-05-21 19:13:59
Profile

Joined: 2005-10-05 15:05:51
Posts: 12
Location: Portland, OR
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...


2017-05-23 15:30:41
Profile WWW

Joined: 2005-10-05 15:05:51
Posts: 12
Location: Portland, OR
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!


2017-05-26 21:01:59
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


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:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software