I have a .txt file saved on my computer. I want my Automator application to open the .txt file and then move the location of the cursor to the first blank line in the file.
For example, I have a .txt file entitled My Fruit Log.txt saved on my computer. This is what the contents of the .txt file may look like:
So, in the above instance, I want the file to be opened, and then the cursor moved to the second line of the file.
Note: It is not necessarily the second line from the top where I want the cursor to be sent to. Depending on what text has already been written to the file, I might want the cursor on a lower line. For example:
In the above case, I would want the cursor to be moved to the sixth line.
The location where I want the cursor will always be at least the second line from the top, but it could be as far down as the seventh line from the top.
So, the most reliable way to articulate where I want the cursor to be placed is "on the first instance of a blank line in the file."
I already have the code to open the file in TextEdit (borrowed from the top answer of this Stack Overflow question):
set Target_Filepath to POSIX file "/Users/Me/Desktop/My Fruit Log.txt"
tell application "Finder" to open Target_Filepath
Now I need the code to move the cursor. The default location of the cursor is the first line of the file.
Best Answer
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.The code above is coded to open the text file in TextEdit, which is what
open -e
in the seconddo 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 removethe
-e
and theopen
command will open it in whatever app the .txt file extension is registered to open with. Then you'll also need to change:To:
And replace:
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 torepeat
.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 themenu_click
stuff.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.
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!