> On Feb 16, 2023, at 22:15, ChristianBoyce <[email protected]> wrote:
> ...
> 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] <mailto:[email protected]>><subject=Sign me 
> up!><body=I want to subscribe to the newsletter.>
> 
> ...


Hey Christian,

You can't do this very easily with AppleScript, particularly because of the 
selected text requirement.

The only way I know of using AppleScript would be to loop the find using the 
selecting match option and to replace your spaces in the selected match.

You'd have to grab the first line and last line of the selection and test for 
those as you loop through. I've done this kind of thing now and then, and for a 
long document it's pretty slow.
a
You'd really expect to be able to find all instances and then loop through them 
directly, but unfortunately BBEdit doesn't support that.

Provided I didn't have to worry about "<subject=Sign me up!>" spanning lines 
I'd do this with a Perl filter.

The selection goes to Perl as STDIN.  You can then loop through line by line 
testing for your pattern and when found replace the spaces in only it, and once 
complete the selection will be replaced with STDOUT.

Something like this should suffice, and compared to AppleScript it will be 
lightning quick.

#!/usr/bin/env perl -sw
# ----------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2012/11/27 08:12
# dMod: 2023/02/16 23:18
# Task: Encode Spaces in Text Selection for a Given Pattern.
# Tags: @ccstone, @Shell, @Script, @Encode, @Spaces, @RegEx
# ----------------------------------------------------------
use v5.010;

while (<>) {
    if ( m!(.+)(<subject=.+?>)(.+)! ) {
        my $prefixStr  = $1;
        my $newSubject = $2;
        my $suffixStr  = $3;

        $newSubject =~ s!\h!\$20!g;
        say $prefixStr . $newSubject . $suffixStr;
    }
}

# ----------------------------------------------------------

--
Take Care,
Chris

-- 
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/EB9E66F7-3F97-44CF-B830-0A9229CAF21D%40gmail.com.

Reply via email to