BBEdit find from Applescript

379 views
Skip to first unread message

Whiterock

unread,
Sep 29, 2008, 11:41:51 PM9/29/08
to BBEdit Talk
I have been having a great deal (weeks) of trouble with the find
command. I can find one instance of a pattern in a text file
containing multiple instances, but I must use the option "starting at
top". This won't work inside a loop so I had to decipher the structure
of the "found matches" record of the "Search Match" record of "find"
using error messages in XCode.
set aMatch to find thePattern searching in fileRef options {search
mode:grep, starting at top:true, returning results:true} without
selecting match
set theMatches to found matches of aMatch
repeat with i from 1 to (count of theMatches)
set aString to (message of record i of theMatches) as Unicode text
-- work with a list
end repeat

This doesn't quite work either: The returned strings do contain the
pattern but they also contain text on either side of the pattern. The
pattern works in a simple search of the file using BBEdit directly but
not in a search using BBEdit 9 in Applescript.

pattern: "ged2html/d\\d{4}/g\\d{7}.html#I\\d+" used in Applescript
application
(\r and \t represent <return> and <tab> characters)
sample text: ' <li>\r\t<a href="../ged2html/d0000/
g0000083.html#I1267">Chauvin Isabelle Marie</a>\r\t</li>'
expected results: 'ged2html/d0000/g0000083.html#I1267'
actual results: '\t<a href="../ged2html/d0000/
g0000083.html#I1267">Chauvin Isabelle Marie</a>'

Can anyone explain this? What about a workaround?

Jim Correia

unread,
Sep 30, 2008, 11:22:13 AM9/30/08
to bbe...@googlegroups.com
On Sep 29, 2008, at 11:41 PM, Whiterock wrote:

> I have been having a great deal (weeks) of trouble with the find
> command. I can find one instance of a pattern in a text file
> containing multiple instances, but I must use the option "starting at
> top". This won't work inside a loop

To deterministically iterate over all the matches in a document via
script, you must either use the batch find technique, *or* make sure
you start from the top on the first loop iteration. This is the
technique I suggested last time you asked this question. Let me give
you an explicit example:

tell application "BBEdit"
tell text window 1
set zStartAtTop to true
repeat while true
set zFindResult to find "co.*eia" options {search mode:grep,
starting at top:zStartAtTop} with selecting match
set zStartAtTop to false
if found of zFindResult then
set selection to "NEW_TEXT"
else
exit repeat
end if
end repeat
end tell
end tell


> so I had to decipher the structure of the "found matches" record of
> the "Search Match" record of "find"
> using error messages in XCode.

[...]

> This doesn't quite work either: The returned strings do contain the
> pattern but they also contain text on either side of the pattern.

If you use the batch method for find, in the returned record will be a
list of "Result Entry". You can look at the definition of this in the
scripting dictionary.

The "message" is the message text that would be displayed if we
displayed the match in a search results browser.

There is no field of the record which specifies the matched text. But
the record does tell you the file and offsets of the match (the values
for "start_offset" and "end_offset") so you can extract the text
directly as necessary.

Here's an abbreviated example:

tell application "BBEdit"
set zFindResult to find "co.*eia" options {search mode:grep,
returning results:true} searching in {text window 1}
if found of zFindResult and length of found matches of zFindResult >
0 then
set zEntry to item 1 of found matches of zFindResult
-- assume text window 1 since we know we search it above
set zString to characters (start_offset of zEntry) thru (end_offset
of zEntry) of text window 1 as text
display alert zString
end if
end tell

Jim

Michael Richards

unread,
Sep 30, 2008, 2:27:22 PM9/30/08
to bbe...@googlegroups.com
Thanks much
I should have thought of using a variable for "starting at top". Now that section works

After using "returning results: true" on my reference file to get a complete list of matches, I had to use multiple searches for single matches in other files. The only inconsistency was that I needed to specifically set "returning results" to false. A new use of "find" in the same script does not revert to the default value. I don't know if this is new but it doesn't fit.


2008/9/30 Jim Correia <cor...@barebones.com>
Reply all
Reply to author
Forward
0 new messages