BBEdit - Strip Outer HTML tags

So, I monitor the BBEdit Google group, now that I’m a paid-up BBEdit user. One question piqued my interest today, and here is my solution:

 1 tell application "BBEdit"
 2 	tell front window
 3 		set cursorPos to characterOffset of selection
 4 		balance tags
 5 		set startPos to characterOffset of selection
 6 		set endPos to startPos + (length of selection)
 7 		select (characters (startPos - 6) thru (endPos + 6))
 8 		set selectedText to selection as text
 9 		if characters 1 thru 6 of selectedText as text is equal to "<span>" then
10 			set replaceText to characters startPos thru (endPos - 1) as text
11 			set selection to replaceText
12 			select insertion point before character (cursorPos - 6)
13 		else
14 			select insertion point before character (cursorPos)
15 		end if
16 	end tell
17 end tell

In summary, it uses the BBEdit builtin command to select the contents of the current tag, and then extends that selection to grab the span tags that surround it. If indeed it was as span block, then it removes those tags.

This is just a simple one-off, but it might be useful as a basis for generating a script that has more features: like arbitrary tag types (rather than just span), or some other thing I haven’t thought of.

Note that it will only strip the outer tags. BBEdit has a Remove Markup feature, but that does not seem to be accessible using AppleScript.