AS BBedit - extending text selection a specific number of characters

Hi,
I’m having trouble selecting and deleting a specific character and the 7 characters after it.
Below is a sample of the file I’m trying to edit: (There are hundreds of entries but I’ve only included 4) If a product is removed, the corresponding headline, subhead, legal and classes variable must be removed as well. Ex. To remove “OreoCheesecake”, which is item number two, I must also remove items two from the variables (the #663700 columns). This get really tedious when I have to remove item 68, 74, and 119 from a list of 150 items. The only way is to count the variable entries and delete them manually on 54 different xml files. Whew!

So I’m working on a script to do this. I’ve already done the part where it finds the line numbers of the item and the line numbers of the variables. Then it counts over to first character of the variable color it has to select. This is where I’m stuck. I can select character 50 of line 2, 3, and 4. But I need to extend that selection 7 characters up to and including the separator bar (#663700|). If character 50 of line 2 is selected, I set a variable called startMark for the characterOffset of the selection. Then I set another variable called endMark for the startMark + 7. This is characters 64 thru 71. I wrote a line “select (characters startMark thru endMark)” in order to delete them but AS doesn’t understand the “select” message. I just don’t get it. Can someone please shed some light on this?

Thanks in advance

with the xml file open in BBedit 10.5.13…


tell application "BBEdit"
	select character 50 of line 2 of text window 1
--the previous line is hardcoded for testing
	set startMark to characterOffset of selection of text window 1
	set endMark to startMark + 7
	select (characters startMark thru endMark)
	display alert startMark
	display alert endMark
end tell

Sample of xml file:

#FFFFFF|#663700|#FFFFFF|#FFFFFF| #FFFFFF|#663700|#FFFFFF|#FFFFFF| #FFFFFF|#000000|#FFFFFF|#FFFFFF| Iced Green Tea, Orig. Iced Tea and Iced Sweet Tea Oreo Cheesecake Southwest Supreme Bagel Spicy Omelet Breakfast Sandwich and Wake-up Wrap

Model: Macbook Pro 2011
AppleScript: 2.4
Browser: Firefox 36.0
Operating System: Mac OS X (10.8)

Hello.

You’ll have to use the select command inside a tell text window 1 block, in order to use that command in BBEdit or TextWrangler.

One more thought, wouldn’t it be better to script it as one or two searches, during the first search, you select, and then during the second search you select the rest, by extending the selection?

-If you manage to do one search, then you can select the grep substitution afterwards. The manual of BBEdit for scripting searches, may come in handy.

Here are some snippets:

tell application "BBEdit"
	set theResult to find "\\r(.*)\\rAuthor:.(.*)\\r" searching in text document 1 ¬
		options {search mode:grep, starting at top:true, returning results:true}
	set bookReferences to found object of theResult as list
	set book_title to grep substitution of "\\1"
	set book_author to grep substitution of "\\2"
end tell
-- shamlessly stolen from Crhistopher Stone http://www.betalogue.com/2012/08/22/applescript-scripts-for-paragraph-selection-in-bbedit-continued/
tell application "BBEdit"
	tell text of front text window
		if (characterOffset of selection) > 1 then
			set endMarker to (characterOffset of selection) + (length of selection) - 1
			set fRec to find "[^\\r]+" options {search mode:grep, starting at top:false, wrap around:false, backwards:true, case sensitive:false, match words:false} with extend selection
			set startmarker to characterOffset of fRec's found object
			select (characters startmarker thru endMarker)
		end if
	end tell
end tell

Another great source of examples for BBEDit, is “zencoding for BBEdit”, which you can find at AngelWatt.com. Here you may look into the select next placeholder script. Those scripts may also be great if you edit xml, because I think you can make whole structures by using the dependency operators like > and + for instance of css.

If you enter something like ul>li*6, then you get an ul, with 6 li pairs inside it. :slight_smile:

Finally, here is a little example of my own (after having looked at scripts from AngelWatt.com):

tell application "TextWrangler" to tell front window
	set lineNum to startLine of selection
	set leng to length of line lineNum
	-- Move cursor to start of line
	if leng > 0 then
		select insertion point before (character 1 of line lineNum)
	end if
	-- Find leading whitespace
	set theResult to find "(^[\\s]*)" options {search mode:grep} searching in line (lineNum)
	-- Set text to the white space found
	set white to ""
	if found of theResult then
		set white to found text of theResult
	end if
	-- Insert a return plus the white space
	set selection of it to white & "<ul>" & return & white & tab & "<li></li>" & return & white & "</ul>"
	set newLength to (length of (line ((startLine of selection) + 1))) + 3
	select insertion point after (character newLength of selection)
end tell

Thank you for the quick reply! I posted this question at almost midnight EST and by the time I woke up at 8AM there was a reply.

Your post got me on the right track and I think I have it solved like this:

tell application "BBEdit"
	select character 50 of line 2 of text window 1
	set startMark to characterOffset of selection of text window 1
	set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
	set endMark to characterOffset of targetColor's found object
	select (characters startMark thru endMark) of text window 1
end tell

One thing that was really throwing me off was not typing “of text window 1” after "select (characters startMark thru endMark). I kept getting an error stating “startMark doesn’t understand the select command” grrrr

I’ll post the complete script when I get all finished so maybe someone can learn something from it or at least use pieces of it.

Hello.

I am glad you are on the right track.

I really meant to convey that you should put all your code inside a “tell text window 1 block”, like so:

tell application "BBEdit"
	tell text window 1 
		select character 50 of line 2 of it
		set startMark to characterOffset of selection of it
		set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
		set endMark to characterOffset of targetColor's found object
		select (characters startMark thru endMark) of it
	end tell
end tell
-- you may be able to remove "of it", one at a time, while you check that it still works!

Ah, if it’s in a text window 1 tell block, I won’t need to add “of text window 1” after every command, right? I would just need “of it”.

The script as I wrote it, is almost the same like the one you had, the “of it”, shouldn’t really be necessary, you should be able to remove one of them at a time, and still have a script that compiles well.

It is just a way of transforming stuff, so it doesn’t break along the way. :slight_smile:

set targetColor to find "[|]" searching in text 1 of it options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection

NB: On this line, “of it” is necessary, as long as you have specified the “searching in” clause, but I think you can try to remove the “searching in” clause all together, with “text 1 of it”, and it should still work, but by all means try it out if you like, and just keep the syntax you are most comfortable with.

(Personally, I use the “searching in”, because it all becomes a little bit more clearer to me).

Cool.

Here is the finished script. All you have to do is select all or part of the product you want to deactivate in lines 31 thru 125 in the xml file at the bottom of this post. and run the script. The script will then find the chronological product number by subtracting the line number of the product from the line number of the tag and then count over to the correct color tag entries and the specialClasses tags and delete them.

I think I commented them pretty well. I hope this helps someone out as you’ve helped me out.

I didn’t get rid of the “of window 1” part because they were already there but the AS project I have will include what you’ve taught me.

--Select the product to be deleted first

tell application "BBEdit"
	set myProduct to the selection of text window 1
	set tagLength to 41 + 1
	set colorCodeLength to 8
	set specialClassesLength to 2
	set thumbsLine to "<thumbs>"
	set headlineColors to "headlineColors"
	set subheadlineColors to "subheadlineColors"
	set legalColors to "legalColors"
	set specialClasses to "specialClasses"
	-------------------------------------------------
	--find the line that the thumbs tag is on
	find thumbsLine searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to thumbsLineCount
	-------------------------------------------------
	--find the line that the desired product tag is on
	find myProduct searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to productLineCount
	
	-- and subtract it from the thumbs line
	set productNumber to productLineCount - thumbsLineCount
	set productColor to productNumber - 1
	-----------------------------------------------
	--LINE COUNT 1
	--find the text color tag and get its line number and set var lineCount1
	find headlineColors searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to lineCount1
	
	--select first char of chosen product color
	select character (productColor * colorCodeLength + tagLength) of line lineCount1 of text of window 1
	set startMark to characterOffset of selection of text window 1
	
	-- and find next pipe char and extend selection
	set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
	set endMark to characterOffset of targetColor's found object
	select (characters startMark thru endMark) of text window 1
	set text of selection to ""
	-----------------------------------------------
	--LINE COUNT 2
	--find the text color tag and get its line number and set var lineCount2
	find subheadlineColors searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to lineCount2
	
	--select first char of chosen product color
	select character (productColor * colorCodeLength + tagLength) of line lineCount2 of text of window 1
	set startMark to characterOffset of selection of text window 1
	
	-- and find next pipe char and extend selection
	set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
	set endMark to characterOffset of targetColor's found object
	select (characters startMark thru endMark) of text window 1
	set text of selection to ""
	-----------------------------------------------
	--LINE COUNT 3
	--find the text color tag and get its line number and set var lineCount3
	find legalColors searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to lineCount3
	
	--select first char of chosen product color
	select character (productColor * colorCodeLength + tagLength) of line lineCount3 of text of window 1
	set startMark to characterOffset of selection of text window 1
	
	-- and find next pipe char and extend selection
	set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
	set endMark to characterOffset of targetColor's found object
	select (characters startMark thru endMark) of text window 1
	set text of selection to ""
	-----------------------------------------------
	--LINE COUNT 4
	--find the text color tag and get its line number and set var lineCount4
	find specialClasses searching in text 1 of text window 1 options {search mode:text, starting at top:true, backwards:false, case sensitive:false, match words:false, extend selection:false} with selecting match
	copy the startLine of selection of window 1 to lineCount4
	
	--select first char of chosen product color
	select character (productColor * specialClassesLength + tagLength) of line lineCount4 of text of window 1
	set startMark to characterOffset of selection of text window 1
	
	-- and find next pipe char and extend selection
	set targetColor to find "[|]" searching in text 1 of text window 1 options {search mode:grep, starting at top:false, wrap around:false, backwards:false, case sensitive:true} with extend selection
	set endMark to characterOffset of targetColor's found object
	select (characters startMark thru endMark) of text window 1
	set text of selection to ""
	
end tell

--replace (characters 114 thru 121 of line 14) of text window 1 using ""

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?xml version="1.0" encoding="utf-8"?> Lifestyle_AFrame Description Notes 0.125in 0.5in #FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#663700|#663700|#663700|#FFFFFF|#663700|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#663700|#663700 #FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#663700|#663700|#663700|#FFFFFF|#663700|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#FFFFFF|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#FFFFFF|#663700|#FFFFFF|#FFFFFF|#FFFFFF|#663700|#663700|#663700|#663700|#663700 #FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#000000|#FFFFFF|#FFFFFF|#000000|#000000|#000000|#000000|#000000|#000000|#FFFFFF|#000000|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#000000|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#000000|#000000|#FFFFFF|#FFFFFF|#000000|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#000000|#000000|#FFFFFF|#000000|#000000|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#000000|#FFFFFF|#000000|#FFFFFF|#FFFFFF|#FFFFFF|#000000|#000000|#663700|#000000|#000000 -|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|gradient|-|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|gradient|-|-|-|-|-|- For FREE offers, the font size of the product name must be at least half the font size of the word FREE. Use the return key to move text to the next line. To insert a ¢, type "ALT+0162" (Windows) or "Option+4" (Mac) 130|100|70 54|44|34 Iced Green Tea, Orig. Iced Tea and Iced Sweet Tea Raspberry and Oreo Cheesecake Square Southwest Supreme Bagel Spicy Omelet Breakfast Sandwich and Wake-up Wrap K-cup and Pound of Coffee Snack N Go Chicken Wrap Coffee and Espresso Line Turkey Sausage Flatbread and Wake Up Wrap BR Coffee Flavors Cookie Dough Heart Shaped Donuts Dark Roast K Cup and Pound Of Coffee Dark Roast and Hot Cocoa K Cups White Chocolate Raspberry Lattes with Heart Shaped Donut Tomato Mozzarella Supreme Bagel Angus Steak and Egg Breakfast Sandwich Croissant Donut Dunkin Go Bar Spinach Artichoke Supreme Bagel Cold Beverage Happy Hour Hot Dark Roast Hot and Iced Dark Roast Frozen Coffee Coolatta Swirl Chicken Bacon Croissant Sandwich With Hashbrowns And Iced Tea Grilled Chicken Flatbread With Bakery Line Pretzel Twist with Beverage Iced Coffee Suite Box O' Joe and Dozen Donuts Iced And Hot Coffee Suite Glazed Donut Breakfast Sandwich Iced Green Tea Grilled Chicken Flatbread Sandwich & Iced Tea 3 Hopper Hot Chocolate Suite 5 Hopper Hot Chocolate Suite Bacon Ranch Chicken Sandwich with Iced Tea Espresso Line Hot & Iced Lattes Hot Latte Deluxe Grilled Cheese Sandwich Line & Iced Tea Roast Beef Pretzel Roll Sandwich & Coca-Cola® Iced Tea & Iced Coffee Chicken Salad & Tuna on a Bagel Sandwiches Iced Tea Roast Beef Pretzel Roll Sandwich & Iced Tea Bakery Sandwich Line COOLATTA® Line Hot Coffee, Iced Coffee & Hot Latte Southern Style Biscuit Line Sweet Tea Tuna & Chicken Salad Wraps Peppered Fried Egg & Sausage on Croissant Turkey Sausage Sandwich, Wake-Up® Wrap & Iced Coffee Egg & Cheese Wake-Up® Wrap Four Breakfast Sandwiches Big N' Toasted® & Hot Coffee Big N' Toasted® & Iced Coffee Hot Coffee Pour Chicken Biscuit & Hot Coffee Dozen Donuts Turkey, Bacon & Cheddar and Ham & Cheese Big N' Toasted® & Hash Browns Bagel Twists & Hot Coffee Donut & Hot Coffee Coffee Roll MUNCHKINS® & Hot Coffee Half Dozen Donuts Iced Latte Line Frozen Beverage Line Muffin Varieties K-Cup® & Brewer Bagels Mocha Latte & Hot Coffee Oatmeal Bulk Items Chicken Biscuit & Sweet Tea Bagel Twists & Iced Tea Bagel Twists & Iced Coffee Chicken Salad Sandwich & Iced Tea Tuna on Bagel & Iced Tea Tuna Melt & Iced Coffee Bagel with Cream Cheese & Hot Coffee Pound of Coffee Line Pound of Coffee 2 Pounds of Coffee Bagel & Iced Coffee Brownie & Hot, Iced Coffee Mocha Swirl Iced Coffee & Latte Cookies Hash Browns in Bag Turkey Sausage Breakfast Sandwich Hot Chocolate & DUNKACCINO® Iced Coffee & Two Donuts Iced Coffee & Single Donut Sliced Turkey Breakfast Sandwich Better for You Breakfast Sandwiches lifestyleSuite