Select continuous text string excluding the beginning and ending white spaces

Hello KM community,

How would one create a macro to select a continuous text string excluding the beginning and ending white spaces?

Example: given the following text
abc [#$/fox(/jumps/over)the/lazy/dog/123:-/93<82}=][\] 123

If mouse cursor is anywhere inside the text string, activating the macro would automatically select the full text string.
[#$/fox(/jumps/over)the/lazy/dog/123:-/93<82}=][\]

test1

Thanks,
Chris

If mouse cursor is anywhere inside the text string

What application (editor) is that ?

( These things are not really application-independent – the shortest answer to your question might be: by using a scriptable editor)

Application is BBEdit.

Good – there are plenty of BBEdit-scripters here, I think.

Newbie here. I was able to get this to work. But I'm sure there is a better and more elegant way of doing this.

test3

Others will be more familiar with the BBEdit scripting interface, but I think you should also be able to do this kind of thing:

Selection extended to first space to left and right.kmmacros (3.2 KB)


Expand disclosure triangle to view AppleScript
on extendToSpace(doc, isBackwards)
    using terms from application "BBEdit"
        tell doc to find "\\s" options ¬
            {search mode:grep, extend selection:true, backwards:isBackwards} ¬
                with selecting match
    end using terms from
end extendToSpace

on run
    tell application "BBEdit"
        set docs to text documents
        
        if 0 < (count of docs) then
            set doc to front text document
            
            if found of (my extendToSpace(doc, true)) then
                
                if found of (my extendToSpace(doc, false)) then
                    
                    tell text of doc
                        tell selection
                            set iStart to its characterOffset
                            set iEnd to iStart + ((its length) - 2)
                        end tell
                        
                        select (characters iStart thru iEnd)
                    end tell
                    {iStart, iEnd}
                else
                    "No space or newline found to right"
                end if
            else
                "No space or newline found to left"
            end if
        else
            "No document open in BBEdit"
        end if
    end tell
end run

ComplexPoint,

Many many thanks for your help! Your macro with AppleScript works beautifully and very fast. Thank you so much again, really appreciate it. :slight_smile:

Chris

1 Like