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

如何在applescript中向iterm2会话写入文本?,applescript,iterm2,Applescript,Iterm2,我正在尝试为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

我正在尝试为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
告诉应用程序“iTerm”
告诉当前窗口的当前会话
使用默认配置文件水平拆分
结束语
--这就是我需要帮助的地方?
将新会话设置为当前窗口的
告诉你新的会话
写文本“ls”
结束语
结束语

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

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)

我的最终目标是构建一个脚本,该脚本将打开一个终端,并在终端编辑器(micro)打开的情况下创建第二个会话。然后,当脚本再次运行时,它将检查编辑器会话是否存在,如果存在,它将打开我发送的文件?为什么需要终端会话?我正在编写一个脚本,以在现有终端编辑器中打开一个文件,该编辑器已在其他窗格中打开。您想在shell中使用类似于nano的edior打开该文件,还是只是一个将启动第三方软件的打开命令?在shell(单独的窗格)中,我在胡闹着和你做这件事。当然,我希望它能与任何终端编辑器(nano、emacs等)配合使用。它将使我能够维护一个类似于使用单个实例编辑器(例如atom)的工作流程,但完全在终端shell中。理想情况下,它在登录到远程会话时也能工作。只是我正在玩的东西,还没有投入很多时间,但我认为它会起作用。谢谢你的关心。