Type text into iTerm terminal from apple script

1

I'm trying to paste text from apple script to iTerm.

activate application "iTerm"
tell application "iTerm"
    tell current tab of current window
        set cmd to "command"
        keystroke cmd
        keystroke return
    end tell
end tell

But i'm getting an error:

iTerm got an error: Can’t get keystroke "command" of current tab of current window.

Any ideas how to fix this?

As I here there is available write text command https://www.iterm2.com/documentation-scripting.html

Artsiom Miklushou

Posted 2017-11-24T15:25:34.950

Reputation: 123

Answers

4

The write text command is done within the current session, so use the following example AppleScript code:

activate application "iTerm"
tell application "iTerm"
    tell current session of current window
        set cmd to "command"
        write text cmd
    end tell
end tell

To use the keystroke command use System Events:

activate application "iTerm"
tell application "iTerm"
    tell current tab of current window
        set cmd to "command"
        tell application "System Events"
            keystroke cmd
            keystroke return
        end tell
    end tell
end tell

Note: You may need to strategically place a delay command to allow iTerm to fully activate before the write text or keystroke commands are executed.

user3439894

Posted 2017-11-24T15:25:34.950

Reputation: 206