如何使用Python API在iTerm会话中运行某些命令

时间:2020-06-03 09:44:48

标签: python iterm2 iterm

我正在尝试编写一个脚本,对iTerm2进行一些自动化。 由于我对AppleScript一无所知,因此我正在使用Python API for iTerm

基本上,我需要做的是,将屏幕拆分为6个窗口,并在每个窗口中本地运行6个微服务。我可以成功拆分屏幕,但是无法在其中任何一个中运行命令。

谢谢。

我现有的代码如下

#!/usr/bin/env python3.7

import iterm2
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.

async def main(connection):
    # Your code goes here. Here's a bit of example code that adds a tab to the current window:
    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    if window is not None:
        await window.async_create_tab()
    else:
        # You can view this message in the script console.
        print("No current window")

    leftOne = app.current_terminal_window.current_tab.current_session
    rightOne = await leftOne.async_split_pane(vertical=True)
    leftTwo = await leftOne.async_split_pane()
    leftThree = await leftOne.async_split_pane()
    rightTwo = await rightOne.async_split_pane()
    rightThree = await rightOne.async_split_pane()

    await leftOne.async_activate()
    await leftOne.as

iterm2.run_until_complete(main)

1 个答案:

答案 0 :(得分:1)

您可以使用Sessions方法将击键发送到async_send_text()

在您的代码中,以下将执行命令:

leftOne = app.current_terminal_window.current_tab.current_session
await leftOne.async_send_text('whoami\n')