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

applescriptbugmacostext;

I have a text variable of class text that shows something like this when it is returned:

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

I want to be able to delete specific lines from the text, without affecting the rest of the variable.

For example: delete {1, 3} to get this result:

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

The method described in this answer has a serious bug:

"How to delete a specific line of a paragraph in AppleScript?"

The linked method to delete lines in a paragraph actually converts all linefeeds in the text variable to returns. In other words, it is impossible to run this code more than once on the same variable.

For example, the following code:

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
-- Employing the same method on the same variable:
set varText to do shell script "sed -e '1d;3d' <<< " & quoted form of varText
return varText

returns

""

Since this method only works correctly if the lines of the text variable are the product of a linefeed (as it should), the problem is not that this method fails to perceive a return as a new line (as I had originally claimed in this question). The problem is that this code introduces a return to the text variable in the first place.


Thus, I want a solution that will allow me to run the same text variable through the solution more than once in the same AppleScript.

In other words, I am looking for a method to remove a specific line from a paragraph that does not have this bug and does not insert a return anywhere in the text.

Best Answer

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