BBEdit Replace using GREP Help Needed

Hello, I am not anywhere close to an expert Applescript user, so please make your replies gentle. :wink: I Have gotten some help with a script that goes through several documents and extracts key data. Unfortunately this leaves a find replace function undone at the end and this is where I am asking for help. I was given the code for the grep find/replace but I cannot get it into my Applescript.

What I have is a “search results browser” which has all the results of the terminal script opened in a BBEdit search browser. What I don’t understand is how to proceed to get it to a search/replace found set so that I can make use of the search? Applescript has a language that, despite my best efforts continues to elude me. I want to gather the results of the search browser and run the find/replace but can I do that on the search browser or should I open a new window first to do that? Some commands work with some windows and not on other windows.

Looking in the Applescript dictionary, here are some concepts which could be important, if only I could make sense of how?
– copying to new document
– make new text window with properties {contents:search results browser}

tell application “BBEdit”
tell text of front text window
replace “(^.+ )(.+$)” searching in ¬
document 1 ÂŹ
options {search mode:grep} ÂŹ
using (grep substitution of “\2”)
end tell
end tell

The grep shown in the above test is the correct grep I want to use but the example doesn’t work.

– tell selection of text window 1
– set foundStr to find “(^.+ )(.+$)” options {search mode:grep}
– set text of (found object of foundStr) to (grep substitution of “\2”)

And finally, with my best logic, this should work, so obviously I am not understanding Applescript.

tell application “BBEdit”
make new text window with properties {contents:search results browser}

This yields a new window with just a few characters but no search results.

Any help out there?

Much thanks

David

Using BBEdit version 9.x, Mavericks

Hello.

I think the search browser, is just a gui, which you can’t script. However, it is possible to script multifile searches from applescript. see here!.

Hi David.

This perhaps:

tell application "BBEdit"
	tell text of front text window
		replace "^(.+    )(.+)$" options {starting at top:true, search mode:grep} using "\\2"
	end tell
end tell

Almost, changed the search and … GREAT EXAMPLE, thanks much.

This is working;

tell application “BBEdit”
tell text of front text window
replace “(^.+ )(.+$)” options {starting at top:true, search mode:grep} using “\2”

	set matchOptions to {match mode:leaving_one}
	set outputOptions to {duplicates to new document:true, deleting duplicates:true}
	
end tell

process duplicate lines (text 1 of window 1) ÂŹ
	duplicates options matchOptions ÂŹ
	output options outputOptions

end tell

Last question, how do I sort results, and then copy results to clipboard?

Trying variations of the following dictionary items:

– set clipboard to sort lines
– copying results to clipboard

– The purpose of all this is to run through all my TaskPaper documents, pulling out important tasks and have them on my clipboard so I can paste then into a saved text file which I show tasks with, on my desktop, via NerdTool.

Ongoing thanks.

Including the original ‘replace’:

tell application "BBEdit"
	tell text of front text window
		replace "(^.+    )(.+$)" options {starting at top:true, search mode:grep} using "\\2"
		process duplicate lines (sort lines it) output options {unique lines to clipboard:true}
	end tell
end tell

This leaves the ‘replace’ result in the text window, but the sorted unique items on the clipboard ” which seems a bit untidy, but I don’t see a way round that straight away.

Thank you. I Know I said last question, guess I didn’t mean that.

I borrowed the duplicate script from BBEdits Applescripts folder, the one for killing duplicates of course.
Oddly, it does not do a very good job of finding duplicates and when I run BBEtis de-duplication command from the program menu, not Applescript, it finishes the job and kills the remaining duplicates. I am wondering how I can make this more efficient so that killing duplicates actually does, kill ALL duplicates?

OK, I just found an example of Using the Terminal busy to better delay the Applescript part of this scriipt. All I need now is to clean up those extra dups. Yay!

tell application “Terminal”
activate
do script myvar in front window
set a to 0
repeat until (a = 1)
if (front window is not busy) then
close front window
set a to 1
end if
end repeat
end tell

Thanks.

Hello.

I have some trouble understanding what you mean, but try to run this, after having run Nigel’s, maybe that is what you are after. It removes everything from the document, by pasting the unique lines back, which Nigel put on the clipboard.


tell application "BBEdit"
	tell text of front text window
		set contents of it to the clipboard
	end tell
end tell

Thanks, here is a working example, finally:

tell application “Terminal”
activate
do script myvar in front window
delay 3
set a to 0
repeat until (a = 1)
if (front window is not busy) then
quit
set a to 1
end if
end repeat
end tell

tell application “BBEdit”
tell text of front text window
replace “(^.+ )(.+$)” options {starting at top:true, search mode:grep} using “\2”
end tell
process duplicate lines text 1 of text document 1 duplicates options {match mode:leaving_one} output options {deleting duplicates:true, unique lines to clipboard:true}
sort lines text 1 of text document 1 output options {replacing target:true}
end tell

I was having trouble because the tell text of front window worked for the replace line, but did not work for the other lines (process and sort). Those lines needed to be removed to the tell level for BBEdit.

