Batch search-and-replace

255 views
Skip to first unread message

Andrew Brown

unread,
Sep 9, 2018, 2:29:54 PM9/9/18
to BBEdit Talk
My BBEdit scripts went down with some earlier computer and I am looking for a search-and-replace for grep and non-grep on multiple folders or files.

I also need to convert HTML to text in batch mode, with the options that BBEdit offers for single file conversions.

Any help much appreciated.

AB

Rich Siegel

unread,
Sep 9, 2018, 2:32:09 PM9/9/18
to bbe...@googlegroups.com
On 9/8/18 at 2:02 AM, li...@c18.org (Andrew Brown) wrote:

> My BBEdit scripts went down with some earlier computer and I am looking for
> a search-and-replace for grep and non-grep on multiple folders or files.
>
> I also need to convert HTML to text in batch mode, with the options that
> BBEdit offers for single file conversions.

File => New => Text Factory should get you started.

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

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

Christopher Stone

unread,
Sep 9, 2018, 3:25:34 PM9/9/18
to BBEdit-Talk
On 09/09/2018, at 01:02, Andrew Brown <li...@c18.org> wrote:
My BBEdit scripts went down with some earlier computer and I am looking for a search-and-replace for grep and non-grep on multiple folders or files.


Hey Andrew,

That covers a lot of ground.

Disparate files and folders?

Files and folders in the same tree?

Here's one example:

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/10/29 07:10
# dMod: 2018/09/09 14:04
# Appl: BBEdit
# Task: Find and Replace in Files On-Disk.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Find, @Replace, @Files, @Disk
----------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
----------------------------------------------------------------

# The Search Folder reference can be either an alias or a posix path or a posix file.

set searchFolderPath to "~/test_directory/Find_and_Replace_It_Test/"

if searchFolderPath starts with "~" then
    set searchFolderPath to ((current application's NSString's stringWithString:searchFolderPath)'s stringByExpandingTildeInPath) as text
end if

----------------------------------------------------------------

set findStr to "good men"
set replStr to "I want my MTV!"

----------------------------------------------------------------
tell application "BBEdit"
    replace findStr using replStr searching in searchFolderPath ¬
        options {search mode:grep, case sensitive:false, showing results:false} ¬
        saving true ¬
        recursion true ¬
        with text files only
end tell
----------------------------------------------------------------

I also need to convert HTML to text in batch mode, with the options that BBEdit offers for single file conversions.

If you want to do this in batch mode then a Text Factory is probably the way to go.

--
Best Regards,
Chris

Andrew Brown

unread,
Sep 10, 2018, 9:17:05 AM9/10/18
to bbe...@googlegroups.com
Thanks, Rich and Christopher. It has now come back to me that the process I used most was in two parts, 1) a short script invoking 2) separate lists of the numerous changes involved, each list easily generated from a database and each change occupying one line. TextFactory needs around fifteen XML lines per change, anathema to my old bones.

I have found one ancient script where the list is actually in the script :

tell application "BBEdit 4.0"
activate
copy (choose file) to sourceFileName
open sourceFileName
--  ••• Replace characters
replace Every Occurrence searching for "Ü" using "<" case sensitive yes start at top yes
replace Every Occurrence searching for "†" using ">" case sensitive yes start at top yes
replace Every Occurrence searching for "Ì" using "<cm>" case sensitive yes start at top yes
replace Every Occurrence searching for "≈" using "â" case sensitive yes start at top yes
replace Every Occurrence searching for "Å" using " " case sensitive yes start at top yes
replace Every Occurrence searching for "" using "ï" case sensitive yes start at top yes

Will this sort of thing still work ? Saving this script caused Script Editor to hang, so I cannot test it. 

AB

Rich Siegel

unread,
Sep 10, 2018, 9:19:54 AM9/10/18
to bbe...@googlegroups.com
On 9/10/18 at 2:00 AM, li...@c18.org (Andrew Brown) wrote:

>Thanks, Rich and Christopher. It has now come back to me that the
>process I used most was in two parts, 1) a short script invoking 2)
>separate lists of the numerous changes involved, each list easily
>generated from a database and each change occupying one line.

This would be a perfect application of the Canonize tool.

Write the canon file using your script, and then apply it to
whatever folder you need, using a text factory with a single
Canonize action. (The text factory never needs to change.)

Andrew Brown

unread,
Sep 11, 2018, 11:26:13 AM9/11/18
to bbe...@googlegroups.com
> On 10 Sep 2018, at 15:19, Rich Siegel <sie...@barebones.com> wrote:
>
> This would be a perfect application of the Canonize tool.
>
> Write the canon file using your script, and then apply it to whatever folder you need, using a text factory with a single Canonize action. (The text factory never needs to change.)

I see now that we went through this not so long ago, it has been a heavy year…

Have I understood correctly that Canonize will not handle grep ?

Cordially,

A.


Christopher Stone

unread,
Sep 11, 2018, 12:42:24 PM9/11/18
to BBEdit-Talk
On 09/11/2018, at 10:01, Andrew Brown <li...@c18.org> wrote:
Have I understood correctly that Canonize will not handle grep ?


Hey Andrew,

What version of BBEdit are you using?

--
Best Regards,
Chris

Andrew Brown

unread,
Sep 11, 2018, 3:27:50 PM9/11/18
to bbe...@googlegroups.com
Hi Chris. version 12.1.5 (410102, 64-bit). Best — A.

Christopher Stone

unread,
Sep 11, 2018, 6:34:17 PM9/11/18
to BBEdit-Talk
On 09/11/2018, at 12:21, Andrew Brown <li...@c18.org> wrote:
Hi Chris. version 12.1.5 (410102, 64-bit). Best — A.


Hey Andrew,

Then you are quite correct.

Canonize does not support grep.

You might consider making a formal feature request (see the message footer).

--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages