How to prepend to text file in AppleScript?
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.
Updated Answer:
The code below it what I'd use to replace the original code offered as there appears to be a bug in AppleScript when using the do shell script
command in the manner originally presented that isn't present went the code, in a representative manner, is run in a bash
script in Terminal.
Note that anytimeopen for access
is used, it needs to be coded to trap any errors and try and close the file, which this attempts to do. That said it is not necessarily then only error handling I'd employ and all coding answer I present are done so as proof of concept and the onus to write code employing reasonable error handing is yours to fulfill.
As coded, this will create the target file if it doesn't exist while adding the text to add to it and if it does exist, places the text to add as the top line of the target file.
set targetFilePathname to (POSIX path of (path to desktop as string) & "My Fruit Log.txt")
-- # set theTextToWrite to "This text will be written at the top of the file." & "\n"
set theTextToWrite to "This text will be written at the top of the file." & "\n"
set theOriginalText to ""
try
set theOriginalText to (read targetFilePathname) as string
end try
-- # set theTextToWrite to theTextToWrite & "\n" & theOriginalText
set theTextToWrite to theTextToWrite & "\n" & theOriginalText
try
set referenceNumber to open for access targetFilePathname with write permission
write theTextToWrite to referenceNumber starting at 0
close access referenceNumber
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access referenceNumber
end try
return
end try
You could do it this way, avoiding the shell scripts.
set the_file to choose file
set the_content to read of the_file
set the_new_stuff to "Wassup?" & return & the_content
set the_file_path to the_file as string
set open_for_writing to open for access file the_file_path with write permission
write the_new_stuff to open_for_writing starting at 0
close access open_for_writing
Basically you read the text in the .txt file, you put "Wassup?" and a return in front of the text, and then you write over what's in the file (because you start at position 0).
You could copy the current content of the file to a variable, then copy TextToWrite
to TargetFilePath
and then append the previous content back to TargetFilePath
.
set x to (do shell script "cat " & quoted form of TargetFilePath)
do shell script "echo " & quoted form of TextToWrite & " > " & quoted form of TargetFilePath
do shell script "echo " & quoted form of x & " >> " & quoted form of TargetFilePath