Search code examples
terminalkeystrokeosascript

Can't send keystrokes from Terminal to app using osascript in MacOS


I have TextEdit open. I want to enter "hello" in TextEdit, but not by typing it in myself, but using a command from Terminal.

I tried this:

osascript -e 'tell application "TextEdit" to keystroke "hello"'

but it give the following error:

31:48: execution error: TextEdit got an error: Can’t get keystroke "hello". (-1728)

What am I doing wrong?


Solution

  • red_menace is right, this works:

    osascript -e 'activate application "TextEdit"'; osascript -e 'tell application "System Events" to keystroke "hello"'