Hello, I am not anywhere close to an expert Applescript user, so please make your replies gentle. 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.
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.
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
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
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.
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.
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.
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
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. 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.