如何在 applescript 中将文本写入 iterm2 会话?

How to write text to a iterm2 session in applescript?

我正在尝试为 iTerm2 创建一个脚本,它将创建一个会话(通过拆分),然后将文本输入到新创建的会话中。如何激活新创建的会话?

tell application "iTerm"
    tell current session of current window
        split horizontally with default profile
    end tell

    -- Here is what I need help with?
    set _new_session to <what goes here> of current window
    tell _new_session
        write text "ls"
    end tell
end tell

当然,我在发布问题后不久就想通了。要获得会话,您必须通过选项卡,而我没有这样做。这是一个工作示例:

tell application "iTerm"
    tell current session of current window
        split horizontally with default profile
    end tell

    tell current tab of current window
        set _new_session to last item of sessions
    end tell

    tell _new_session
        select
        write text "ls"
    end tell
end tell

如果您的最终目标是在 shell 上执行脚本,您也可以使用

set sc to "ping google.com"
do shell script (quoted form of sc)