How can I delete a specific line of a .txt file in AppleScript? I have a .txt file saved on my computer. This is the location of the file:


  /Users/Me/Desktop/My Documents/My Fruit Log.txt


I want my AppleScript-based Automator application to delete a specific line of the .txt file.

The contents of the .txt file looks like this:



So, if:

set lineNumberToErase to 8


The resulting .txt file should be:



Notice how not only the text on that line is removed, but the entire line is gone as well.

Bonus points if the solution includes some sort of mechanism to ensure that not more than 1 line is removed from the file. My fear when automating deletions like this is that the script somehow erases the entire file (based on me making a small mistake somewhere).
Here's a do shell script AppleScript command solution using sed to delete the target line of the target file, in-place.

set targetFile to "/Users/Me/Desktop/My Documents/My Fruit Log.txt"
set lineNumberToErase to 8

do shell script "sed -i '' -e '" &amp; lineNumberToErase &amp; "d' " &amp; quoted form of targetFile




If you want a backup of the target file made, change the sed option -i '' to -i '.bak', e.g.:

do shell script "sed -i '.bak' -e '" &amp; lineNumberToErase &amp; "d' " &amp; quoted form of targetFile