This is actually a 2 part question. Before getting to the question to which the Subject line refers, i have a Big Picture question about whether perhaps my entire approach is off base.
*Big Picture*
I have an HTML fragment file, consisting of all the chapter titles of my
current story (being converted from TextEdit RTF to HTML). Most of the
time, the chapter titles are plain text. Once in awhile, they contain
markup, usually <em> open and close tags, but could be all kinds of
possible basic HTML markup.
I’ve made up a demo file, attached. I’ll paste in 2 example lines here, to
explain what i’m trying to do.
******* File excerpt, Before *********
[…]
<li><a href="MySwellTestStoryC3.html" title="My Swell Test Story chapter 3:
<em>Markup In The Chapter Title!?</em>">#BARRIER#<em>Markup In The Chapter
Title!?</em></a></li>
[…]
<li><a href="MySwellTestStoryC9.html" title="My Swell Test Story chapter 9:
<strong>We Could <em>Really</em> Go <span
id="nutz">Wild!</span></strong>">#BARRIER#<strong>We Could <em>Really</em>
Go <span id="nutz">Wild!</span></strong></a></li>
*******************************
I want to preserve whatever markup exists as it is, to the right of my
delimiter #BARRIER#. To the left, in the actual title="", each </?em>
should be replaced with * and each </?strong> with **, and any other tags
should be removed.
******* File excerpt, After *********
[…]
<li><a href="MySwellTestStoryC3.html" title="My Swell Test Story chapter 3:
*Markup In The Chapter Title!?*">#BARRIER#<em>Markup In The Chapter
Title!?</em></a></li>
[…]
<li><a href="MySwellTestStoryC9.html" title="My Swell Test Story chapter 9:
**We Could *Really* Go Wild!**">#BARRIER#<strong>We Could <em>Really</em>
Go <span id="nutz">Wild!</span></strong></a></li>
*******************************
I tried to form some basic *replace* statements that would take care of
this all at once, but couldn’t get anything to work. So i set up a loop,
for *find* etc. to iterate through one line at a time.
*-1728 error issue*
My test script is attached as test #2 script, and pasted here:
*******************************
*set* currchap *to* 1 -- Number of the current chapter in the loop
*tell* *application* "BBEdit"
*tell* *text document* 1
*set* totalchaps *to* *count* *of* *lines*
*select* *insertion point* *before* *line* 1
*repeat* *with* currchap *from* 1 *to* totalchaps
*find* "(?<=title=\")(.+)(?=\">#BARRIER#)" options {search mode:*grep*}
*with* selecting match
*set* curr_chapter_title *to* *grep substitution* of "\\1" *as* *text*
*log* curr_chapter_title --for testing only
*if* curr_chapter_title *contains* "</" *then* --there’s markup in the
chapter title, and we need to process it for the title tags
-- Most likely case is <em>. There can be <em> and <strong> both, so we
handle them separately:
*if* curr_chapter_title *contains* "<em>" *then*
*replace* "</?em>" using "*" searchingString curr_chapter_title options
{search mode:*grep*}
*set* curr_chapter_title *to* result
*log* "In the <em> branch" --for testing
*log* curr_chapter_title --for testing only
*end* *if*
-- Note that <strong> is an extremely unlikely use case, since at least for
HTML <h2> is represented as Bold by default.
*if* curr_chapter_title *contains* "<strong>" *then*
*replace* "</?strong>" using "**" searchingString curr_chapter_title
options {search mode:*grep*}
*set* curr_chapter_title *to* result
*log* "In the <strong> branch" --for testing
*log* curr_chapter_title --for testing only
*end* *if*
-- Eliminate any remaining tags. There isn’t going to be an <hr> or <br> or
similar, so if there is anything else, there will be a closing tag:
*if* curr_chapter_title *contains* "</" *then*
*replace* "</?(.+)>" using "" searchingString curr_chapter_title options
{search mode:*grep*}
*set* curr_chapter_title *to* result
*log* "In the <[anything]> branch" --for testing
*log* curr_chapter_title --for testing only
*end* *if*
-- At this point the chapter title has been changed. Now we replace it.
*replace* selection using curr_chapter_title
*end* *if*
*log* curr_chapter_title --for testing only
*end* *repeat*
*end* *tell*
*end* *tell*
*******************************
This script seems to work all the way up to the last *replace* command:
*replace* selection using curr_chapter_title
Nothing gets replaced and it errors out.
The ending part of the Replies in Script Editor looks like this:
*replace* selection *of* *text document* 1 using "My Swell Test Story
chapter 3: *Markup In The Chapter Title!?*"
--> *error* "An attempt was made to resolve an Apple Event reference to a
non-existent object (MacOS Error code: -1728)" number -1728
*Result:*
*error* "BBEdit got an error: An attempt was made to resolve an Apple Event
reference to a non-existent object (MacOS Error code: -1728)" number -1728
from selection *of* *text document* 1
**********
I’ve visually verified that the proper text is selected in the frontmost
text document (My Swell Test Story Chapter Titles.txt). Apparently i’ve
made one or more errors with that *replace* command, but after over an hour
of research, i have no idea what i’m doing wrong.
Looking forward to further learning thanks to your kind answers,
))Sonic((
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" 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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/82e8d953-9381-4c52-a4e2-214e151a394an%40googlegroups.com.
test #2 script.scpt
Description: Binary data
<li><a href="MySwellTestStoryC1.html" title="My Swell Test Story chapter 1: My House with 100 Rooms">#BARRIER#My House with 100 Rooms</a></li> <li><a href="MySwellTestStoryC2.html" title="My Swell Test Story chapter 2: Will It Blow Up?">#BARRIER#Will It Blow Up?</a></li> <li><a href="MySwellTestStoryC3.html" title="My Swell Test Story chapter 3: <em>Markup In The Chapter Title!?</em>">#BARRIER#<em>Markup In The Chapter Title!?</em></a></li> <li><a href="MySwellTestStoryC4.html" title="My Swell Test Story chapter 4: <em>Now</em> Who’s Being Unreasonable?">#BARRIER#<em>Now</em> Who’s Being Unreasonable?</a></li> <li><a href="MySwellTestStoryC5.html" title="My Swell Test Story chapter 5: I Feel <strong>Strongly</strong> About This!">#BARRIER#I Feel <strong>Strongly</strong> About This!</a></li> <li><a href="MySwellTestStoryC6.html" title="My Swell Test Story chapter 6: But What If We <strong><em>Combine</em> Emphasis & Strong Emphasis?!</strong>">#BARRIER#But What If We <strong><em>Combine</em> Emphasis & Strong Emphasis?!</strong></a></li> <li><a href="MySwellTestStoryC7.html" title="My Swell Test Story chapter 7: Go <a href="#">Here!</a>">#BARRIER#Go <a href="#">Here!</a></a></li> <li><a href="MySwellTestStoryC8.html" title="My Swell Test Story chapter 8: <em>Then <span style="color: purple">Add Some Color</span></em>">#BARRIER#<em>Then <span style="color: purple">Add Some Color</span></em></a></li> <li><a href="MySwellTestStoryC9.html" title="My Swell Test Story chapter 9: <strong>We Could <em>Really</em> Go <span id="nutz">Wild!</span></strong>">#BARRIER#<strong>We Could <em>Really</em> Go <span id="nutz">Wild!</span></strong></a></li>
