scripting a grep to be applied on selected text

47 views
Skip to first unread message

Pablo Nolla

unread,
Jun 23, 2020, 9:35:14 AM6/23/20
to BBEdit Talk
HI!

I'm not familiar with scripting, so please forget me if the question is too basic or out of scope.

I would need to learn how to automate a simple search-replace (eventually with greps) grep that would run only on the selected text of the front BBEdit window.

The idea is to trigger the find-replace with a keyboard shortcut.


I guess it could be done with Applescript, something like: 

tell application "BBEdit"

tell front text window's text

      set _selection to contents of selection


           set newText to "(here the grep "find" and "replace" patterns,   in this case "<p" and "somethingelse <p" )

  set text of selection to newText

 
        end tell
end tell


Could someone give me a hand with this?

Any ideas would be much appreciated.

Thanks for your time.


Pablo


PS: i'm attaching a screen capture of the search itself, not a grep in this case.

 

Captura de pantalla 2020-06-23 a las 12.31.49.png


CCd 

Rich Siegel

unread,
Jun 23, 2020, 9:37:29 AM6/23/20
to bbe...@googlegroups.com
On 6/23/20 at 6:40 AM, pablo...@gmail.com (Pablo Nolla) wrote:

>I'm not familiar with scripting, so please forget me if the
>question is too basic or out of scope.
>
>I would need to learn how to automate a simple search-replace
>(eventually with greps) grep that would run only on the
>selected text of the front BBEdit window.

Are you specifically trying to learn how to script an operation?
Or do you simply want to perform a "Replace All in Selection"
with a keyboard equivalent?

R.
--
Rich Siegel Bare Bones Software, Inc.
<sie...@barebones.com> <https://www.barebones.com/>

Someday I'll look back on all this and laugh... until they
sedate me.

Pablo Nolla

unread,
Jun 23, 2020, 12:05:15 PM6/23/20
to BBEdit Talk
Hi, 
thanks for answering.

I'm trying to script an operation.
Actually, browsing the documentation and looking at other examples I've got to something that does the trick:
 

tell application "BBEdit"

tell selection of front text window

   replace "<p" options {starting at top:true, search mode:grep} using "<p class=\"i-plomo-2\"></p> <p"
end tell

end tell


I then trigger this with a keyboard shortcut, which is what i need.

Now, your question leaves me wondering:  Could one assign a keyboard shorcut to a saved search-replace pattern ?


Best!

Reply all
Reply to author
Forward
0 new messages