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.:return
isx0D
vs. the more proper use oflinefeed
(x0A
) instead, in a case where the variable is data and not a disposable message.Or:
linefeed
(x0A
) as it should be on a Mac.The
do shell script
command has a bug as it convertsx0A
tox0D
after what's returned from the command line having the expectedx0A
endings passed back. I confirmed this because if I use the following:Then
outfile
containslinefeed
(x0A
) endings so the same is returned todo shell script
but it then erroneously converts thex0A
line endings tox0D
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 followinghandler
and example code.The following assumes that
varText
has already been set by either of the methods described above.You could then use the following again to delete more lines from
varText
:The image below shows and example of calling the
do shell script " sed ..."
command twice.