MacOS – Is it possible to have the “Find” field engaged on open of .rtf file

applescriptautomationmacostextedit

When I click on the Dock shortcut of a specific .rtf file to open the file in TextEdit, my cursor is by default set in front of the first character of the first line of the document.

However, I would prefer if my cursor is instead set to the search field of the "Find" function. The "Find" function can be manually accessed by pressing ⌘ command + F .

Is it possible for the cursor to be automatically placed in this location when the file is opened, perhaps with AppleScript?

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.

set fileToOpen to (path to documents folder as text) & "file name.rtf"

tell application "TextEdit"
    open file fileToOpen
    activate
    tell application "System Events"
        key code 3 using command down -- ⌘F
        --  # Uncomment the line below if you want to clear the Find field of previous search.
        -- key code 51 -- Clear previous Find data by pressing the delete key.
    end tell
end tell

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.