MacOS – AppleScript: How to open text file and move cursor to specific location (signaled by specific character)

applescriptcursormacostext;

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.

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!