Grep next with AppleScript

393 views
Skip to first unread message

Macdrifter

unread,
Jan 7, 2012, 11:39:17 PM1/7/12
to BBEdit Talk
I'm trying to figure out how to perform a grep for the next whole word
from the current insertion point. I had assumed I could use the find
function like so:

set results to find "\\b\\S+\\b" searching in lineOfInsertionPoint
options {search mode:grep, showing results:false, returning
results:true} with selecting match

but for a long line, this is very slow (as expected) since there are
so many matches. I really just want to select the next whole word and
stop there. I don't want to extend the selection like the built in opt-
shift-right arrow but rather change the entire selection to just the
next word.

Thanks.

Christopher Stone

unread,
Jan 8, 2012, 11:31:21 AM1/8/12
to bbe...@googlegroups.com
On Jan 07, 2012, at 22:39, Macdrifter wrote:
I'm trying to figure out how to perform a grep for the next whole word from the current insertion point. I had assumed I could use the find function like so:
______________________________________________________________________

Hey There,

I'm not certain how you can limit the search to the current line without it starting over and over again.

Try this, and see if you can live with it.

try


tell application "BBEdit"
tell text of front text document
set results to find "\\b\\S+\\b" options {search mode:grep, showing results:false, returning results:true} with selecting match
end tell
end tell


on error errMsg number errNum
set {cr, sep} to {return, "------------------------------------------"}
set e to sep & cr & "Error: " & errMsg & cr & sep & cr & "Error Number: " & errNum & cr & sep
beep
display dialog e
end try

--
Best Regards,
Chris

Macdrifter

unread,
Jan 8, 2012, 2:43:15 PM1/8/12
to BBEdit Talk
Hi Chris,

Very nice. I am a bit confused though. This seems much faster than
when the "searching in" modifier is used. This also only seems to
return one result where "searching in" returns all matches. I guess I
don't understand why limiting the the search to a specific line
returns all results a more generic find only returns one result. Did
you expect that? Whatever the reason, thanks for the help. This is
quite useful when triggered by a a hot key. It provides a mechanism to
quickly jump selections through the text. If I add a test for the
current selection length, I can also use an if statement to use the
Gruber "select current word" script.

Thanks again Chris. Nice work.

Gabe

On Jan 8, 11:31 am, Christopher Stone <listmeis...@thestoneforge.com>
wrote:

Macdrifter

unread,
Jan 8, 2012, 2:49:04 PM1/8/12
to BBEdit Talk
Here's the modified version that also incorporates John Gruber's Select
+Word script:

try
tell application "BBEdit"
tell text of front text document
set current_offset to characterOffset of selection
set current_line to startDisplayLine of selection
if length of selection = 0 then
select (last word of display_line current_line whose
characterOffset ≤ current_offset)
else
set results to find "\\b\\S+\\b" options {search mode:grep,
showing results:false, returning results:true} with selecting match
end if
end tell
end tell
on error errMsg number errNum
set {cr, sep} to {return,
"------------------------------------------"}
set e to sep & cr & "Error: " & errMsg & cr & sep & cr & "Error
Number: " & errNum & cr & sep
beep
display dialog e
end try

On Jan 8, 11:31 am, Christopher Stone <listmeis...@thestoneforge.com>
wrote:
Reply all
Reply to author
Forward
0 new messages