4

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?

  • Is this just for an individual file that you access regularly? If yes, just write an AppleScript app to open the target document and the Find field. Then place the AppleScript app in the Dock after you give it the icon of the document and remove the document from the Dock. – user3439894 Mar 1 '17 at 15:37
  • @user3439894 Correct. I only want the “Find” field engaged on open of this one, specific .rtf file. – rubik's sphere Mar 1 '17 at 18:16
  • Then the only solution I can think of is to do as outlined in my answer. – user3439894 Mar 1 '17 at 18:18
3

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.

  • Note: The example AppleScript code in this answer is minimal code in that it does not include any error handling as may be appropriate when programming, e.g. the use of try statements, on error handlers and or checking on the files existence before trying to open it, etc. It's expected that you add relevant and appropriate error handing to whatever code you implement. – user3439894 Mar 1 '17 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .