Searching backward from a selection
Posted: 2017-05-21 15:32:59
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).
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).