search: ^\s*(.*[^\s])\s*$
replace: \1

search: (.*)\r(.*)\n
replace: \1 \2\n

James Reynolds
https://magnusviri.com

> On Oct 30, 2024, at 11:33, Andrew Ward <[email protected]> wrote:
> 
> Any suggestions on how to join two lines, deleting all trailing and leading 
> whitespace replacing with a single space?
> 
> I've tried various script suggestions by chatGPT, Gemini(useless) and 
> Perplexity (Almost works except does not remove leading whitespace from the 
> line to be concatenated).   Here is the almost working .scpt
> 
> tell application "BBEdit"
>     tell front text window
>         if (count of characters of selection) is 0 then
>             select line (get startLine of selection)
>         end if
>         set oldText to contents of selection
>         log "Old text: " & oldText -- Debug output
>         set newText to my joinLines(oldText)
>         log "New text: " & newText -- Debug output
>         set contents of selection to newText
>     end tell
> end tell
> 
> on joinLines(theText)
>     set AppleScript's text item delimiters to {return, linefeed, character id 
> 8233, character id 8232}
>     set theLines to text items of theText
>     if (count of theLines) > 1 then
>         -- Trim trailing whitespace and add a single space to the first line
>         set firstLine to do shell script "echo " & quoted form of (item 1 of 
> theLines) & " | sed 's/[[:space:]]*$/ /'"
>         
>         -- Remove all leading whitespace from the second line
>         set secondLine to do shell script "echo " & quoted form of (item 2 of 
> theLines) & " | sed 's/^[[:space:]]*//'"
>         
>         -- Combine the modified first and second lines
>         set combinedLines to firstLine & secondLine
>         
>         -- Add any remaining lines unchanged
>         if (count of theLines) > 2 then
>             set AppleScript's text item delimiters to return
>             set remainingLines to (items 3 thru -1 of theLines) as text
>             set combinedLines to combinedLines & return & remainingLines
>         end if
>         
>         return combinedLines
>     else
>         return theText
>     end if
> end joinLines
> 
> 
> 
> -- 
> This is the BBEdit Talk public discussion group. If you have a feature 
> request or believe that the application isn't working correctly, please email 
> "[email protected]" rather than posting here. Follow @bbedit on Mastodon: 
> <https://mastodon.social/@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 visit 
> https://groups.google.com/d/msgid/bbedit/2b22def4-0b67-42c0-a2de-a45080b3f5a3n%40googlegroups.com.

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or believe that the application isn't working correctly, please email 
"[email protected]" rather than posting here. Follow @bbedit on Mastodon: 
<https://mastodon.social/@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 visit 
https://groups.google.com/d/msgid/bbedit/FA7A0A69-B407-4D94-8DB6-17FBB4812A42%40gmail.com.

Reply via email to