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).

  • 1
    Wow, that's a strange coincidence. The specific of the line to erase is being the 8th line. The contents of the line is irrelevant. Commented Jan 23, 2017 at 11:23

1 Answer 1


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 '" & lineNumberToErase & "d' " & 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 '" & lineNumberToErase & "d' " & quoted form of targetFile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .