Confused by differences between AppleScripted Replace and manual BBEdit Replace All

34 views
Skip to first unread message

Sonic Purity

unread,
Oct 8, 2021, 12:35:48 AM10/8/21
to BBEdit Talk
Initial conditions:
BBEdit 11.6.8
macOS 10.12.6 Sierra
AppleScript 2.5 with Script Editor 2.9

As part of my overall project, i have a small temporary text file where i’ve collected my story chapter titles. At this stage of things, this file is partly processed, related to earlier steps—which all work great. This is the step where i finish processing it to syntactically-correct HTML, in preparation for selecting all of it and copy/pasting into (an ordered list in) another document.

A sample file is attached, for anyone who may wish to test/help. It’s short enough that i can also paste it here:

1Quite An Introduction</a></li>
2Moving the Plot Forward</a></li>
3Things Get Messy</a></li>
4Life Is Better</a></li>
5Adulting Sucks!</a></li>
6Aging Is Worse</a></li>
7Let’s Not Talk About Late Life</a></li>
8Epilog</a></li>

My traditional workflow has the following Grep Find/Replace:
Grep Find, wrap around:
(\d\d?)(.+?)</a>

Replace:
<li><a href="ExampleStoryC\1.html" title="Example Story chapter \1: \2">\2</a>

The end result should look like this (other than without the empty lines between each <li> line):

<li><a href="ExampleStoryC1.html" title="Example Story chapter 1: Quite An Introduction">Quite An Introduction</a></li>

<li><a href="ExampleStoryC2.html" title="Example Story chapter 2: Moving the Plot Forward">Moving the Plot Forward</a></li>

<li><a href="ExampleStoryC3.html" title="Example Story chapter 3: Things Get Messy">Things Get Messy</a></li>

<li><a href="ExampleStoryC4.html" title="Example Story chapter 4: Life Is Better">Life Is Better</a></li>

<li><a href="ExampleStoryC5.html" title="Example Story chapter 5: Adulting Sucks!">Adulting Sucks!</a></li>

<li><a href="ExampleStoryC6.html" title="Example Story chapter 6: Aging Is Worse">Aging Is Worse</a></li>

<li><a href="ExampleStoryC7.html" title="Example Story chapter 7: Let’s Not Talk About Late Life">Let’s Not Talk About Late Life</a></li>

<li><a href="ExampleStoryC8.html" title="Example Story chapter 8: Epilog">Epilog</a></li>


This works perfectly, done manually. I’m trying to script the same thing. Here is my test script:

set StoryTitle to "Example Story"

set ConcatStoryTitle to "ExampleStory"

-- In reality, ^those titles^ are set and used much earlier in the much longer overall script


tell application "BBEdit"

tell text document 1

replace "(\\d\\d?)(.+?)</a>" using "<li><a href=\"" & ConcatStoryTitle & "C" & (grep substitution of ("\\1" as text)) & ".html\" " & "title=\"" & StoryTitle & " chapter " & (grep substitution of ("\\1" as text)) & ": " & (grep substitution of ("\\2" as text)) & "\">" & (grep substitution of ("\\2" as text)) & "</a>" options {search mode:grep, wrap around:true}

end tell

end tell


When i run the script, the Events look like this:

tell application "BBEdit"

grep substitution of "\\1"

Result:

error "BBEdit got an error: The replacement string couldn’t be computed, because the previous Grep search did not succeed (application error code: 12003)" number 12003


I haven’t been able to figure out what i’m doing wrong. Looking forward to learning something else new about scripting BBEdit.

Example Story Chapter Titles.txt

Sonic Purity

unread,
Oct 8, 2021, 12:40:37 AM10/8/21
to BBEdit Talk
Forgot to mention: if the following block is inserted in place of the replace, it works:

find "(\\d\\d?)(.+?)</a>" options {search mode:grep, wrap around:true} with selecting match

set chapnumtest to grep substitution of ("\\1" as text)

set chaptitletest to grep substitution of ("\\2" as text)

log chapnumtest

log chaptitletest


So this tells me that my Grep string is valid (which i know from it working with the manual not scripted Replace All), making it all the more puzzling to me why it fails in a scripted replace (all).

jj

unread,
Oct 8, 2021, 1:38:48 AM10/8/21
to BBEdit Talk
Sonic,

You don't need to use grep substitutions in replacement expressions.

set StoryTitle to "Example Story"
set ConcatStoryTitle to "ExampleStory"
tell application "BBEdit"
    tell text document 1
        set vRegex to "(\\d\\d?)(.+?)</a>"
        set vReplacement to "<li><a href=\"" & ConcatStoryTitle & "\\1.html\" title=\"" & StoryTitle & " chapter \\1: \\2\">\\2</a>"
        replace vRegex using vReplacement searching in it options {search mode:grep, wrap around:true}
    end tell
end tell

HTH

Jean Jourdain

Sonic Purity

unread,
Oct 8, 2021, 4:46:02 PM10/8/21
to BBEdit Talk
Perfect! Thank you, Jean.

Literally learning something new (about AppleScript) every day,

))Sonic((

Reply all
Reply to author
Forward
0 new messages