5

I am trying to write a script that'd do some automation on my iTerm2. I am using the Python API for iTerm since I know nothing about AppleScript.

What I need to do is basically, split the screen is 6 windows and run 6 microservices locally in each of them. I was successfully able to split the screen but I am unable to run a command in any of them.

Thanks in advance.

My existing code is as follow

#!/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
  • Run a command is easiest part but for last few days I'm scratching my head how to run a command and capture result... Commented Nov 5, 2021 at 16:07

1 Answer 1

7

You can send keystrokes to Sessions using the async_send_text() method.

In your code, the following will execute a command:

leftOne = app.current_terminal_window.current_tab.current_session
await leftOne.async_send_text('whoami\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.