I have a .txt file saved on my computer. I want Automator to add a bit of text to the very top of the .txt file. I do not want any text in the .txt file to be overwritten. I also want a new line to be created for the new text.
The following command adds the text to the .txt file, but at the end of the file:
set TextToWrite to "This sentence should be the first line of the .txt file."
set TargetFilepath to "/Users/Me/Desktop/Documents/My File.txt"
do shell script "echo " & quoted form of TextToWrite & " >> " & quoted form of TargetFilepath
Can prepending to a .txt file be accomplished in AppleScript? Thank you.
Update:
I have discovered a pernicious little bug in all of the current 4 answers to this question.
Blank lines in a .txt file created on a Mac should show up as 0A0A
in a hex editor. But, for whatever reason, the answers provided by Matteo, Christian Boyce, and user3439894 convert blank lines into 0D0D
. In practice, 0D0D
produces an identical result to 0A0A
-- they both look like a blank line -- but Mac perceives 0D0D
as a carriage return while Mac perceives 0A0A
as a line feed.
Here's the test that I did to discover this issue in the answers provided by Matteo and user3439894 (and if you do the same, you can reproduce the issue):
Create a New Document in TextEdit. Click "Make Plain Text" in the Format menu. Save the empty file as a .txt file.
Run the AppleScript code provided by Matteo, Christian Boyce, or user3439894 using this .txt file as TargetFilepath.
Open the .txt file in TextEdit and manually add a blank line to the file by using your Enter key.
Open the .txt file in your hex editor and confirm that this new blank line is
0A0A
.Run the AppleScript code again.
Open the .txt file in your hex editor. You will now notice that the
0A0A
has been converted to0D0D
.
Here's the test that I did to discover this issue in the answer provided by Christian Boyce:
Create a New Document in TextEdit. Type "This is a sentence.". The file cannot be empty if you want run the code provided by Christian Boyce without error. Click "Make Plain Text" in the Format menu. Save the file as a .txt file.
Run the AppleScript code that Christian Boyce provided. When asked to choose a file, open this .txt file.
Open the .txt file in TextEdit and manually add one blank line in between the new "Wassup?" line and the original "This is a sentence" line by using your Enter key.
Open the .txt file in your hex editor. Notice that this new blank line is
0A0D
. It should be, however,0A0A
. So, while the answer provided by Christian Boyce does not change all instances of0A0A
to0D0D
(in the way that the answers provided by Matteo and user3439894 do), it does use0D
instead of0A
when putting "Wassup?" on a new line.
It took me a long time to discover this glitch, as one could never tell that there was a problem with these AppleScript solutions unless one opens the .txt file in a hex editor.
The problem with the answer provided by Mateusz Szlosek is more severe; it replaces all instances of 0A0A
with 20
, thereby ensuring that there are no blank lines anywhere in the .txt file (which, unlike the other answers, meant that I could easily identify that it had a bug without opening the .txt file in a hex editor).
So, I am re-opening this question, now hoping that someone can provide a way to prepend text to a .txt file using AppleScript, and without converting line feeds to carriage returns in the process.
If you are curious why I need the line feeds to be preserved, it is because I have a later process that writes to the first blank line of the .txt file, and this process does not work properly if the line feeds are replaced with carriage returns.
bash
script and running it in Terminal alone produces the expected and wanted results of having just line feeds not carriage returns whether starting with a zero length file or a file that already has ASCII Text content. The introduction of carriage returns when using AppleScript'sdo shell script
command with the samebash
commands in this use case would have to be considered a bug, due to the fact that it is not producing the expected behavior. Continued in next comment...do shell script
command anywhere in it. Can you tell me if my answer is okay to use?