Automation question

42 views
Skip to first unread message

Christian Boyce

unread,
Sep 20, 2019, 4:18:31 PM9/20/19
to BBEdit Talk
I have a BBEdit document looking something like this:

{Tag_A}
Some text, could be multiple sentences, multiple paragraphs.
{/Tag_A}

Some other text here

{Tag_B}
More text here. Could be multiple sentences, multiple paragraphs, as above.
{/Tag_B}

{Tag_A}
Some text, could be multiple sentences, multiple paragraphs.
{/Tag_A}

Some other text here

{Tag_B}
More text here. Could be multiple sentences, multiple paragraphs, as above.
{/Tag_B}

Etc.

I have two AppleScripts which currently work on whatever text it selected. For purposes of this discussion let's say I have Script A which applies some custom capitalization, and Script B that wraps certain words in <strong></strong> tags.

The scripts work great as long as I select, by hand, the text I want to work with. At the end of the script, after I have done the operations I want to do, I set the selection to the newly computed text.

The way it SEEMS to work is the selected text stays selected the whole time, and at the very end I am writing into the selection. It is as if I pasted a result into a selection— the selected text is replaced with what I pasted.

What I'm trying to do is automate the selections. That is, in the example above, I want to (somehow) automatically select everything between the first set of {Tag_A}{/Tag_A} tags, and run Script A on it. Then I want to (somehow) automatically select everything between the first {Tag_B}{/Tag_B} tags, and run Script B. Then I want to select the text between the next set of A tags and run Script A, and then select the text between the next set of B tags and run Script B, and so on.

Any suggestions? What I'm describing is sort of an invisible hand that makes the selections for me. There might be a better way to do it so I am open to suggestions!

I don't have any trouble with the Scripts A and B. Those are fine. What I need is a way to tell BBEdit to select the right chunks of text to apply these scripts to.

THANK YOU—

Christian Boyce

Christian Boyce

unread,
Sep 22, 2019, 1:10:32 AM9/22/19
to bbe...@googlegroups.com
Well how about that: I figured out how to answer my own question. In case you're interested, here's how I did it.

The key is to realize that I can find the text surrounded by a known set of tags using a grep search, with the option "selecting match" turned on, and with the "starting at top" option also turned on. This means I can run a script to automatically find text surrounded by certain tag, and more importantly SELECT that text. My other scripts (UPPERCASE.scpt and lowercase.scpt) work on the selected text. The first time through the loop, I get the first "find" result, the result is selected, and the proper script (UPPERCASE.scpt or lowercase.scpt) operates on that. The next time through the loop, it finds the second "find" result (because the first one isn't there anymore— it's been changed), and the result is selected, and the proper script is called… etc. 

Obviously the way I've done it ("repeat 100 times") is a little crude, but it works. I could figure out how many instances of each script tag is in the document, and only loop that many times, but this is so fast already that I don't mind the inefficiency. I'll fix that up later. 

Anyway, here's what I came up with. Might help someone else. The key take-aways are the "with selecting match" and "starting at top." 

tell application "BBEdit"
set SearchStringOne to "{Script_One}\\n(.*)\\n{/Script_One}"
set ReplaceStringOne to "\\1"
--
set SearchStringTwo to "{Script_Two}\\n(.*)\\n{/Script_Two}"
set ReplaceStringTwo to "\\1"
set myDocument to document 1
tell myDocument
repeat 100 times
set myResult to find SearchStringOne options {search mode:grep, starting at top:true} with selecting match
if (found of result) then
set text of found object of myResult to grep substitution of ReplaceStringOne
tell script "UPPERCASE.scpt"
UPPERCASE(myDocument)— for demonstration purposes, this script turns text to all UPPERCASE
end tell
end if
--
set myResult to find SearchStringTwo options {search mode:grep, starting at top:true} with selecting match
if (found of result) then
set text of found object of myResult to grep substitution of ReplaceStringTwo
tell script "lowercase.scpt"
lowercase(myDocument)— for demonstration purposes, this script turns text to all lowercase.
end tell
end if
end repeat
end tell
end tell

--
Christian Boyce


Reply all
Reply to author
Forward
0 new messages