How to delete a specific line of a paragraph in AppleScript, while retaining the original format of the text?

Okay, I've deleted the original and first edit because you've edited your originally question to the point it's easier to write a new answer altogether.

Since your originally question showed the following line of code, as an example of how the variable may be set, I'm going to include it to say the following.

Whether the varText has been set by e.g.:

set varText to (return & "This is sentence 1." & return & "This is sentence 2." & return & "This is sentence 3." & return & "This is sentence 4.")
  • In which return is x0D vs. the more proper use of linefeed (x0A) instead, in a case where the variable is data and not a disposable message.

Or:

set varText to "
This is sentence 1.
This is sentence 2.
This is sentence 3.
This is sentence 4."
  • In which each of these lines actually end with a linefeed (x0A) as it should be on a Mac.

The do shell script command has a bug as it converts x0A to x0D after what's returned from the command line having the expected x0A endings passed back. I confirmed this because if I use the following:

set varText to "
This is sentence 1.
This is sentence 2.
This is sentence 3.
This is sentence 4."

set varText to do shell script "sed  -e '1d;3d' <<< " & quoted form of varText & " | tee $HOME/Desktop/outfile"

Then outfile contains linefeed (x0A) endings so the same is returned to do shell script but it then erroneously converts the x0A line endings to x0D endings which can then handled by the following:

So, to accommodate the bug, always insure the content of the variable passed and returned contains x0A line endings by using the following handler and example code.

The following assumes that varText has already been set by either of the methods described above.

on ensureLinesEndWith0A(varText)
    set varText to paragraphs of varText
    set oldTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to linefeed
    set varText to varText as string
    set AppleScript's text item delimiters to oldTID
    return varText
end ensureLinesEndWith0A

set varText to ensureLinesEndWith0A(varText)
set varText to do shell script "sed  -e '1d;3d' <<< " & quoted form of varText
set varText to ensureLinesEndWith0A(varText)

You could then use the following again to delete more lines from varText:

set varText to ensureLinesEndWith0A(varText)
set varText to do shell script "sed  -e '1d;3d' <<< " & quoted form of varText
set varText to ensureLinesEndWith0A(varText)

The image below shows and example of calling the do shell script " sed ..." command twice.

image of code example


Example:

set varText to "
This is sentence 1.
This is sentence 2.
This is sentence 3.
This is sentence 4."

set varText to do shell script "sed -e '1d;3d' <<< " & quoted form of varText

Returns:

"This is sentence 1.
This is sentence 3.
This is sentence 4."

Update: As a result of a discovery mentioned in the Edit: of How to delete a specific line of a “return”-based paragraph in AppleScript?, let me make the following statement:

NOTE: Unfortunately what's returned in this case has carriage return (x0D) characters instead of the expected new line (x0A) characters and in my opinion is a bug!

It's a bug because: The do shell script "sed ..." command when run in Terminal with that varText returns with \n (x0A) not \r (x0D) as it's supposed to. Even the compiled do shell script "sed ..." command in Script Editor still has \n (x0A) not \r (x0D) however why it's being returned with \r (x0D) intend of the expected \n (x0A) is a mystery to me at the moment and I'm going to consider this an AppleScript bug since it not mirroring the expected behavior of the same when run in Terminal.

So, to account for the bug in the results of the do shell script "sed ..." command, this is how I'd handle it. After the do shell script "sed ..." command use the following lines of code:

set newLine to "\n"
set varText to paragraphs of varText
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to newLine
set varText to varText as string
set AppleScript's text item delimiters to oldTID

Now what's returned contains new line (x0A) characters as it should have to begin with if there wasn't this bug, not carriage return (x0D) characters.


Note: When compiled, the set newLine to "\n" line of code will show as below:

set newLine to "
"