iTerm2 Python API:如何运行命令并捕获结果

Jar*_*rek 6 python api macos iterm iterm2

使用 API 发送命令非常简单:

#!/usr/bin/env python3

import tkinter as tk
import iterm2
# To install, update, or remove packages from PyPI, use Scripts > Manage > Manage Dependencies...

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)
    session = app.current_window.current_tab.current_session
    if session is not None:
        await session.async_send_text('date\n')     
Run Code Online (Sandbox Code Playgroud)

但是如何捕获该命令的结果呢?我查过:

  • ScreenStreamer
  • async_get_contents

(参见API 文档

但没有什么对我真正有用。有什么想法吗?