Applescript to run Grep patterns

87 views
Skip to first unread message

Alberto Gutiérrez

unread,
Sep 23, 2024, 8:22:16 AM9/23/24
to BBEdit Talk
Hello, I'm trying to write a simple script to run sequentially grep patterns saved in BBEdit. I asked Claude to do so and i got this code:

tell application "BBEdit"
    set theDoc to front document
   
    set grepPatterns to {"PATTERN1", "PATTERN2", "PATTERN3"}
   
    repeat with currentPattern in grepPatterns

        set foundRange to find currentPattern searching in text of theDoc options {search mode:grep, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
       
        if foundRange is not "Not found" then
            select foundRange

            delay 1
        end if
    end repeat
end tell

It gives me this error:

Captura de Pantalla 2024-09-23 a las 9.19.56.png

Can anyone point me in the right direction? Thanks in advance.

Rich Siegel

unread,
Sep 23, 2024, 10:14:36 AM9/23/24
to BBEdit Talk
On 23 Sep 2024, at 4:32, Alberto Gutiérrez wrote:

> Hello, I'm trying to write a simple script to run sequentially grep
> patterns saved in BBEdit. I asked Claude to do so and i got this code:

Are you desiring to perform multiple Grep replacements in a sequence? If so, the "replace" verb is what you need, rather than "find". The script editor's "Record" feature might be helpful here.

However, for sequential "Replace All" operations, a Text Factory is probably the appropriate tool to use. (Or Canonize.) More about both of these is available in the user manual (via the Help menu).

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.

Alberto Gutiérrez

unread,
Sep 23, 2024, 1:53:41 PM9/23/24
to BBEdit Talk
Thanks a lot for your help, Rich!. I managed to record a script with all the grep searches i need. I Am working on ePub files like this:

Captura de Pantalla 2024-09-23 a las 19.35.30.png

I can target individual files of the ePub with replace lines like this:

replace ">([IVXivx]+)" using ">\\U\\1" searching in text 1 of text document "02_INDICE.xhtml" options {search mode:grep, starting at top:true}


How can I extend the search to all of the ePub files?


Thanks!

Reply all
Reply to author
Forward
0 new messages