In Yosemite, I can open a file with any application in the Dock by dragging the file's icon over the application while holding the command and option keys down.
This only works if the targeted application is indeed able to open the file, of course.
Another option, when the application is not currently in your Dock, is to click the file with the right mouse button and choose an application from the submenu Open with
, which is the second item in the context menu. This submenu lists the applications that can open the file you clicked on, with the default one at the top.
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
TextEdit does not have an option to preform some actions when a file is opened, e.g. to run an
on open()
handler that does things upon opening a file. That said, if you have a particular file you want to open in TextEdit and have Find show at the top of the document, you'll need to do it programatically.The example AppleScript code below is saved as an application and placed in the Dock instead of the target document itself. This of course will have to be on the left side of the Dock separator vs. the right side of the separator where the document's icon is.
Note: You can make the Dock Tile of the AppleScript application that opens the target file have the same icon as the target file, by copying and pasting the icon from the target file's Get Info sheet to its Get Info sheet before dragging the AppleScript application you created to open the target file to the Dock.
Hint: As a suggestion, when you save the AppleScript application, name it the same name as the document it's opening.