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
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((
...i have a Big Picture question about whether perhaps my entire approach is off base.
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.
replace selection of window of it using curr_chapter_title
which works very well.