So you want to select two lines and carry out this action on the selection,
or do you have a big document with lots of lines to clean up? Can you give
an example, before and after?

On Wed, 30 Oct 2024 at 17:34, 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
> <https://groups.google.com/d/msgid/bbedit/2b22def4-0b67-42c0-a2de-a45080b3f5a3n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Kaveh Bazargan PhD
Director
River Valley Technologies <http://rivervalley.io> ● Twitter
<https://twitter.com/rivervalley1000> ● LinkedIn
<https://www.linkedin.com/in/bazargankaveh/> ● ORCID
<https://orcid.org/0000-0002-1414-9098> ● @[email protected]
<https://mastodon.social/@kaveh1000>
*Accelerating the Communication of Research*

*
<https://www.linkedin.com/posts/bazargankaveh_ismte-innovation-award-recipient-kaveh-bazargan-activity-7039348552526921728-XAEB/?utm_source=share&utm_medium=member_desktop>
 [image:
https://rivervalley.io/gigabyte-wins-the-alpsp-scholarly-publishing-innovation-award-using-river-valleys-publishing-technology/]
<https://rivervalley.io/gigabyte-wins-the-alpsp-scholarly-publishing-innovation-award-using-river-valleys-publishing-technology/>*

-- 
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/CAJ2R9pgH32OS0RzOSd5V8omg0K2JmZtqOe%3DDoV%2Bm-n0VOwhpvg%40mail.gmail.com.

Reply via email to