The grep search and replace was done for me, I just had to put it in. How can I make a hopefully simpler search and replace as some of the lines begin with "- ". That would be a search for return character, space, dash, space: replaced with a return character.

Much thanks

David

Hello, I’m all done for at this moment, so I’ll just enter the patterns for you.

Personally, I’d repeat the search and replace operations for this pattern, but there is of course nothing stopping you from digging deep into the manual, and read up on how to write conditional perl-like patterns. :slight_smile:

Try this for searching:

, and this for replace

Thank you. I am afraid I have some very real learning disabilities which already makes this deep water for me. I have tried grep tutorials and I have been using the Applescript dictionary, doing my best to make sense of the Applescript language. This is just the speed I travel in. Not being lazy, just doing my best. I do appreciate the help though. :slight_smile:

I will try your example next. This is as far as I was able to go recording actions, etc. It wont do the final cleanup though.I would think that if I recorded the script and tested it out I should get it to work. Again, Applescript seems to have it’s own way and, while I very much appreciate that I now have a solution, it remains an enigma to me much of the time.

replace " - " using “¬” searching in text of front text window options {search mode:grep, starting at top:true}

Thanks very much

David

ADDED: Complete success, thanks all for all the help.

tell application "BBEdit"
	tell text of front text window
		replace "(^.+	)(.+$)" options {starting at top:true, search mode:grep} using "\\2"
	end tell
	add line breaks text of front text window
	replace "- " using "" searching in text of front text window options {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
	sort lines text of front text window output options {replacing target:true}
	-- hard wrap text of front text window limit window width indentation none with relative without paragraph fill
	process duplicate lines text of front text window duplicates options {match mode:leaving_one} output options {deleting duplicates:true}
	select text of front text window
	copy selection
end tell

:cool:

Hi levelbest.

I’m not surprised you were puzzled!

The script compiler understands from BBEdit’s scripting dictionary that these two forms mean exactly the same thing:


-- Form 1.
tell application "BBEdit"
	replace "(^.+    )(.+$)" searching in text of front text window options {starting at top:true, search mode:grep} using "\\2"
end tell

-- Form 2.
tell application "BBEdit"
	tell text of front text window
		replace "(^.+    )(.+$)" options {starting at top:true, search mode:grep} using "\\2"
	end tell
end tell

It’s fairly unusual for an enclosing ‘tell’ statement to be understood as the default target of an optional labelled parameter (as it is here with ‘searching in’). More often, it’s the default for the direct parameter (the one immediately following the command word). However, the direct parameter here is literal text, which wouldn’t understand the BBEdit command.

With the ‘sort lines’ command, an enclosing ‘tell’ statement is understood as the default target for the direct parameter:


-- Form 1.
tell application "BBEdit"
	sort lines text of front text window
end tell

-- Form 2.
tell application "BBEdit"
	tell text of front text window
		sort lines
	end tell
end tell

HOWEVER, in the ‘tell text of front text window’ version, if the ‘output options’ parameter immediately follows ‘sort lines’, the compiler sees “sort lines output options” as the BBEdit class ‘Sort Lines Output Options’ and compiles that rather than the ‘sort lines’ command followed by the ‘output options’ parameter. What you can do here is to use the word ‘it’, which refers to the object of the ‘tell’ statement, to separate ‘sort lines’ from ‘output options’. The compiler then sees what’s meant:


tell application "BBEdit"
	tell text of front text window
		sort lines it output options {replacing target:true}
	end tell
end tell

You don’t need grep to do what you describe literally:


tell application "BBEdit"
	tell text of front text window
		replace "\\r - " options {starting at top:true} using "\\r"
	end tell
end tell

But this wouldn’t lose a space-dash-space sequence at the beginning of the first line, since there’s no return there. What you could do instead is to use a grep search to find space-dash-space at the beginnings of lines and simply delete any found:


tell application "BBEdit"
	tell text of front text window
		replace "^ - " options {starting at top:true, search mode:grep} using ""
	end tell
end tell

In BBEdit itself, by the way, the line (ie. paragraph) delimiter is the ‘return’ character. In the files it saves, the delimiter depends on the preference set or on what the file had before it was opened.

Great. Glad you got what you needed. :slight_smile: Taking into account what I’ve written above, the code could be tidied up to:


tell application "BBEdit"
	tell text of front text window
		replace "(^.+	)(.+$)" options {starting at top:true, search mode:grep} using "\\2"
		add line breaks
		replace "- " using "" options {starting at top:true}
		-- Or, if you still want to delete space-dash-space at the beginnings of lines:
		-- replace "^ - " options {starting at top:true, search mode:grep} using ""
		sort lines it output options {replacing target:true}
		process duplicate lines output options {deleting duplicates:true}
		copy
	end tell
end tell

I find BBEdit’s replace command easier to read when replace and using are adjacent.

replace "(^.+	)(.+$)" using "\\2" options {search mode:grep, starting at top:true}

Process duplicate lines and sort will not remove extra blank lines, so you have to do this with replace if desired.

add line breaks breaks soft-wrapped lines, so I’d think it should be last in Nigel’s final script of post #11, otherwise it will probably break process duplicate lines and sort lines.