AppleScript: How to open text file and move cursor to specific location (signaled by specific character)?
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 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 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!