How can I insert a text into Terminal.app using AppleScript?
I don't mean to execute it (press Enter). Only to insert it.
My current script almost works:
tell application "Terminal"
activate
tell application "System Events" to keystroke "TextToInsert" using {}
end tell
The trouble is when this is executed by a hot key, e.g. CMD-T, and then the pressed CMD interferes with the keystrokes and changes the desired behavior.
I need either to tell the keystroke
command to ignore any modifier keys or to use other command to write the text there. I think write
should do it but have no idea how.
Best Answer
You can try something like, first copying the text to your clipboard, that you want inserted into Terminal, then have the AppleScript code paste the clipboard into Terminal.
If you have all of your Terminal windows set to open in tabs rather than separate windows, this following AppleScript code should account for any of the situations as mentioned by @user3439894 in his comment to your post.