Hi there. I have a big bunch of text, marked up with codes that get
replaced with fancy HTML stuff in BBEdit. I’m doing it with AppleScripts
that do “replace” operations. Everything is great except for one new
situation.
Normally, we select some text, and run a script to operate on the
selection. So I can write things like this:
*replace* the_search_string using the_replace_string searching in selection
*of* *text window* 1 *of* myDocument options {search
mode:literal, starting at top:true, wrap
around:false, backwards:false, case sensitive:false, match
words:false, extend selection:false}
In my new situation, I want to replace spaces with %20 but only in PART of
the selected text. I have text like this:
[Joe J. Smith]<[email protected]><subject=Sign me up!><body=I want to
subscribe to the newsletter.>
(There would be hundreds of lines of text selected, with various email
addresses and “subject=“ text. Maybe 1 percent of the selected text
contains text that I want to change)
Here, I want to change “subject=Sign me up!” to “subject=Sign%20me%20up!”
I can’t do this:
*replace* " " using "%20" searching in selection *of* *text window* 1 *of*
myDocument
… because that would change “Joe J. Smith” to “Joe%20J%20Smith” (among
other things. There is a lot of text in the selection that should retain
its spaces rather than convert to %20.
What I want to do is (somehow) do a Find (searching in the original
selection), looking for “subject=“ and then some GREP to get everything up
to the closing “>”. I was hoping that by adding “with selecting match” I
could select EVERY matching string (a discontiguous selection) and then
search in this new selection for spaces and replace them all in one move
with %20. Basically I’m trying to change the selection to be only the parts
I want to make changes in.
My problem is, when I do a Find and “with selecting match” it only finds
and selects the FIRST instance. I can then do my replacing within that
text, but that only changes the first instance. What I’m doing now is
looping through the document, changing the first instance that the Find
finds, and then coming around on the loop to pick up the next one, and the
next one, etc. It’s a little on the slow side and I have to set the number
of loops to something pretty high to be sure I cover the most extreme
situation (which is hard to predict so I am doing 1000 loops). A
modification would be to figure out how many loops I have to make, which I
think I could do, but I don't want to loop at all.
I know I can do a Find manually in BBEdit and choose the Find All option.
Can I make use of the results of a Find All in a script? Maybe do a Find…
with a Find All option, and then do my replace on the results of that Find?
Maybe my approach is all wrong here. Even if it is, it would be useful to
know whether I can do a Find… to create a discontiguous selection. If I
can, my problem goes away— I do a Find... within the original selection, I
select the results box that Find... (producing a new, smaller selection), I
run a single “replace” on that new selection. If I can’t do that, I wonder
what is the right way to go.
Thanks for any assistance you can give. Basically I'm trying to get out of
doing a loop.
--
This is the BBEdit Talk public discussion group. If you have a feature request
or need technical support, please email "[email protected]" rather than
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/bbedit/8ab01435-e68a-4025-ba87-062c99fe481bn%40googlegroups.com.