Find & Replace just once vs. all

59 views
Skip to first unread message

Hal Day

unread,
May 27, 2020, 7:58:04 AM5/27/20
to BBEdit Talk
I am learning to use AppleScript for BBEdit to do find/replace editing. I find that replace does "replace all", and have not discovered how one does a single find/replace.

Could someone explain that, and could someone tell me where to get good, detailed information on this topic. I find the BBEdit manual very brief on AppleScripting.

My thanks.

Fletcher Sandbeck

unread,
May 27, 2020, 9:35:39 AM5/27/20
to bbe...@googlegroups.com
You can tailor your regular expression to only find one match. I use [\s\S]* to defeat the tendency of .* to match only within a single line.

replace "([\\s\\S]*?)find string([\\s\\S]*)" using "\\1replace string\\2" searching in text 1 of project window 1 options {search mode:grep, starting at top:true}

Or, you can do a find, capturing it into the current selection, and then do a replace within that selection.

find "find string" searching in text 1 of project window 1 options {starting at top:true} with selecting match
replace ".*" using "replace string" searching in selection of project window 1 options {search mode:grep}

Hope this helps,

[fletcher]
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/b86c8a49-c777-4548-a77c-3c322df45697%40googlegroups.com.

Rich Siegel

unread,
May 27, 2020, 9:44:40 AM5/27/20
to bbe...@googlegroups.com
On 5/26/20 at 12:36 AM, hal...@frontier.com (Hal Day) wrote:

>I am learning to use AppleScript for BBEdit to do find/replace
>editing. I find that replace does "replace all", and have not
>discovered how one does a single find/replace.

In general, BBEdit's AppleScript support is for intended
automating text processing and other non-UI operations, and not
for replicating the UI. (Some UI elements such as window shapes
and ordering can be manipulated using AppleScript, but that's
not its primary purpose in BBEdit.)

The operation of the "Replace" menu command thus might be
modeled as:

1. Search for the desired string/pattern, specifying the text to
be searched and the search options;

2. set the contents of the found range (if any) to a desired string.

The "find" event returns a result, which includes information
about the matched range of text (and whether the search
succeeded). You can then explicitly replace the range of text
with something else, as desired.

You can get a pretty good head start on what BBEdit supports by
choosing "Open Scripting Dictionary" from the Scripts 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.

Hal Day

unread,
May 30, 2020, 8:21:43 AM5/30/20
to BBEdit Talk
Fletcher:

You have given me what I need, especially from your second idea. You have 
answered several puzzles at once, including what that "with selecting 
match" is for.

I didn't realize that "\S" includes line endings.

I had some trouble with ".*" in replace, so used the 'found object' 
property of Search Match, the result of a find operation. I am finally 
starting to know how to use that dictionary thing. Took some learning.


Rich:

Thank you for your help. I have used both the AppleScript Language Guide 
and the dictionaries, but still needed more explanations. I am finally 
getting to the point where I don't get cryptic errors on 9 tries out of 
10. I think I needed to start asking questions at forums sooner, and 
searching out examples of basic operations, such as getting a text document 
ready to edit. Hard to find.

I suspect in hindsight that BBEdit's factories may have been an easier 
approach, but I'm glad to be better AppleScript.

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

My current search & replace script is below in case anyone has feedback.

Thank you both for your help.
Hal

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

set myFile to POSIX file "/Users/Hal/Documents/.../ScriptTarget.2.txt"
set findText to "is"
set replText to "isn't"
tell application "BBEdit"
activate
set myDoc to open myFile
set myWindow to window of myDoc
find findText searching in myDoc with selecting match
replace found object of result using replText searching in selection of myWindow
close myWindow saving yes
end tell -- BBEdit
Reply all
Reply to author
Forward
0 new messages