Thank you JJ for the explanation and especially the short test example for 
reproducing the same sort of error. I did some experimentation with 
different wording and learned more about correctly referring to objects—and 
reading the AppleScript dictionaries more carefully. It’s strange to me 
that windows and the BBEdit app itself have selection properties, but 
documents do not.

In the context of my overall script where i was already in an inner Tell 
loop for the document in question with the selection and an outer one for 
BBEdit, i used:

*replace* selection *of* *window* *of* *it* using curr_chapter_title
which works very well.

I also experimented with JD’s Perl script. If i was starting from scratch, 
or if in the future i encounter a major problem with my (to me) complex 
AppleScript, i’ll want to be taking a serious look at learning Perl. As it 
is, I’ve finally gotten the entire AppleScript working correctly, including 
with some difficult edge cases, so i’m going back to being a fiction story 
author rather than learning more about any form of scripting for the time 
being.

~~ Thanks to All who’ve helped with this project over the past few months! 
~~

))Sonic((
On Saturday, October 23, 2021 at 11:43:46 PM UTC-7 jj wrote:

> Hi Sonic,
>
> If not otherwise specified a property is relative to the current "Tell" 
> object.
> So line 34, selection is relative to the current "Tell" object, in this 
> case text document 1.
> This triggers the error because text documents don't have a selection 
> property. 
> You can reproduce it with:
>     
>     tell application "BBEdit"
>         selection of text document 1
>     end tell
>     
>     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
>
> To access the selection of a text document ask for the selection of its 
> window because windows do have a selection property.
>     
>     tell application "BBEdit"
>         selection of window of text document 1
>     end tell
>     
>     Result:
>         insertion point before character 3158 of text document 1 of 
> application "BBEdit"
>
> HTH
>
> Jean Jourdain
>
> On Sunday, October 24, 2021 at 6:55:06 AM UTC+2 [email protected] wrote:
>
>> 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/144e4082-c1c8-48df-9be8-4891494a8f29n%40googlegroups.com.

Reply via email to