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
I don't know if there's an easy or pretty way to do what you're asking. In other words, you can't just simply say something like open target file and move cursor to first empty line
, however the code below will do that.
set filePathName to POSIX path of (path to desktop as string) & "My Fruit Log.txt"
set firstEmptyLineNumber to (do shell script "awk '$1 == \"\" {print NR;exit;}' \"" & filePathName & "\"")
do shell script "open -e " & quoted form of filePathName
tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
repeat (firstEmptyLineNumber - 1) times
key code 125 # Down Arrow
end repeat
end tell
The code above is coded to open the text file in TextEdit, which is what open -e
in the second
do shell script
command is doing and it's coded this way because System Events needs to know where to sent the down arrow keystrokes to. If you want a different text editor then remove
the -e
and the open
command will open it in whatever app the .txt file extension is registered to open with. Then you'll also need to change:
tell application "System Events" to tell process "TextEdit"
To:
tell application "System Events" to tell front process
And replace:
tell application "TextEdit" to activate
With: delay 1
In the first do shell script
command, awk
is getting the line number of the first empty line and exiting and this is what's used to calculate how many down arrow keystrokes to repeat
.
I modified my original answer slightly to get rid of the delay
commmand but wanted to add my take on adc's answer while eliminating all the menu_click
stuff.
set filePathName to POSIX path of (path to desktop as string) & "My Fruit Log.txt"
set firstEmptyLineNumber to (do shell script "awk '$1 == \"\" {print NR;exit;}' \"" & filePathName & "\"")
if firstEmptyLineNumber = "" then set firstEmptyLineNumber to 1 as string
do shell script "open -e " & quoted form of filePathName
tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
key code 37 using command down # ⌘L
keystroke firstEmptyLineNumber
keystroke return
key code 123 # Left Arrow - So the line is not highlighted.
end tell
Update:
The code below has been modified from the code above at the top of my answer, not my take on arc's answer although it's applicable there too, to address the issue you're having with TextEdit writing two carriage returns instead of the expected and normal two line feeds for an empty line after a line containing content or two empty lines in a row.
set filePathName to POSIX path of (path to desktop as string) & "My Fruit Log.txt"
set firstEmptyLineNumber to (do shell script "awk '$1 == \"\" {print NR;exit;}' \"" & filePathName & "\"")
if firstEmptyLineNumber is equal to "" then
set firstEmptyLineNumber to (do shell script "awk '/\r\r/{print NR+1;exit;}' \"" & filePathName & "\"")
end if
do shell script "open -e " & quoted form of filePathName
tell application "TextEdit" to activate
tell application "System Events" to tell process "TextEdit"
repeat (firstEmptyLineNumber - 1) times
key code 125 # Down Arrow
end repeat
end tell
Note: Although the modified code works with your testfile.txt file from the link in your comment, nonetheless I personally do not subscribe to this workaround and would instead find out the root cause of the issue and fix it and your files!
Best Answer
Here's a shell script:
You can wrap this in a ‘do shell script’ if you really want AppleScript, but for an Automator script, you can use a Run Shell Script action and just use the shell script.
Here's an AppleScript wrapper for the script, where the TextToWrite variable and the Target_Filepath variable have been defined elsewhere per your comments: