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

3

我正在尝试创建一个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
2个回答

9

当然,在我发布问题后不久,我就弄清楚了这个问题。要获取会话,您必须通过选项卡进行操作,而我没有这样做。以下是一个可行的示例:

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

0
如果您的最终目标是在 shell 上执行脚本,您也可以使用以下方法:
set sc to "ping google.com"
do shell script (quoted form of sc)

我的最终目标是构建一个脚本,它将打开一个终端并创建一个带有终端编辑器(micro)的第二个会话。然后当脚本再次运行时,它将检查编辑器会话是否存在,如果存在,则会打开我发送的文件。 - slaughter98
所以你最终需要打开一个文件吗?为什么需要终端会话呢? - Pat_Morita
我正在编写一个脚本,以在不同的窗格中已经打开的终端编辑器中打开一个文件。 - slaughter98
你想在shell中使用像nano这样的编辑器打开它,还是只是一个启动第三方软件的open命令? - Pat_Morita
在 shell(单独的窗格)中,我正在尝试使用 micro 来实现这个。当然,我希望它能够与任何终端编辑器(nano、emacs 等)一起使用。这将使我能够保持类似于使用单个实例编辑器(例如 atom)的工作流程,但完全在终端 shell 中完成。理想情况下,它也可以在远程会话中使用。这只是我正在玩耍的东西,还没有花费太多时间,但我认为它会起作用。感谢您的关注。 - slaughter98

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接