AppleScript -1728 error with replace selection

45 views
Skip to first unread message

Sonic Purity

unread,
Oct 24, 2021, 12:55:06 AM10/24/21
to BBEdit Talk
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((

test #2 script.scpt
My Swell Test Story Chapter Titles.txt

jj

unread,
Oct 24, 2021, 2:43:46 AM10/24/21
to BBEdit Talk
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

John Delacour

unread,
Oct 24, 2021, 2:36:31 PM10/24/21
to bbe...@googlegroups.com


On 24 Oct 2021, at 05:55, Sonic Purity <sonic...@gmail.com> wrote:

 ...i have a Big Picture question about whether perhaps my entire approach is off base.

It is.  There are several languages that are good for text editing, and AppleScript is not among them. The conciseness of your desiderata below should be matched in your script.

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.

 
BBEdit’s regular expressions are not much different from Perl’s, on which they are based.  Here is a simple Perl script that will do what you demand:

#!/usr/bin/perl
#Save in ../Text Filters/; run from palette
while (<>) { #read line by line
my ($left, $right) = split "#BARRIER";
$left =~ s~</?em>~\*~g; # s~x~y~g means substitute y for x globally
$left =~ s~</?strong>~\*\*~g;
$right =~ s~<[^>]+?>~~g;
print $left . $right;
}
#END

With your document frontmost, try running the script above from the Text Filters palette after saving it as ***.pl in the proper folder; to open this run this line in Terminal:

cd; open Library/Application\ Support/BBedit/Text*

Have fun !

JD



Sonic Purity

unread,
Oct 26, 2021, 9:29:22 PM10/26/21
to BBEdit Talk
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((
Reply all
Reply to author
Forward
0 new messages